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 purchase = purchase + amount;
25 }
26
27 /**
28 Processes a payment received from the customer.
29 @param amount the amount of the payment
30 */
31 public void receivePayment(double amount)
32 {
33 payment = payment + amount;
34 }
35
36 /**
37 Computes the change due and resets the machine for the next customer.
38 @return the change due to the customer
39 */
40 public double giveChange()
41 {
42 double change = payment - purchase;
43 purchase = 0;
44 payment = 0;
45 return change;
46 }
47 }