2013-09-24 71 views
-1

我有3个类:Order,InternalOrderOrderTester。我一直在寻找一段时间,对于我一直在寻找的东西,我只是无法尝试将这些示例更改为我需要的东西。计算不正确,不知道为什么

所以我遇到了InternalOrderOrderTester类的问题。到目前为止我的代码因为这两者都是...

public class InternalOrder extends Order { 
    public static final int DISCOUNT = 40/100; 

    public InternalOrder(String productName, int quantity) { 
     super(productName, quantity, DISCOUNT); 
     } 

public void printInternalReport() { 
    System.out.println("Printing internal report..."); 
} 

} 

而且

public class OrderTester { 

public static void main(String[] args) { 
    testCase1(); 
    testCase2(); 
    testCase3(); 
    testCase4(); 
    testCase5(); 
    testCase6(); 
    testCase7(); 
    testCase8(); 
    testCase9(); 
    testCase10(); 
} 
public static void testCase1() { 
    Order ord = new Order(); 

    System.out.println("Test Number : " + Order.orderNum + " Type of Order : Normal, " + "Product Name : " + "Order Quantity : " + "Discount : "); 
} 
public static void testCase2() { 
    Order ord = new Order(); 

    System.out.println("Test Number : " + Order.orderNum + " Type of Order : Normal, " + "Product Name : " + "Order Quantity : " + "Discount : "); 
} 

好,它进入测试10,但它是目前所有相同。

这里有我需要的东西:

InternalOrder类是包含内部谁下令 固定的股票作为其工作要求的一部分恒星固定工作人员的逻辑。

内部订单自动获得40%的折扣。

  • InternalOrder类是扩展Order类。
  • 是包含一个名为DISCOUNTfinal static字段。该字段用作 的常数,该折扣率是员工收到的折扣率,应设为40%。

现在,我有这两个,但我不完全确定下一部分。

  • 该类包含一个构造函数。构造函数将接收两个 参数,productName和数量。构造函数是将这些参数 传递给它的超类(Order)以及DISCOUNT常量作为第三个参数 。

我是否需要在超类中添加任何内容,因为这很让我困惑。与OrderTester,我有一个表很难导入,所以我只会产生几条线。

OrderTester类用于启动该程序,并测试OrderInternalOrder类以确保其工作正常。

有十个测试需要运行,每个测试应该是自己的一个静态方法,并且它们应该被称为testCase1()testCase10()。每个测试应该测试按照 如下表:

Test Number Type of Order Product Name Order Quantity Discount 
1   "Normal"  "N/A"   "N/A"  "N/A" 

有了这个测试。我不确定我的数量和折扣是如何产生“不适用”的。

如果你需要我的其他代码,我会在下面发布它。

public class Order { 

private String productName; 
private double price; 
private int discount; 
private int quantity; 
private double total; 
private String message; 
private boolean isDiscounted; 
private boolean isValidOrder; 
public static int orderNum = 0; 

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

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


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

    } 

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

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

private String getOrderDetails(){ 
    message = message; 
    if(isValidOrder == true && isDiscounted == false){ 

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


private void calculate(){ 

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

private void getPrice(String productName){ 
    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 "Erasor": 
     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; 
      } 
} 

private void testDiscount(int discount) { 
    if (discount <=0) { 
     message = "**ERROR**: The discount rate cannot be lower than or equal to 0."; 
    } 
    else if (discount >50) { 
     message = "**ERROR**: The discount rate cannot be higher than 50."; 
    } else { 
    this.discount = discount;   
    this.isDiscounted = true;  
    } 
} 


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

} 

回答

4

此行可能是你错误的来源:

public static final int DISCOUNT = 40/100; 

DISCOUNT为零。

原因是两个整数除法的结果是tru truated到最接近的较小整数 - 小数位被简单地删除。由于40/100.4,在小数位被删除后,您将剩下0

您有两种基本选择:

保存它作为一个百分比:

public static final int DISCOUNT_PERCENT = 40; 

使用,可以存储小数类型:

public static final double DISCOUNT = .4; 

你的生活可能会,如果你会更容易选择了第二个选项,因为如果你使用int,任何时候你乘以它,你将面临类似的算术问题。

+0

感谢您提供的信息,只是在我发布时才实际意识到我忘记了更改折扣 – ChokeOnThis

相关问题