2016-11-27 57 views
-4

即时做一个java项目,我遇到了一个问题。我试图找出为什么它不计算项目的总价格,因为您可以在输出中看到没有总价格显示。如何让我的代码打印出下面的例子

我得到下面的输出(FYI测试案例五应该是这样的)

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

} 
} 
+0

你能显示主吗? – MordechayS

+0

计算非折扣总额的'else'被注释掉了,所以总数总是为零,因为你没有任何打折的测试输入。 –

+0

刚刚添加了主。 –

回答

0

需要在两个构造函数的运行计算()方法in Order类别

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

同时从计算类中移除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; */ 
} 
+0

非常感谢兄弟。祝你有美好的一天。 –

相关问题