0

它希望我返回显示以下输出的“消息”字符串。如果订单有效但未打折,则输出中不需要折扣,但如果订单有效且有折扣,则输出中需要折扣。现在,我已经尝试了一些事情,但我无法弄清楚 =/

getOrderDetails 方法的主要工作是返回消息实例变量。如果订单有效且未打折,则设置消息,以便在使用适当的实例变量返回时显示如下(不带折扣实例变量),如下所示:

- Order number: 1
- Product name: Pencil
- Product Price: $0.6
- Order Quantity: 6
- Total Price: $3.6

如果订单有效且已打折,则更改消息以使其包含折扣实例变量,如下所示:

- Order number: 1
- Product name: Pencil
- Product Price: $0.6
- Order Quantity: 6
- Discount: 10%
- Total Price: $3.24

如果订单无效,则仅返回消息实例变量。

public class Order {

    public String productName; //Name
    public double price;  //Price
    public double discount;  //Discount
    public int quantity;  //Quantity
    public double total; //Total for the price multiplied with quantity (with or without discount)
    public String message; //Message
    public boolean isDiscounted = false; //Set to false, but if it's true, the discount comes in
    public boolean isValidOrder = true; //Set to true, but if it's false, the Order becomes invalid and Message comes in
    public static int orderNum = 0; //Order number set to 0 as default


        public void setProductName(String productName) {
            this.productName = productName;
        }//Set the name
        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }//Set the price
        public void setDiscount(double discount) {
            this.discount = discount;
        }//Set the discount
        public void setPrice(double price) {
            this.price = price;
        }//Set the price
        public void setOrderDetails(boolean isValidOrder, boolean isDiscounted) {
            this.isValidOrder = isValidOrder;
        }//Set the Order Details
        public String getProductName() {
            return productName;
        }//Get the name
        public int getQuantity() {
            return quantity;
        }//Get the price
        public double getDiscount() {
            return discount;
        }//Get the discount
        public double getPrice() {
            return price;
        }//Get the price

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

      public Order(String productName, int quantity){  //Constructor 2
          this.productName = productName;
          this.quantity = quantity;
          getPrice(this.productName);


          if(isValidOrder != false){
              calculate();
          }
          orderNum++;

      }

    public Order(String productName, int quantity, double discount){ //Constructor 3
        this.productName = productName;
        testQuantity(quantity);
        getPrice(productName);
                this.discount = discount;

          if(isValidOrder != false){
              calculate();
          }
                  orderNum++;
    }

    public String getOrderDetails(){ //Order Details

        if(isValidOrder == true && isDiscounted == true){

            message = "Order Number: " + orderNum + "\n" + "Product Name: " + productName + "\n" + "Product Price: $" + price + "\n" + "Order Quantity: " + this.quantity + "\n" + "Discount: " + this.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: " + this.quantity + "\n" + "Total Price: $" + total;  
        }  
                return message; 
    }


    public void calculate(){ //Calculate for total
        if(this.isDiscounted == false){
            total = quantity * price;
        } 
                else {
            total = quantity * price - quantity * price * (discount / 100 );  
        }
    }

    public void getPrice(String productName){ //Switch so you can find what item ordered and allocate the correct price
        switch(productName){
        case "Pencil":
            this.price = 0.6;
            break;
        case "Pen":
            this.price = 0.3;
            break;
        case "Ruler":
            this.price = 1.2;
                        break;
        case "Pencil Sharpener":
            this.price = 0.3;
            break;
        case "Compass":
            this.price = 4.5;
            break;
        case "Eraser":
            this.price = 4.5;
            break;
        case "Scissors":
            this.price = 2.5;
                        break;
        case "Pencil Case":
            this.price = 10.0;
            break;
        default:
            this.price = 0.0;
            this.isValidOrder = false;
            this.message = "**ERROR** : Invalid product name";
                        break;
                }
    }

    public void testDiscount(double discount) { //Testing for a Discount
        if (discount <=0) {
            isDiscounted = false;
            message = "**ERROR** : The discount rate cannot be lower than or equal to 0.";
        }
        else if (discount >50) {
            isDiscounted = false;
            message = "**ERROR** : The discount rate cannot be higher than 50.";
        } 
       else {
       this.discount = discount;        
       this.isDiscounted = true;     
        }  
    }


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

}
4

0 回答 0