[英]Calculation incorrect and can't figure out why
我有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...");
}
}
和
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
類。 DISCOUNT
的final static
字段。 此字段用作員工收到的折現率的常數,應設置為40%。 現在,我有這兩個部分,但是我不確定下一部分。
DISCOUNT
常量傳遞給其超類(Order)。 我是否需要在超類中添加任何內容,因為這讓我很困惑。 有了OrderTester
,我有了一個很難導入的表,所以我只產生幾行。
OrderTester
類用於啟動程序並測試Order
和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;
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.