2016-05-21 4 views
0

を表示するには取得できません20、30)、割引は、通常の合計、適用されません。は、だから私は、私はこれを作成するために必要なプロジェクトを持っている計算が

public 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" +"Discount : " + discount + "%" + "\n" + "Total Price: $" + total; 
     } 
     else { 
      return message; 
     } 
    return message; 
} 

私は私はあなたが私が言っているか理解している場合はわからないので、非常に初心者プログラマです。基本的に私はそれをリンクすることはできません:/

をここでは、コードの残りの部分です:

/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package order; 
import java.util.Arrays; 
/** 
* 
* @author Alexander 
*/ 
public class Order { 
    private static String[] products = { 
    "Compass", "Eraser", "Pen", "Pencil", "Pencil Case", "Pencil Sharpener", "Ruler", "Scissors" 
    }; 
    private static double[] prices = { 
    4.5, 0.5, 0.3, 0.6, 10, 0.3, 1.2, 2.5 
    }; 
    public static int orderNum = 0; // 
    private String productName; 
    private double price = 0; 
    private int discount = 0; 
    private final int minDiscount = 0; 
    private final int maxDiscount = 50; 
    private int quantity = 0; 
    private final int minQuantity = 0; 
    private final int maxQuantity = 1000; 
    private double total; 
    private String message; 
    private boolean isDiscounted = false; 
    private boolean isValidOrder = true; 

    public void calculate() { 
    if (isDiscounted == true) { 
     total = quantity * price - quantity * price * (discount/100); 
    } else { 
     total = quantity * price; 
    } 
    } 


    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.quantity = quantity; 
    this.productName = productName; 
    testQuantity(quantity); 
    getPrice(productName); 
    if (isValidOrder == true) { 
     calculate(); 
    } 
    orderNum++; 
    } 

    public Order(String productName, int quantity, int discount) { 
    this.productName = productName; 
    this.quantity = quantity; 
    this.discount = discount; 
    testQuantity(quantity); 
    testDiscount(discount); 
    getPrice(productName); 
    if (isValidOrder != false) { 
     calculate(); 
    } 
    orderNum++; 
    } 



    private void getPrice(String pce) { 
    Arrays.sort(products); 
    int searchProductArray = Arrays.binarySearch(products, pce); 
    if (searchProductArray >= 0) { 
     price = prices[searchProductArray]; 
     productName = products[searchProductArray]; 
     isValidOrder = true; 
    } else { 
     price = 0.0; 
     isValidOrder = false; 
     message = "**ERROR**: Invalid product name"; 
    } 
    } 




    public void testQuantity(int quantity) { 
    boolean isValidOrder = true; 
    if (quantity <= minQuantity) { 
     message = "**ERROR**: Invalid quantity. Quantity cannot be 0 or less"; 
     isValidOrder = false; 
    } else if (quantity > maxQuantity) { 
     message = "**ERROR**: Invalid quantity. Quantity cannot be greater than 1000"; 
     isValidOrder = false; 
    } else { 
     this.quantity = quantity; 
     this.isValidOrder = true; 
    } 
    } 

    public void testDiscount(int discount) { 
    boolean isDiscounted = false; 
    if (discount <= minDiscount) { 
     message = "**ERROR**: The discount rate cannot be lower than or equal to 0"; 
    } else if (discount > maxDiscount) { 
     message = "**ERROR**: The discount rate cannot be greater than 50"; 
    } else { 
     this.discount = discount; 
     this.isDiscounted = true; 
    } 
    } 

    public 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" + "Discount : " + discount + "%" + "\n" + "Total Price: $" + total; 
    } else { 
     return message; 
    } 
    return message; 
    } 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
    Order O1 = new Order("Compass", 20, 30); 




    System.out.println(O1.total); 

    OrderLaunch frame = new OrderLaunch(); 
    frame.setVisible(true); 
    } 
} 
+0

'if(isDiscounted == true)'と書かないでください。 'if(isDiscounted)'を使ってください。同様に、 'if(isValidOrder == true && isDiscounted == false)'は 'if(isValidOrder &&!isDiscounted)'でなければなりません。 – Andreas

答えて

1

整数除算!

このライン

total = quantity * price - quantity * price * (discount/100); 

整数ですので、割引は100

よりも低い場合、これはあなたが単にそれをダブル操作確認する必要があります0と評価された両方100でdiscountを分割:

total = quantity * price - quantity * price * (discount/100.0); 
+0

oh my。ありがとう、私はそれを逃した方法を知らない:( –

関連する問題