Alexander Song
Alexander Song

Reputation: 53

Don't really understand how to match two 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());
    }  
}

    

The getPrice method is to receive one parameter: productName. The method is to assign a valNeed to use two arrays to accomplish this. One array called products is to hold the list of product names. The other array called prices is to hold the price for that product. The index positions should match. You need to find the index position of the productName within the products array by using the binarySearch method. Once you have this position you will then need to assign the price from the prices array using this index position.

This is what I have so far, but i'm not entirely sure if its correct.

Upvotes: 0

Views: 81

Answers (1)

Ely
Ely

Reputation: 11152

No, it is not correct in general. The reason is that you sort the products array, but you keep the prices array unmodified, so the products don't correspond to their prices in general, i.e. the index positions do not match.

  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

Bear in mind the following.

  • Binary search can only be used in sorted collection
  • When you sort products array then you have to modify the prices array accordingly so that the index positions match. You don't do that in your app.
  • Also, as a side note, check your syntax. Your copied code does not compile. E.g. you missed a closing bracket for the getPrice function.

Upvotes: 1

Related Questions