【发布时间】:2016-09-11 21:23:44
【问题描述】:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package order;
import java.util.Arrays;
/**
*
* @author Alexander
*/
public class Order {
private static String[] products = {"Compass", "Eraser", "Pen", "Pencil","Pencil Case", "Pencil Sharpener", "Ruler", "Scissors"};
private static double[] prices = {4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5};
public static int orderNum = 0; //
private String productName ;
private double price;
private int discount;
private final int minDiscount = 0;
private final int maxDiscount = 50;
private int quantity;
private final int minQuantity = 0;
private final int maxQuantity = 1000;
private double total;
private String message;
private boolean isDiscounted = false;
private boolean isValidOrder = true;
public Order (){
isValidOrder = false;
message = "**ERROR** : Order number cannot be totalled as no details have been supplied.";
orderNum++;
}
public Order (String productName, int quantity) {
this.quantity = quantity;
this.productName = productName;
testQuantity(quantity);
if (isValidOrder = true){
calculate();
}
orderNum++;
}
public Order (String productName, int quantity, int discount) {
this.productName = productName;
this.quantity = quantity;
this.discount = discount;
testQuantity(quantity);
testDiscount(discount);
getPrice(productName);
if (isValidOrder = true){
calculate();
}
orderNum++;
}
public String getOrderDetails(){
if(isValidOrder == true && isDiscounted == true){
message = "Order Number: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Discount: " + discount + "%" + "\n" + "Total Price: $" + total;
}
else if(isValidOrder == true && isDiscounted == false){
message = "Order Number: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + quantity + "\n" + "Total Price: $" + total;
}
return message;
}
private void getPrice(String pce) {
Arrays.sort(products);
int searchProductArray = Arrays.binarySearch(products, pce);
if (searchProductArray >= 0) {
price = prices[searchProductArray];
productName = products [searchProductArray];
isValidOrder = true;
}
else {
price = 0.0;
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
}
public void calculate (){
if (isDiscounted == false){
total = quantity * price;
}
else {
total = quantity * price - quantity * price * (discount/10);
}
}
public void testQuantity(int quantity){
boolean isValidOrder = true;
if (quantity <= minQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
isValidOrder = false;
}
else if (quantity > maxQuantity) {
message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
isValidOrder = false;
}
else {
this.quantity = quantity;
this.isValidOrder = true;
}
}
public void testDiscount (int discount) {
boolean isDiscounted = false;
if (discount <= minDiscount) {
message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
isDiscounted = false;
}
else if (discount > maxDiscount) {
message = "**ERROR**: The discount rate cannot be greater than 50";
isValidOrder = false;
}
else {
this.discount = discount;
this.isDiscounted = true;
this.isValidOrder = true;
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Order O1 = new Order();
O1.getPrice("Compass");
System.out.println(O1.getOrderDetails());
}
}
getPrice 方法是接收一个参数:productName。方法是分配一个 valNeed 以使用两个数组来完成此操作。一个称为 products 的数组是保存产品名称的列表。另一个称为价格的数组是保存该产品的价格。索引位置应该匹配。您需要使用 binarySearch 方法在 products 数组中找到 productName 的索引位置。一旦你有了这个位置,你就需要使用这个索引位置从价格数组中分配价格。
这是我目前所拥有的,但我不完全确定它是否正确。
【问题讨论】:
-
stackoverflow.com/help/mcve。如果问题是 getPrice 函数,只需显示该函数和相关代码即可。此外,您提供的代码无法编译,据我所知函数不能嵌套。
-
“这是我目前所拥有的,但我不完全确定它是否正确。” - 我不确定你的问题/问题在这里是否足够清楚。您的代码似乎可以运行并且没有重大编译错误(除了 getPrice 上缺少括号可能是复制粘贴错误?)。需要记住的是,方法
Arrays.sort(products);将重新订购您的产品标题,但不会重新订购prices,一旦您对它进行排序,一旦您有点搞砸了。 Consder 制作了一个Product类,其中有一个price和title这样两个保持联系。