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