【问题标题】:Calculation incorrect and can't figure out why计算不正确,无法弄清楚原因
【发布时间】:2013-09-24 00:02:43
【问题描述】:

我有 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%。

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

  • 该类将包含一个构造函数。构造函数是接收两个 参数、产品名称和数量。构造函数是传递这些 其超类 (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;
        }
}

}

【问题讨论】:

    标签: java math superclass


    【解决方案1】:

    这一行可能是您的错误的来源:

     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,任何时候乘以它都会遇到类似的算术问题。

    【讨论】:

    • 谢谢你的信息,我发帖的时候才发现忘记改折扣了
    猜你喜欢
    • 1970-01-01
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多