Java 计算不正确,无法找出原因

Java 计算不正确,无法找出原因,java,math,superclass,Java,Math,Superclass,我有3个类:Order、InternalOrder和OrderTester。我已经四处搜索了一段时间,对于我一直在寻找的东西,我只是无法尝试将这些示例更改为我需要的 所以我在InternalOrder和OrderTester类方面遇到了问题。到目前为止,我的代码都是 public class InternalOrder extends Order { public static final int DISCOUNT = 40/100; public InternalOrder(

我有3个类:Order、InternalOrder和OrderTester。我已经四处搜索了一段时间,对于我一直在寻找的东西,我只是无法尝试将这些示例更改为我需要的

所以我在InternalOrder和OrderTester类方面遇到了问题。到目前为止,我的代码都是

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...");
}

}

好吧,它去测试10,但现在一切都一样

以下是我需要的东西:

InternalOrder类将包含用于订购的Stellar Stability staff的逻辑 内部固定库存,作为其工作要求的一部分

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

InternalOrder类用于扩展Order类。 将包含名为折扣的最终静态字段。此字段用作的常量 员工收到的折扣率应设置为40%。 现在,我有这两个在我的部分,但我不完全确定下一部分

该类将包含一个构造函数。构造函数将接收两个 参数、产品名称和数量。构造函数要传递这些 其超类顺序的参数以及作为第三个参数的折扣常量 参数 我是否需要在超类中添加任何内容,因为这让我很困惑。使用OrderTester,我有一个很难导入的表,所以我将只生成几行

OrderTester类用于启动程序并测试订单和 InternalOrder类以确保其正常工作

有十个测试要运行,每个测试都应该采用自己的静态方法,并且 它们应该被称为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; 
折扣是零

原因是两个整数的除法结果被转换为最接近的较低整数-小数位数被简单地截断。因为40/100是0.4,所以删除小数点后,剩下的是0

您基本上有两种选择:

将其保存为百分比:

public static final int DISCOUNT_PERCENT = 40; 
使用可以存储小数的类型:

public static final double DISCOUNT = .4;

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

感谢您提供的信息,我在发布时才意识到我忘记更改折扣了
public static final double DISCOUNT = .4;