CashRegister.java
/**
A cash register totals up sales and computes change due.
*/
public class CashRegister
{
private double purchase;
private double payment;
...
/**
Constructs a cash register with no money in it.
*/
public CashRegister()
{
purchase = 0;
payment = 0;
...
}
/**
Records the sale of an item.
@param amount the price of the item
*/
public void recordPurchase(double amount)
{
double total = purchase + amount;
...
purchase = total;
}
/**
Enters the payment received from the customer.
@param amount the amount of the payment
*/
public void enterPayment(double amount)
{
payment = amount;
}
/**
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;
purchase = 0;
payment = 0;
return change;
}
/**
Get the total amount of all sales for the day.
@param return sale total
*/
...
/**
Get the total number of sales for the day.
@param return the number of sales.
*/
...
/**
Reset counters and totals for the next days sales.
@param none
*/
...
}
CashRegisterTester.java
/**
A class to test the CashRegister class.
*/
public class CashRegisterTester
{
public static void main(String[] args)
{
CashRegister register = new CashRegister();
// transaction #1
register.recordPurchase(30);
register.recordPurchase(10);
register.enterPayment(50);
double change = register.giveChange();
System.out.println(change);
System.out.println("Expected: ...");
// transaction #2
register.recordPurchase(20);
register.enterPayment(20);
change = register.giveChange();
System.out.print("Change: ");
System.out.println(change);
System.out.println("Expected: ...");
// test new functionality
System.out.print("Total: ");
System.out.println(register.getSalesTotal());
System.out.println("Expected: ...");
System.out.print("Count: ");
System.out.println(register.getSalesCount());
System.out.println("Expected: 2");
register.reset();
System.out.print("Total: ");
System.out.println(register.getSalesTotal());
System.out.println("Expected: ...");
System.out.print("Count: ");
System.out.println(register.getSalesCount());
System.out.println("Expected: ...");
}
}