无法显示消息

时间:2016-05-21 16:12:17

标签: java

我有一个getOrderDetails()方法,但是当我输入(" Compass",2000,20)时,我无法显示它应该通过的错误消息。 *错误数量必须小于1000"。当我运行程序时,它将计算2000而不是显示错误消息。

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

这是邮件的代码:

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

这是我的其余代码:

/*
 * 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);
    }      
}

3 个答案:

答案 0 :(得分:0)

您的代码存在一些问题:

1)testQuantity方法正在创建一个本地isValidOrder布尔值,从而掩盖了成员isValidOrder布尔值。

示例

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)在 testQuantity 方法中将成员isValidOrder boolean设置为false后,您将在getPrice方法中将其设置为true。

示例

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

如果您解决了这两个问题,那么这些调用将为您提供所需的输出

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

错误:数量无效。数量不能大于1000

答案 1 :(得分:0)

您正在使用变量|检查是否运行计算。仍然使用无效数量值进行计算的原因是,在您的数量测试中将isValidOrder方法设置为false后,您在isValidOrder方法中设置getPrice为真。

答案 2 :(得分:0)

在你的代码中,方法private void getPrice(String pce)设置isValidOrder = true;这是错的。 评论线和&amp;它会很好:)

Fragment