簡體   English   中英

無法顯示消息

[英]Can't get message to display

我有一個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);
    }      
}

您的代碼有一些問題:

1) testQuantity方法正在創建一個局部的isValidOrder布爾值,從而掩蓋了成員isValidOrder boolean。

示例

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

您正在使用變量isValidOrder來檢查是否運行計算。 它仍在使用無效數量值進行計算的原因是,在數量測試中將其設置為false后,在getPrice方法isValidOrder設置isValidOrder設置為true。

在你的代碼中,方法private void getPrice(String pce)設置isValidOrder = true; 這是錯的。 評論該行&它會很好:)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM