5
5
import org .junit .Before ;
6
6
import org .junit .Test ;
7
7
8
- public class VendingMachineTests {
9
-
8
+ public class VendingMachineTest {
9
+
10
+ private static final Double DIME = (Double ) 0.10 ;
11
+ private static final Double NICKEL = (Double ) 0.05 ;
12
+ private static final Double PENNY = (Double ) 0.01 ;
13
+ private static final Double NO_COIN = (Double ) 0.0 ;
14
+ private static final Double QUARTER_VALUE = (Double ) 0.25 ;
15
+
10
16
private VendingMachine vendingMachine ;
11
17
12
18
@ Before
@@ -16,53 +22,52 @@ public void setUp() {
16
22
17
23
@ Test
18
24
public void shouldAcceptQuarter () {
19
- vendingMachine .insertQuarter ();
25
+
26
+ vendingMachine .insert (Coin .QUARTER );
20
27
21
- assertEquals (( Double ) 0.25 , vendingMachine .getCurrentAmount ());
28
+ assertEquals (QUARTER_VALUE , vendingMachine .getCurrentAmount ());
22
29
}
23
30
24
31
@ Test
25
32
public void shouldResetCurrentAmountEqualToZeroWhenReturnCoinsIsPressed () {
26
- vendingMachine .setCurrentAmount (0.25 );
33
+
34
+ vendingMachine .setCurrentAmount (QUARTER_VALUE );
27
35
28
36
vendingMachine .returnCoins ();
29
37
30
- assertEquals (( Double ) 0.0 , vendingMachine .getCurrentAmount ());
38
+ assertEquals (NO_COIN , vendingMachine .getCurrentAmount ());
31
39
}
32
40
33
41
@ Test
34
42
public void shouldNotAcceptPenny () {
35
- vendingMachine .insertQuarter ();
36
-
37
- vendingMachine .insertPenny ();
43
+ vendingMachine .insert (Coin .PENNY );
38
44
39
- assertEquals ((Double ) 0.25 , vendingMachine .getCurrentAmount ());
40
- assertEquals ((Double ) 0.01 , vendingMachine .getCoinReturnAmount ());
45
+ assertEquals (PENNY , vendingMachine .getCoinReturnAmount ());
41
46
}
42
47
43
48
@ Test
44
49
public void shouldAcceptNickel () {
45
- vendingMachine .insertNickel ( );
50
+ vendingMachine .insert ( Coin . NICKEL );
46
51
47
- assertEquals (( Double ) 0.05 , vendingMachine .getCurrentAmount ());
52
+ assertEquals (NICKEL , vendingMachine .getCurrentAmount ());
48
53
}
49
54
50
55
@ Test
51
56
public void shouldAcceptDime () {
52
- vendingMachine .insertDime ( );
57
+ vendingMachine .insert ( Coin . DIME );
53
58
54
- assertEquals (( Double ) 0.10 , vendingMachine .getCurrentAmount ());
59
+ assertEquals (DIME , vendingMachine .getCurrentAmount ());
55
60
}
56
61
57
62
@ Test
58
63
public void pressingCoinReturnShouldReturnExactCoinsInserted () {
59
64
insertFiftyCentsInQuarters ();
60
- vendingMachine .insertNickel ( );
65
+ vendingMachine .insert ( Coin . NICKEL );
61
66
62
67
ArrayList <Double > expectedCoinList = new ArrayList <Double >();
63
- expectedCoinList .add (( Double ) 0.25 );
64
- expectedCoinList .add (( Double ) 0.25 );
65
- expectedCoinList .add (( Double ) 0.05 );
68
+ expectedCoinList .add (QUARTER_VALUE );
69
+ expectedCoinList .add (QUARTER_VALUE );
70
+ expectedCoinList .add (NICKEL );
66
71
67
72
assertEquals (expectedCoinList , vendingMachine .returnCoins ());
68
73
}
@@ -71,13 +76,13 @@ public void pressingCoinReturnShouldReturnExactCoinsInserted() {
71
76
public void returnCoinSlotShouldHoldAllCoinsReturned () {
72
77
vendingMachine .insertPenny ();
73
78
insertFiftyCentsInQuarters ();
74
- vendingMachine .insertNickel ( );
79
+ vendingMachine .insert ( Coin . NICKEL );
75
80
76
81
ArrayList <Double > expectedCoinList = new ArrayList <Double >();
77
- expectedCoinList .add (( Double ) 0.01 );
78
- expectedCoinList .add (( Double ) 0.25 );
79
- expectedCoinList .add (( Double ) 0.25 );
80
- expectedCoinList .add (( Double ) 0.05 );
82
+ expectedCoinList .add (PENNY );
83
+ expectedCoinList .add (QUARTER_VALUE );
84
+ expectedCoinList .add (QUARTER_VALUE );
85
+ expectedCoinList .add (NICKEL );
81
86
82
87
vendingMachine .returnCoins ();
83
88
@@ -88,17 +93,17 @@ public void returnCoinSlotShouldHoldAllCoinsReturned() {
88
93
public void currentAmountShouldContinueToSumAllChangePutInMachine () {
89
94
vendingMachine .insertPenny ();
90
95
insertFiftyCentsInQuarters ();
91
- vendingMachine .insertNickel ( );
96
+ vendingMachine .insert ( Coin . NICKEL );
92
97
93
98
assertEquals ((Double ) 0.55 , vendingMachine .getCurrentAmount ());
94
99
}
95
100
96
101
@ Test
97
102
public void displayShouldShowAmountOfCurrencyInsertedIntoMachine () {
98
103
insertDollarInQuarters ();
99
- vendingMachine .insertDime ( );
100
- vendingMachine .insertDime ( );
101
- vendingMachine .insertNickel ( );
104
+ vendingMachine .insert ( Coin . DIME );
105
+ vendingMachine .insert ( Coin . DIME );
106
+ vendingMachine .insert ( Coin . NICKEL );
102
107
103
108
assertEquals ("$1.25" , vendingMachine .getDisplay ());
104
109
}
@@ -164,9 +169,8 @@ public void itemBinShouldHoldCandyWhenCandyButtonIsPressed() {
164
169
165
170
@ Test
166
171
public void vendingMachineShouldNotVendCandyIfChangeIsInsufficent () {
167
- VendingMachine vendingMachine = new VendingMachine ();
168
- vendingMachine .insertQuarter ();
169
- vendingMachine .insertNickel ();
172
+ vendingMachine .insert (Coin .QUARTER );
173
+ vendingMachine .insert (Coin .NICKEL );
170
174
171
175
ArrayList <String > expectedItemBinList = new ArrayList <String >();
172
176
@@ -180,7 +184,7 @@ public void itemBinShouldHoldCandySodaChipsIfAllArePressed() {
180
184
insertDollarInQuarters ();
181
185
insertDollarInQuarters ();
182
186
insertDollarInQuarters ();
183
- vendingMachine .insertQuarter ( );
187
+ vendingMachine .insert ( Coin . QUARTER );
184
188
185
189
ArrayList <String > expectedItemBinList = new ArrayList <String >();
186
190
expectedItemBinList .add ("Candy" );
@@ -202,7 +206,7 @@ public void machineShouldMakeChangeIfTooMuchMoneyPaidForSoda() {
202
206
vendingMachine .sodaButton ();
203
207
vendingMachine .returnCoins ();
204
208
205
- assertEquals (( Double ) 0.25 , vendingMachine .getCoinReturnAmount ());
209
+ assertEquals (QUARTER_VALUE , vendingMachine .getCoinReturnAmount ());
206
210
}
207
211
208
212
private void insertDollarInQuarters () {
@@ -211,7 +215,7 @@ private void insertDollarInQuarters() {
211
215
}
212
216
213
217
private void insertFiftyCentsInQuarters () {
214
- vendingMachine .insertQuarter ( );
215
- vendingMachine .insertQuarter ( );
218
+ vendingMachine .insert ( Coin . QUARTER );
219
+ vendingMachine .insert ( Coin . QUARTER );
216
220
}
217
221
}
0 commit comments