/*
* 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的索引位置。获得此仓位后,您需要使用此指数位置从价格数组中分配价格。
这是我到目前为止所做的,但我不完全确定它是否正确。
答案 0 :(得分:1)
不,一般来说不正确。原因是您对产品数组进行了排序,但是您保持价格数组未经修改,因此产品通常与其价格不符,即索引位置不匹配。
private void getPrice(String s) {
Arrays.sort(products); <--- sort products, but prices' indices remain unchanged
int searchedProductIndex = Arrays.binarySearch(products, s);
if (searchedProductIndex >= 0) {
this.price = prices[searchedProductIndex];
this.isValidOrder = true;
} else {
price = 0.0;
isValidOrder = false;
message = "**ERROR**: Invalid product name";
}
} <--- closing curly bracket
请记住以下内容。