真的不明白如何匹配两个数组

时间:2016-05-16 06:30:29

标签: java arrays

/*
 * 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的索引位置。获得此仓位后,您需要使用此指数位置从价格数组中分配价格。

这是我到目前为止所做的,但我不完全确定它是否正确。

1 个答案:

答案 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

请记住以下内容。

  • 二进制搜索只能用于已排序的集合
  • 对产品数组进行排序时,必须相应地修改价格数组,以使索引位置匹配。你没有在你的应用程序中这样做。
  • 此外,作为旁注,请检查您的语法。您复制的代码无法编译。例如。你错过了getPrice函数的结束括号。