如何让我的代码打印出以下示例

时间:2016-11-27 06:37:39

标签: java

我正在做一个java项目,我遇到了一个问题。我试图弄清楚为什么它不计算物品的总价格,你可以在te输出中看到没有显示总价。

我得到以下输出(仅供参考FYI TEST CASE)

Test Case 1: 
ProductName:  
ProductPrice: 0.0
Order Quantity: 0
Total price: 0.0
Test Case 2: 
ProductName: Pens 
ProductPrice: 0.6
Order Quantity: 50
Total price: 0.0
Test Case 3: 
ProductName: Pencil's 
ProductPrice: 0.3
Order Quantity: 115
Total price: 0.0
Test Case 4: 
ProductName: Rulers 
ProductPrice: 1.2
Order Quantity: 4
Total price: 0.0
Test Case 5: 
ERROR: the discount value:115.0 is not valid

我的代码:

public class Order {
String ProductName;
double Price;
double Discount;
int Quantity;
double Total;
String Message; // intallising them as var 

boolean isDiscounted = false; //declaring the var also to false
boolean isValidOrder = true;

public static int OrderNum = 0; //static var  

public Order() {        // constructor #!
    isValidOrder= false;
    Message = "Error: You have not specified any parameters for the order";
    OrderNum++;
}
public Order(String ProductName, double Price, int Quantity){
     //calling from parameter 
    this.ProductName = ProductName;
    this.Quantity = Quantity; //receving the three parameters 
    this.Price = Price;
    OrderNum++;
}

public Order(String ProductName, double Price, int Quantity, double Discount){
    this.ProductName = ProductName;
    this.Price = Price;
    this.Quantity = Quantity;
    this.Discount = Discount;
    OrderNum++;

    if (Discount < 100){  // Will run if the Discount is < 100

         this.Discount = Discount;//assigning this to discount parameter to its instance variable 
        isDiscounted = true;
    }
    else {
        isValidOrder = false;

        Message = "ERROR: the discount value:"+ Discount +" is not valid";}

    }

    public void calculate(){
        if (isValidOrder=true){ //if its a vaild order
            System.out.println(Total = Quantity * Price);

        }
        else{ //if not valid order this will run 
            Message = "Error:order number: "+OrderNum + "cannot be totalled as it is invaild";
            isDiscounted = false; 
        }


        if (isDiscounted = true){ // runs if order is discounted
            System.out.println(Total = Quantity * Price - Quantity * Price * (Discount/100));
        }
         /*else{ //if not  discounted then this will run
            System.out.print(Total= Quantity * Price); */
        }


        public String toString(){
        if (isValidOrder){
                Message =  "ProductName: " + ProductName +"\n" +  "ProductPrice: " + Price +"\n"+  "Order Quantity: " + Quantity  +
                        "\n" +  "Total price: " + Total; 


                    //if the order is valid and not discounted
        }

        else if (isDiscounted ){

            Message = "Product Name: " + ProductName + "\n " + "Product Price:  $" + Price + "\n"+ "Order Quanity:" + Quantity + "\n " +
            "Discount: " + Discount + "%" + "\n" + "Total Price: $" + Total;
                // if the item is discounted 
        }
        return Message;


        }}





public class OrderCreator {


public static void main(String[] args){
    Order o1 = new Order (" ",0, 0);
    System.out.println("Test Case 1: " + "\n" + o1); //test case one

Order o2 = new Order ( "Pens ",0.60, 50 );
System.out.println("Test Case 2: " + "\n" + o2);

Order o3 = new Order ( "Pencil's ",0.30, 115, 0 );
System.out.println("Test Case 3: " + "\n" + o3);

Order o4 = new Order ( "Rulers ",1.20, 4, 15 ); //15% discount on this one
System.out.println("Test Case 4: " + "\n" + o4);

Order o5 = new Order ( "Pencil Sharpners ",2.05, 8,115 ); //should come up as error due to only 100%, 115% discount
System.out.println("Test Case 5: " + "\n" + o5);

}
}

1 个答案:

答案 0 :(得分:0)

需要在Order类

中的两个构造函数中运行calculate()方法
public Order(String ProductName, double Price, int Quantity){
     //calling from parameter 
    this.ProductName = ProductName;
    this.Quantity = Quantity; //receving the three parameters 
    this.Price = Price;
    OrderNum++;
    this.calculate();
}

public Order(String ProductName, double Price, int Quantity, double Discount){
    this.ProductName = ProductName;
    this.Price = Price;
    this.Quantity = Quantity;
    this.Discount = Discount;
    OrderNum++;

    if (Discount < 100){  // Will run if the Discount is < 100

         this.Discount = Discount;//assigning this to discount parameter to its instance variable 
        isDiscounted = true;
    }
    else {
        isValidOrder = false;

        Message = "ERROR: the discount value:"+ Discount +" is not valid";

    }
    this.calculate();
}

同样从calculate类中删除System.out.println,因为它不会以漂亮的格式输出。 toString方法将执行所有system.out.println

public void calculate(){
if (isValidOrder){ //if its a vaild order
    Total = Quantity * Price; 

}
else{ //if not valid order this will run 
    Message = "Error: order number: "+OrderNum + " cannot be totalled as it is invaild";
    isDiscounted = false; 
}


if (isDiscounted){ // runs if order is discounted
    Total = Quantity * Price - Quantity * Price * (Discount/100); 


}
 /*else{ //if not  discounted then this will run
    Total= Quantity * Price; */
}