CashRegister.java.html

  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     }
 33     
 34     /**
 35        Enters the payment received from the customer.
 36        @param dollars the number of dollars in the payment
 37        @param quarters the number of quarters in the payment
 38        @param dimes the number of dimes in the payment
 39        @param nickels the number of nickels in the payment
 40        @param pennies the number of pennies in the payment
 41     */
 42     public void receivePayment(int dollars, int quarters, 
 43           int dimes, int nickels, int pennies)
 44     {
 45        payment = dollars + quarters * QUARTER_VALUE + dimes * DIME_VALUE
 46              + nickels * NICKEL_VALUE + pennies * PENNY_VALUE;
 47     }
 48     
 49     /**
 50        Computes the change due and resets the machine for the next customer.
 51        @return the change due to the customer
 52     */
 53     public double giveChange()
 54     {
 55        double change = payment - purchase;
 56        purchase = 0;
 57        payment = 0;
 58        return change;
 59     }
 60  }