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