2016-02-25 35 views
0

我剛剛完成了一個基本的收銀機程序。幾乎所有的東西都在工作,除了輸出的最後,我必須顯示每日總銷售額。Java CashRegister類的總銷售額不會正確加起來

收款機類

public class CashRegister 
{ 
    private double purchase; 
    private double payment; 
    private double totalTax; 
    private double taxRate; 
    private double tax; 
    private double salesTotal; 
    private double salesCount; 
    private double amount; 
    private double rate; 
    private double taxPurchase; 
    private double taxable; 
    /** 
    Constructs a cash register with no money in it. 
    */ 
    public CashRegister(double rate) 
    { 
     purchase = 0; 
     payment = 0; 
    taxRate = rate; 
    } 
    /** 
    Records the sale of an item. 
    @param amount the price of the item 
    */ 
    public void recordPurchase(double amount) 
    { 
     purchase = purchase + amount; 
    salesTotal = amount + salesTotal; 
    } 
    /** 
    Records the sale of a taxable item and compute the total tax. 
    @param amount the price of the item 
    */ 
    public void recordTaxablePurchase(double amount) 
    { 
    //taxPurchase = taxPurchase + amount; 
     totalTax = totalTax + amount + (amount* (taxRate/100)); 
    } 

    /** 
    Enters the payment received from the customer. 
    @param amount the amount of the payment 
    */ 
    public void enterPayment(double amount) 
    { 
     payment = amount; 

    } 
    /** 
    Returns the total tax due 
    @return the totalTax 
    */ 
    public double getTotalTax() 
    { 

     return totalTax; 
    } 
    /** 
    Computes the change due and resets the machine for the next customer. 
    @return the change due to the customer 
    */ 
    public double giveChange() 
    { 
     double change = payment - purchase - totalTax; 
    salesTotal += purchase; 
     purchase = 0; 
     payment = 0; 
    //totalTax = 0; 
    salesCount++; 
    salesTotal++; 
     return change; 
    } 

    public double getSalesTotal(){ 
     return purchase + totalTax; 

    } 

    public double getSalesCount(){ 
     return salesCount; 
    } 

    public void reset(){ 
     amount = 0; 
     purchase = 0; 
     totalTax = 0; 
     salesTotal = 0; 
     salesCount = 0; 
    } 
    public double showPayment(){ 
     return payment; 
    } 

    public double getTotalPurchase(){ 
     return payment + totalTax; 
    } 
} 

收銀測試類

public class CashRegisterTester{ 
    public static void main(String [] args){ 

    CashRegister cash1 = new CashRegister(7.5); 
     System.out.println("Customer 1:"); 
     cash1.recordPurchase(20.00); 
     cash1.enterPayment(20.00); 
     System.out.println(" Change is: " + cash1.giveChange()); 
     System.out.println(" Expected is 0.0"); 

     System.out.println(); 
     System.out.println("Customer 2:"); 
     cash1.recordPurchase(30.00); 
     cash1.recordPurchase(10.00); 
     cash1.enterPayment(50.00); 
     System.out.println(" Change is: " + cash1.giveChange()); 
     System.out.println(" Expected is 10.0"); 

     //cash1.reset(); 
     System.out.println(); 
     System.out.println("Customer 3:"); 
     cash1.recordTaxablePurchase(80.00); 
     cash1.recordPurchase(70.00); 
     cash1.recordTaxablePurchase(50.00); 
     cash1.enterPayment(220.00);  
     System.out.println(" Total Sales: " + cash1.getSalesTotal()); 
     System.out.println(" Payment Given: " + cash1.showPayment()); 
     System.out.println(" Change is: " + cash1.giveChange()); 
     System.out.println(" Expected is: 7.0"); 

     System.out.println(); 
     System.out.println(" Daily Totals: "); 
     System.out.println(" Total Sales: " + "$ " + cash1.getSalesTotal()); 
     System.out.println(" Number of Sales: " + cash1.getSalesCount()); 

    } 



} 

的電流輸出爲:

Customer 1: 
Change is: 0.0 
Expected is 0.0 

Customer 2: 
Change is: 10.0 
Expected is 10.0 

Customer 3: 
Total Sales: 209.75 
Payment Given: 220.0 
Change is: 10.25 
Expected is: 7.0 

Daily Totals: 
Total Sales: $ 139.75 
Number of Sales: 3.0 

一切都只是在每日合計總銷售額正確,應該是269.75。我很困惑,爲什麼這不起作用。任何幫助將是偉大的,謝謝。

回答

1

每次您撥打giveChange()即表示您將購買金額重置爲0.因此,您的giveSalesCount僅返回從您的應納稅銷售中總計的購買總額。

當您計算變更時,您可以將支付金額存儲在其他地方,或者在不刪除您的購買交易的情況下找到另一種方式進行計算。

+0

終於明白了,謝謝! – BrianAndris