1  /**
  2     A cash register totals up sales and computes change due.
  3  */
  4  public class CashRegister
  5  {
  6     private double purchase;
  7     private double payment;
  8  
  9     /**
 10        Constructs a cash register with no money in it.
 11     */
 12     public CashRegister()
 13     {
 14        purchase = 0;
 15        payment = 0;
 16     }
 17  
 18     /**
 19        Records the sale of an item.
 20        @param amount the price of the item
 21     */
 22     public void recordPurchase(double amount)
 23     {
 24        double total = purchase + amount;
 25        purchase = total;
 26     }
 27  
 28     /**
 29        Enters the payment received from the customer.
 30        @param amount the amount of the payment
 31     */
 32     public void enterPayment(double amount)
 33     {
 34        payment = amount;
 35     }
 36  
 37     /**
 38        Computes the change due and resets the machine for the next customer.
 39        @return the change due to the customer
 40     */
 41     public double giveChange()
 42     {   
 43        double change = payment - purchase;
 44        purchase = 0;
 45        payment = 0;
 46        return change;
 47     }
 48  }