Skip to content

Commit 14d5baa

Browse files
committed
refatoring to push out moneyHandler, dependency injection - Currently in RED state
1 parent b55b364 commit 14d5baa

File tree

2 files changed

+90
-54
lines changed

2 files changed

+90
-54
lines changed

src/vending/VendingMachine.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ public class VendingMachine {
99
ArrayList<Coin> returnSlotCoins = new ArrayList<Coin>();
1010
String display = "";
1111
ArrayList<String> itemBinList = new ArrayList<String>();
12+
CurrencyHandler currencyHandler;
13+
14+
public VendingMachine(CurrencyHandler currencyHandler) {
15+
this.currencyHandler = currencyHandler;
16+
}
1217

1318
public ArrayList<Coin> returnCoins() {
1419
setCoinReturnAmount(getCurrentAmount());
@@ -70,11 +75,11 @@ public void setCoinReturnAmount(Double coinReturnAmount) {
7075
}
7176

7277
public Double getCurrentAmount() {
73-
return currentAmount;
78+
return currencyHandler.getCurrentAmount();
7479
}
7580

7681
public void setCurrentAmount(Double currentAmount) {
77-
this.currentAmount = currentAmount;
82+
currencyHandler.setCurrentAmount(currentAmount);
7883
}
7984

8085
public ArrayList<Coin> getReturnSlotCoins() {
@@ -102,6 +107,7 @@ public void setItemBinList(ArrayList<String> itemBinList) {
102107
}
103108

104109
public void insert(Coin coin) {
110+
this.currencyHandler.insert(coin);
105111
if (isValidCoin(coin)) {
106112
setCurrentAmount(getCurrentAmount() + coin.getValue());
107113
coinsList.add(coin);

test/vending/VendingMachineTests.java

Lines changed: 82 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -11,106 +11,108 @@ public class VendingMachineTest {
1111

1212
private static final Double NO_COIN = (Double) 0.0;
1313

14-
private VendingMachine vendingMachine;
14+
private VendingMachine underTest;
1515

16+
private CurrencyHandlerStub currencyHandlerStub;
1617
@Before
1718
public void setUp() {
18-
vendingMachine = new VendingMachine();
19+
currencyHandlerStub = new CurrencyHandlerStub();
20+
underTest = new VendingMachine(currencyHandlerStub);
1921
}
2022

2123
@Test
2224
public void shouldAcceptQuarter() {
2325

24-
vendingMachine.insert(Coin.QUARTER);
26+
underTest.insert(Coin.QUARTER);
2527

26-
assertEquals(Coin.QUARTER.getValue(), vendingMachine.getCurrentAmount());
28+
assertEquals(Coin.QUARTER.getValue(), underTest.getCurrentAmount());
2729
}
2830

2931
@Test
3032
public void shouldResetCurrentAmountEqualToZeroWhenReturnCoinsIsPressed() {
3133

32-
vendingMachine.setCurrentAmount(Coin.QUARTER.getValue());
34+
underTest.setCurrentAmount(Coin.QUARTER.getValue());
3335

34-
vendingMachine.returnCoins();
36+
underTest.returnCoins();
3537

36-
assertEquals(NO_COIN, vendingMachine.getCurrentAmount());
38+
assertEquals(NO_COIN, underTest.getCurrentAmount());
3739
}
3840

3941
@Test
4042
public void pennyShouldGoToCoinReturn() {
41-
vendingMachine.insert(Coin.PENNY);
43+
underTest.insert(Coin.PENNY);
4244

43-
assertEquals(Coin.PENNY.getValue(), vendingMachine.getCoinReturnAmount());
45+
assertEquals(Coin.PENNY.getValue(), underTest.getCoinReturnAmount());
4446
}
4547

4648
@Test
4749
public void shouldNotAddPennyToCurrentValue() {
48-
vendingMachine.insert(Coin.PENNY);
50+
underTest.insert(Coin.PENNY);
4951

50-
assertEquals(NO_COIN, vendingMachine.getCurrentAmount());
52+
assertEquals(NO_COIN, underTest.getCurrentAmount());
5153
}
5254

5355
@Test
5456
public void shouldAcceptNickel() {
55-
vendingMachine.insert(Coin.NICKEL);
57+
underTest.insert(Coin.NICKEL);
5658

57-
assertEquals(Coin.NICKEL.getValue(), vendingMachine.getCurrentAmount());
59+
assertEquals(Coin.NICKEL.getValue(), underTest.getCurrentAmount());
5860
}
5961

6062
@Test
6163
public void shouldAcceptDime() {
62-
vendingMachine.insert(Coin.DIME);
64+
underTest.insert(Coin.DIME);
6365

64-
assertEquals(Coin.DIME.getValue(), vendingMachine.getCurrentAmount());
66+
assertEquals(Coin.DIME.getValue(), underTest.getCurrentAmount());
6567
}
6668

6769
@Test
6870
public void pressingCoinReturnShouldReturnExactCoinsInserted() {
6971
insertFiftyCentsInQuarters();
70-
vendingMachine.insert(Coin.NICKEL);
72+
underTest.insert(Coin.NICKEL);
7173

7274
List<Coin> expectedCoinList = new ArrayList<Coin>();
7375
expectedCoinList.add(Coin.QUARTER);
7476
expectedCoinList.add(Coin.QUARTER);
7577
expectedCoinList.add(Coin.NICKEL);
7678

77-
assertEquals(expectedCoinList, vendingMachine.returnCoins());
79+
assertEquals(expectedCoinList, underTest.returnCoins());
7880
}
7981

8082
@Test
8183
public void returnCoinSlotShouldHoldAllCoinsReturned() {
82-
vendingMachine.insertPenny();
84+
underTest.insertPenny();
8385
insertFiftyCentsInQuarters();
84-
vendingMachine.insert(Coin.NICKEL);
86+
underTest.insert(Coin.NICKEL);
8587

8688
List<Coin> expectedCoinList = new ArrayList<Coin>();
8789
expectedCoinList.add(Coin.PENNY);
8890
expectedCoinList.add(Coin.QUARTER);
8991
expectedCoinList.add(Coin.QUARTER);
9092
expectedCoinList.add(Coin.NICKEL);
9193

92-
vendingMachine.returnCoins();
94+
underTest.returnCoins();
9395

94-
assertEquals(expectedCoinList, vendingMachine.getReturnSlotCoins());
96+
assertEquals(expectedCoinList, underTest.getReturnSlotCoins());
9597
}
9698

9799
@Test
98100
public void currentAmountShouldContinueToSumAllChangePutInMachine() {
99-
vendingMachine.insertPenny();
101+
underTest.insertPenny();
100102
insertFiftyCentsInQuarters();
101-
vendingMachine.insert(Coin.NICKEL);
103+
underTest.insert(Coin.NICKEL);
102104

103-
assertEquals((Double) 0.55, vendingMachine.getCurrentAmount());
105+
assertEquals((Double) 0.55, underTest.getCurrentAmount());
104106
}
105107

106108
@Test
107109
public void displayShouldShowAmountOfCurrencyInsertedIntoMachine() {
108110
insertDollarInQuarters();
109-
vendingMachine.insert(Coin.DIME);
110-
vendingMachine.insert(Coin.DIME);
111-
vendingMachine.insert(Coin.NICKEL);
111+
underTest.insert(Coin.DIME);
112+
underTest.insert(Coin.DIME);
113+
underTest.insert(Coin.NICKEL);
112114

113-
assertEquals("$1.25", vendingMachine.getDisplay());
115+
assertEquals("$1.25", underTest.getDisplay());
114116
}
115117

116118
@Test
@@ -121,9 +123,9 @@ public void itemBinShouldHoldSodaWhenSodaButtonIsPressed() {
121123
ArrayList<String> expectedItemBinList = new ArrayList<String>();
122124
expectedItemBinList.add("Soda");
123125

124-
vendingMachine.sodaButton();
126+
underTest.sodaButton();
125127

126-
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
128+
assertEquals(expectedItemBinList, underTest.getItemBinList());
127129
}
128130

129131
@Test
@@ -132,9 +134,9 @@ public void vendingMachineShouldNotVendSodaIfChangeIsInsufficent() {
132134

133135
ArrayList<String> expectedItemBinList = new ArrayList<String>();
134136

135-
vendingMachine.sodaButton();
137+
underTest.sodaButton();
136138

137-
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
139+
assertEquals(expectedItemBinList, underTest.getItemBinList());
138140
}
139141

140142
@Test
@@ -144,9 +146,9 @@ public void itemBinShouldHoldChipsWhenChipsButtonIsPressed() {
144146
ArrayList<String> expectedItemBinList = new ArrayList<String>();
145147
expectedItemBinList.add("Chips");
146148

147-
vendingMachine.chipsButton();
149+
underTest.chipsButton();
148150

149-
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
151+
assertEquals(expectedItemBinList, underTest.getItemBinList());
150152
}
151153

152154
@Test
@@ -155,9 +157,9 @@ public void vendingMachineShouldNotVendChipsIfChangeIsInsufficent() {
155157

156158
ArrayList<String> expectedItemBinList = new ArrayList<String>();
157159

158-
vendingMachine.chipsButton();
160+
underTest.chipsButton();
159161

160-
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
162+
assertEquals(expectedItemBinList, underTest.getItemBinList());
161163
}
162164

163165
@Test
@@ -167,51 +169,51 @@ public void itemBinShouldHoldCandyWhenCandyButtonIsPressed() {
167169
ArrayList<String> expectedItemBinList = new ArrayList<String>();
168170
expectedItemBinList.add("Candy");
169171

170-
vendingMachine.candyButton();
172+
underTest.candyButton();
171173

172-
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
174+
assertEquals(expectedItemBinList, underTest.getItemBinList());
173175
}
174176

175177
@Test
176178
public void vendingMachineShouldNotVendCandyIfChangeIsInsufficent() {
177-
vendingMachine.insert(Coin.QUARTER);
178-
vendingMachine.insert(Coin.NICKEL);
179+
underTest.insert(Coin.QUARTER);
180+
underTest.insert(Coin.NICKEL);
179181

180182
ArrayList<String> expectedItemBinList = new ArrayList<String>();
181183

182-
vendingMachine.candyButton();
184+
underTest.candyButton();
183185

184-
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
186+
assertEquals(expectedItemBinList, underTest.getItemBinList());
185187
}
186188

187189
@Test
188190
public void itemBinShouldHoldCandySodaChipsIfAllArePressed() {
189191
insertDollarInQuarters();
190192
insertDollarInQuarters();
191193
insertDollarInQuarters();
192-
vendingMachine.insert(Coin.QUARTER);
194+
underTest.insert(Coin.QUARTER);
193195

194196
ArrayList<String> expectedItemBinList = new ArrayList<String>();
195197
expectedItemBinList.add("Candy");
196198
expectedItemBinList.add("Soda");
197199
expectedItemBinList.add("Chips");
198200

199-
vendingMachine.candyButton();
200-
vendingMachine.sodaButton();
201-
vendingMachine.chipsButton();
201+
underTest.candyButton();
202+
underTest.sodaButton();
203+
underTest.chipsButton();
202204

203-
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
205+
assertEquals(expectedItemBinList, underTest.getItemBinList());
204206
}
205207

206208
@Test
207209
public void machineShouldMakeChangeIfTooMuchMoneyPaidForSoda() {
208210
insertDollarInQuarters();
209211
insertFiftyCentsInQuarters();
210212

211-
vendingMachine.sodaButton();
212-
vendingMachine.returnCoins();
213+
underTest.sodaButton();
214+
underTest.returnCoins();
213215

214-
assertEquals(Coin.QUARTER.getValue(), vendingMachine.getCoinReturnAmount());
216+
assertEquals(Coin.QUARTER.getValue(), underTest.getCoinReturnAmount());
215217
}
216218

217219
private void insertDollarInQuarters() {
@@ -220,7 +222,35 @@ private void insertDollarInQuarters() {
220222
}
221223

222224
private void insertFiftyCentsInQuarters() {
223-
vendingMachine.insert(Coin.QUARTER);
224-
vendingMachine.insert(Coin.QUARTER);
225+
underTest.insert(Coin.QUARTER);
226+
underTest.insert(Coin.QUARTER);
227+
}
228+
229+
@Test
230+
public void shouldCallCurrencyHandlerWhenMoneyIsInserted(){
231+
underTest.insert(Coin.QUARTER);
232+
assertEquals(Coin.QUARTER, currencyHandlerStub.getCoin());
233+
}
234+
235+
public class CurrencyHandlerStub implements CurrencyHandler{
236+
Coin coin;
237+
Double currentAmount = 0.0;
238+
public Coin getCoin() {
239+
return coin;
240+
}
241+
@Override
242+
public void insert(Coin coin){
243+
this.coin=coin;
244+
this.currentAmount = getCurrentAmount() + coin.getValue();
245+
}
246+
@Override
247+
public Double getCurrentAmount() {
248+
return this.currentAmount;
249+
}
250+
@Override
251+
public void setCurrentAmount(Double currentAmount) {
252+
this.currentAmount = currentAmount;
253+
}
254+
225255
}
226256
}

0 commit comments

Comments
 (0)