1 /**
2 A cash register totals up sales and computes change due.
3 */
4 public class CashRegister
5 {
6 private double taxRate;
7 private double purchase;
8 private double taxablePurchase;
9 private double payment;
10
11 /**
12 Constructs a cash register with no money in it.
13 @param rate the tax rate for taxable purchases
14 */
15 public CashRegister(double rate)
16 {
17 taxRate = rate;
18 purchase = 0;
19 payment = 0;
20 }
21
22 /**
23 Records the sale of a tax-free item.
24 @param amount the price of the item
25 */
26 public void recordPurchase(double amount)
27 {
28 purchase = purchase + amount;
29 }
30
31 /**
32 Records the sale of a taxable item.
33 @param amount the price of the item
34 */
35 public void recordTaxablePurchase(double amount)
36 {
37 taxablePurchase = taxablePurchase + amount;
38 }
39
40 /**
41 Processes a payment received from the customer.
42 @param amount the amount of the payment
43 */
44 public void receivePayment(double amount)
45 {
46 payment = payment + amount;
47 }
48
49 /**
50 Processes the sales tax due.
51 @return the sales tax due
52 */
53 public double getSalesTax()
54 {
55 return taxablePurchase * taxRate / 100;
56 }
57
58 /**
59 Computes the change due and resets the machine for the next customer.
60 @return the change due to the customer
61 */
62 public double giveChange()
63 {
64 double change = payment - purchase - get;
65 purchase = 0;
66 payment = 0;
67 return change;
68 }
69 }