简体   繁体   中英

Can't get message to display

I have a getOrderDetails() method, but when I enter in ("Compass", 2000, 20) I can't get it to display the Error message that its supposed to pass through. *Error quantity must be less than 1000". When I run the program, it will calculate for 2000 instead of displaying the error message.

 public String getOrderDetails(){
        message = message;
        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;  
        }
        else 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 {
            return message;  
        }
    return message; 
}

This is the code for the message :

 public void testQuantity(int quantity){
          boolean isValidOrder = true;
          if (this.quantity <= minQuantity) {
              message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
              isValidOrder = false;
          }
          else if (this.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 (this.discount <= minDiscount) {
              message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
              }
          else if (this.discount > maxDiscount) {
              message = "**ERROR**: The discount rate cannot be greater than 50";
              }
          else {
              this.discount = discount;
              this.isDiscounted = true;
               }
    }

This is the rest of my code:

/*
 * 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 = 0;
    private int discount = 0;
    private final int minDiscount = 0;
    private final int maxDiscount = 50;
    private int quantity = 0;
    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 void calculate (){
            if (isDiscounted == true){
                total = quantity * price - quantity * price * (discount/100.0);
                System.out.println(total);
            }
         else {
                total = quantity * price;
                System.out.println(total);
                 }
         }


    public Order (){
        isValidOrder = false;
        message =  "**ERROR** : Order number cannot be totalled as no details have been supplied.";
        orderNum++;   
       }

    public Order (String productName, int quantity) {
        orderNum++;  
        this.quantity = quantity;
          this.productName = productName;
          testQuantity(quantity);
          getPrice(productName);
          if (isValidOrder == true){
              calculate();
          }
          }

    public Order (String productName, int quantity, int discount) {
        orderNum++; 
        this.productName = productName;
        this.quantity = quantity;
        this.discount = discount;
        testQuantity(quantity);
        testDiscount(discount);
        getPrice(productName);
        if (isValidOrder != false){
              calculate();
          }
      }



   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 testQuantity(int quantity){
          boolean isValidOrder = true;
          if (this.quantity <= minQuantity) {
              message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
              isValidOrder = false;
          }
          else if (this.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 (this.discount <= minDiscount) {
              message = "**ERROR**: The discount rate cannot be lower than or equal to 0";
              }
          else if (this.discount > maxDiscount) {
              message = "**ERROR**: The discount rate cannot be greater than 50";
              }
          else {
              this.discount = discount;
              this.isDiscounted = true;
               }
    }

    public String getOrderDetails(){
        message = message;
        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;  
        }
        else 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 {
            return message;  
        }
    return message; 
}

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Order O1 = new Order("Compass" , 2000, 30);
        System.out.println(O1.getOrderDetails());

        OrderLaunch frame = new OrderLaunch();
        frame.setVisible(true);
    }      
}

Your code has a few issues:

1) The testQuantity method is creating a local isValidOrder boolean and thus overshadowing the member isValidOrder boolean.

Example :

public void testQuantity(int quantity){
      // ----------------------
      // Remove this boolean here...
      // boolean isValidOrder = true;
      // ----------------------
      if (this.quantity <= minQuantity) {
          message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less";
          isValidOrder = false;
      }
      else if (this.quantity > maxQuantity) {
          message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000";
          isValidOrder = false;
      }
      else {
          this.quantity = quantity;
          this.isValidOrder = true;
      }
}

2) After setting the member isValidOrder boolean to false in the testQuantity method you are setting it back to true in the getPrice method.

Example :

 private void getPrice(String pce) {
        Arrays.sort(products);
        int searchProductArray = Arrays.binarySearch(products, pce);
        if (searchProductArray >= 0) {
        price = prices[searchProductArray];
        productName = products [searchProductArray];

       // ----------------------
       // Comment this boolean here for now until you figure things out
       // isValidOrder = true;
       // ----------------------
        } 
        else {
        price = 0.0;
        isValidOrder = false;
        message = "**ERROR**: Invalid product name";
        }
    }

If you fix those 2 issues, then these calls will give you the desired output

Order O1 = new Order("Compass" , 2000, 30);
System.out.println(O1.getOrderDetails());

ERROR : Invalid quantity. Quantity cannot be greater than 1000

You're using the variable isValidOrder to check wheter or not to run the calculation. The reason it is still calculating with an invalid quantity value is that your setting isValidOrder to true in your getPrice method, after it's been set to false in your quantity test.

in your code the method private void getPrice(String pce) setting the isValidOrder = true; which is wrong. Comment that line & it'll woks fine :)

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";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM