CashRegister.java.html

  1  /**
  2     A cash register totals up sales and computes change due.
  3  */
  4  public class CashRegister
  5  {
  6     public static final double QUARTER_VALUE = 0.25;
  7     public static final double DIME_VALUE = 0.1;
  8     public static final double NICKEL_VALUE = 0.05;
  9     public static final double PENNY_VALUE = 0.01;
 10  
 11     private double purchase;
 12     private double payment;
 13  
 14     /**
 15        Constructs a cash register with no money in it.
 16     */
 17     public CashRegister()
 18     {
 19        purchase = 0;
 20        payment = 0;
 21     }
 22  
 23     /**
 24        Records the purchase price of an item.
 25        @param amount the price of the purchased item
 26     */
 27     public void recordPurchase(double amount)
 28     {
 29        purchase = purchase + amount;
 30     }
 31     
 32     /**
 33        Processes the payment received from the customer.
 34        @param dollars the number of dollars in the payment
 35        @param quarters the number of quarters in the payment
 36        @param dimes the number of dimes in the payment
 37        @param nickels the number of nickels in the payment
 38        @param pennies the number of pennies in the payment
 39     */
 40     public void receivePayment(int dollars, int quarters, 
 41           int dimes, int nickels, int pennies)
 42     {
 43        payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
 44              + nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
 45     }
 46     
 47     /**
 48        Computes the change due and resets the machine for the next customer.
 49        @return the change due to the customer
 50     */
 51     public double giveChange()
 52     {
 53        double change = payment - purchase;
 54        purchase = 0;
 55        payment = 0;
 56        return change;
 57     }
 58  }