-1

我有 3 个课程OrderInternalOrderOrderTester。我已经搜索了一段时间,并且我一直在寻找的东西我只是无法尝试将这些示例更改为我需要的内容。

所以我在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课程将包含 Stellar Stationary 员工的逻辑,他们在内部订购固定库存作为其工作要求的一部分。

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

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

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

  • 该类将包含一个构造函数。构造函数是接收两个参数productName和quantity。构造函数是将这些参数传递给它的超类(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"

有了这个测试。当我的 Quantity 和 Discount 是整数时,我不确定如何产生“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

1 回答 1

4

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

 public static final int DISCOUNT = 40/100; 

DISCOUNT为零。

原因是两个整数相除的结果被截断为最接近的较低整数 - 小数位被简单地截去。因为40/100is .4,小数位被删除后,你就剩下0.

你基本上有两种选择:

将其保存为百分比:

public static final int DISCOUNT_PERCENT = 40; 

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

public static final double DISCOUNT = .4;

如果您选择第二个选项,您的生活可能会更轻松,因为如果您使用int,无论何时乘以它,您都会面临类似的算术问题。

于 2013-09-24T00:07:03.600 回答