1 |
/** |
2 |
A purse computes the total value of a collection of coins. |
3 |
*/ |
4 |
public class Purse |
5 |
{ |
6 |
/** |
7 |
Constructs an empty purse. |
8 |
*/ |
9 |
public Purse() |
10 |
{ |
11 |
nickels = 0; |
12 |
dimes = 0; |
13 |
quarters = 0; |
14 |
} |
15 |
|
16 |
/** |
17 |
Add nickels to the purse. |
18 |
@param count the number of nickels to add |
19 |
*/ |
20 |
public void addNickels(int count) |
21 |
{ |
22 |
nickels = nickels + count; |
23 |
} |
24 |
|
25 |
/** |
26 |
Add dimes to the purse. |
27 |
@param count the number of dimes to add |
28 |
*/ |
29 |
public void addDimes(int count) |
30 |
{ |
31 |
dimes = dimes + count; |
32 |
} |
33 |
|
34 |
/** |
35 |
Add quarters to the purse. |
36 |
@param count the number of quarters to add |
37 |
*/ |
38 |
public void addQuarters(int count) |
39 |
{ |
40 |
quarters = quarters + count; |
41 |
} |
42 |
|
43 |
/** |
44 |
Get the total value of the coins in the purse. |
45 |
@return the sum of all coin values |
46 |
*/ |
47 |
public double getTotal() |
48 |
{ |
49 |
return nickels * NICKEL_VALUE |
50 |
+ dimes * DIME_VALUE + quarters * QUARTER_VALUE; |
51 |
} |
52 |
|
53 |
private static final double NICKEL_VALUE = 0.05; |
54 |
private static final double DIME_VALUE = 0.1; |
55 |
private static final double QUARTER_VALUE = 0.25; |
56 |
|
57 |
private int nickels; |
58 |
private int dimes; |
59 |
private int quarters; |
60 |
} |
61 |
|