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。我很困惑,为什么这不起作用。任何帮助将是伟大的,谢谢。
终于明白了,谢谢! – BrianAndris