繁体   English   中英

计算错误,无法找出原因

[英]Calculation incorrect and can't figure out why

我有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类。
  • 是要包含一个名为DISCOUNTfinal static字段。 此字段用作员工收到的折现率的常数,应设置为40%。

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

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

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

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

有十个测试要运行,每个测试应使用自己的静态方法,并且应将其称为testCase1()testCase10() 每个测试应按照下表进行测试:

Test Number  Type of Order  Product Name  Order Quantity  Discount
1           "Normal"       "N/A"           "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;
        }
}

}

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

 public static final int DISCOUNT = 40/100; 

DISCOUNT为零。

原因是两个整数的除法结果被舍去为最接近的小数-小数点被简单地切掉了。 由于40/100.4 ,因此删除小数位后,您将得到0

您基本上有两个选择:

将其保存为百分比:

public static final int DISCOUNT_PERCENT = 40; 

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

public static final double DISCOUNT = .4;

如果选择第二个选项,您的生活可能会更轻松,因为如果使用int ,则每次乘以int都会遇到类似的算术问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM