Skip to content

Commit 562ff0d

Browse files
committed
initial commit of vending machine kata in java, needs refactoring
0 parents  commit 562ff0d

File tree

4 files changed

+350
-0
lines changed

4 files changed

+350
-0
lines changed

bin/vending/VendingMachine.class

3.93 KB
Binary file not shown.

bin/vending/VendingMachineTests.class

4.86 KB
Binary file not shown.

src/vending/VendingMachine.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package vending;
2+
3+
import java.text.NumberFormat;
4+
import java.util.ArrayList;
5+
6+
public class VendingMachine {
7+
8+
Double currentAmount = 0.0;
9+
Double coinReturnAmount = 0.0;
10+
ArrayList<Double> coinsList = new ArrayList<Double>();
11+
ArrayList<Double> returnSlotCoins = new ArrayList<Double>();
12+
String display = "";
13+
ArrayList<String> itemBinList = new ArrayList<String>();
14+
15+
16+
public ArrayList<Double> returnCoins() {
17+
setCoinReturnAmount(getCurrentAmount());
18+
setCurrentAmount(0.00);
19+
20+
returnSlotCoins.addAll(coinsList);
21+
return coinsList;
22+
}
23+
24+
public void insertPenny() {
25+
setCoinReturnAmount(0.01);
26+
27+
returnSlotCoins.add((Double)0.01);
28+
}
29+
30+
public void insertNickel() {
31+
setCurrentAmount(getCurrentAmount() + 0.05);
32+
updateDisplay();
33+
34+
coinsList.add((Double)0.05);
35+
}
36+
37+
public void insertDime() {
38+
setCurrentAmount(getCurrentAmount() + 0.10);
39+
updateDisplay();
40+
41+
coinsList.add((Double)0.10);
42+
}
43+
44+
public void insertQuarter() {
45+
setCurrentAmount(getCurrentAmount() + 0.25);
46+
updateDisplay();
47+
48+
coinsList.add((Double)0.25);
49+
}
50+
51+
public void sodaButton() {
52+
if (getCurrentAmount() >= 1.25){
53+
updateChangeAmount();
54+
itemBinList.add("Soda");
55+
}
56+
}
57+
58+
59+
public void chipsButton() {
60+
if (getCurrentAmount() >= 0.75){
61+
updateChangeAmount();
62+
itemBinList.add("Chips");
63+
}
64+
}
65+
66+
public void candyButton() {
67+
if (getCurrentAmount() >= 0.50){
68+
updateChangeAmount();
69+
itemBinList.add("Candy");
70+
}
71+
}
72+
73+
// Helper Methods
74+
private void updateDisplay() {
75+
NumberFormat nf = NumberFormat.getInstance();
76+
nf.setMinimumFractionDigits(2);
77+
String stringConversion = nf.format(getCurrentAmount());
78+
setDisplay("$" + stringConversion);
79+
}
80+
private void updateChangeAmount() {
81+
if (getCurrentAmount() >= 1.25) {
82+
setCurrentAmount(getCurrentAmount() - 1.25);
83+
}
84+
}
85+
86+
//Getters / Setters
87+
88+
public Double getCoinReturnAmount() {
89+
return coinReturnAmount;
90+
}
91+
92+
public void setCoinReturnAmount(Double coinReturnAmount) {
93+
this.coinReturnAmount = coinReturnAmount;
94+
}
95+
96+
public Double getCurrentAmount() {
97+
return currentAmount;
98+
}
99+
100+
public void setCurrentAmount(Double currentAmount) {
101+
this.currentAmount = currentAmount;
102+
}
103+
104+
public ArrayList<Double> getReturnSlotCoins() {
105+
return returnSlotCoins;
106+
}
107+
108+
public void setReturnSlotCoins(ArrayList<Double> returnSlotCoins) {
109+
this.returnSlotCoins = returnSlotCoins;
110+
}
111+
112+
public String getDisplay() {
113+
return display;
114+
}
115+
116+
public void setDisplay(String display) {
117+
this.display = display;
118+
}
119+
120+
public ArrayList<String> getItemBinList() {
121+
return itemBinList;
122+
}
123+
124+
public void setItemBinList(ArrayList<String> itemBinList) {
125+
this.itemBinList = itemBinList;
126+
}
127+
128+
129+
130+
}

test/vending/VendingMachineTests.java

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package vending;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
7+
import org.junit.Before;
8+
import org.junit.Ignore;
9+
import org.junit.Test;
10+
11+
public class VendingMachineTests {
12+
13+
private VendingMachine vendingMachine;
14+
15+
@Before
16+
public void setUp() {
17+
vendingMachine = new VendingMachine();
18+
}
19+
20+
@Test
21+
public void shouldAcceptQuarter() {
22+
vendingMachine.insertQuarter();
23+
24+
assertEquals((Double) 0.25, vendingMachine.getCurrentAmount());
25+
}
26+
27+
@Test
28+
public void shouldResetCurrentAmountEqualToZeroWhenReturnCoinsIsPressed() {
29+
vendingMachine.setCurrentAmount(0.25);
30+
31+
vendingMachine.returnCoins();
32+
33+
assertEquals((Double) 0.0, vendingMachine.getCurrentAmount());
34+
}
35+
36+
@Test
37+
public void shouldNotAcceptPenny() {
38+
vendingMachine.insertQuarter();
39+
40+
vendingMachine.insertPenny();
41+
42+
assertEquals((Double) 0.25, vendingMachine.getCurrentAmount());
43+
assertEquals((Double) 0.01, vendingMachine.getCoinReturnAmount());
44+
}
45+
46+
@Test
47+
public void shouldAcceptNickel() {
48+
vendingMachine.insertNickel();
49+
50+
assertEquals((Double) 0.05, vendingMachine.getCurrentAmount());
51+
}
52+
53+
@Test
54+
public void shouldAcceptDime() {
55+
vendingMachine.insertDime();
56+
57+
assertEquals((Double) 0.10, vendingMachine.getCurrentAmount());
58+
}
59+
60+
@Test
61+
public void pressingCoinReturnShouldReturnExactCoinsInserted() {
62+
insertFiftyCentsInQuarters();
63+
vendingMachine.insertNickel();
64+
65+
ArrayList<Double> expectedCoinList = new ArrayList<Double>();
66+
expectedCoinList.add((Double) 0.25);
67+
expectedCoinList.add((Double) 0.25);
68+
expectedCoinList.add((Double) 0.05);
69+
70+
assertEquals(expectedCoinList, vendingMachine.returnCoins());
71+
}
72+
73+
@Test
74+
public void returnCoinSlotShouldHoldAllCoinsReturned() {
75+
vendingMachine.insertPenny();
76+
insertFiftyCentsInQuarters();
77+
vendingMachine.insertNickel();
78+
79+
ArrayList<Double> expectedCoinList = new ArrayList<Double>();
80+
expectedCoinList.add((Double) 0.01);
81+
expectedCoinList.add((Double) 0.25);
82+
expectedCoinList.add((Double) 0.25);
83+
expectedCoinList.add((Double) 0.05);
84+
85+
vendingMachine.returnCoins();
86+
87+
assertEquals(expectedCoinList, vendingMachine.getReturnSlotCoins());
88+
}
89+
90+
@Test
91+
public void currentAmountShouldContinueToSumAllChangePutInMachine() {
92+
vendingMachine.insertPenny();
93+
insertFiftyCentsInQuarters();
94+
vendingMachine.insertNickel();
95+
96+
assertEquals((Double) 0.55, vendingMachine.getCurrentAmount());
97+
}
98+
99+
@Test
100+
public void displayShouldShowAmountOfCurrencyInsertedIntoMachine() {
101+
insertDollarInQuarters();
102+
vendingMachine.insertDime();
103+
vendingMachine.insertDime();
104+
vendingMachine.insertNickel();
105+
106+
assertEquals("$1.25", vendingMachine.getDisplay());
107+
}
108+
109+
@Test
110+
public void itemBinShouldHoldSodaWhenSodaButtonIsPressed() {
111+
insertDollarInQuarters();
112+
insertFiftyCentsInQuarters();
113+
114+
ArrayList<String> expectedItemBinList = new ArrayList<String>();
115+
expectedItemBinList.add("Soda");
116+
117+
vendingMachine.sodaButton();
118+
119+
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
120+
}
121+
122+
@Test
123+
public void vendingMachineShouldNotVendSodaIfChangeIsInsufficent() {
124+
insertDollarInQuarters();
125+
126+
ArrayList<String> expectedItemBinList = new ArrayList<String>();
127+
128+
vendingMachine.sodaButton();
129+
130+
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
131+
}
132+
133+
@Test
134+
public void itemBinShouldHoldChipsWhenChipsButtonIsPressed() {
135+
insertDollarInQuarters();
136+
137+
ArrayList<String> expectedItemBinList = new ArrayList<String>();
138+
expectedItemBinList.add("Chips");
139+
140+
vendingMachine.chipsButton();
141+
142+
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
143+
}
144+
145+
@Test
146+
public void vendingMachineShouldNotVendChipsIfChangeIsInsufficent() {
147+
insertFiftyCentsInQuarters();
148+
149+
ArrayList<String> expectedItemBinList = new ArrayList<String>();
150+
151+
vendingMachine.chipsButton();
152+
153+
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
154+
}
155+
156+
@Test
157+
public void itemBinShouldHoldCandyWhenCandyButtonIsPressed() {
158+
insertFiftyCentsInQuarters();
159+
160+
ArrayList<String> expectedItemBinList = new ArrayList<String>();
161+
expectedItemBinList.add("Candy");
162+
163+
vendingMachine.candyButton();
164+
165+
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
166+
}
167+
168+
@Test
169+
public void vendingMachineShouldNotVendCandyIfChangeIsInsufficent() {
170+
VendingMachine vendingMachine = new VendingMachine();
171+
vendingMachine.insertQuarter();
172+
vendingMachine.insertNickel();
173+
174+
ArrayList<String> expectedItemBinList = new ArrayList<String>();
175+
176+
vendingMachine.candyButton();
177+
178+
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
179+
}
180+
181+
@Test
182+
public void itemBinShouldHoldCandySodaChipsIfAllArePressed() {
183+
insertDollarInQuarters();
184+
insertDollarInQuarters();
185+
insertDollarInQuarters();
186+
vendingMachine.insertQuarter();
187+
188+
ArrayList<String> expectedItemBinList = new ArrayList<String>();
189+
expectedItemBinList.add("Candy");
190+
expectedItemBinList.add("Soda");
191+
expectedItemBinList.add("Chips");
192+
193+
vendingMachine.candyButton();
194+
vendingMachine.sodaButton();
195+
vendingMachine.chipsButton();
196+
197+
assertEquals(expectedItemBinList, vendingMachine.getItemBinList());
198+
}
199+
200+
@Test
201+
public void machineShouldMakeChangeIfTooMuchMoneyPaidForSoda() {
202+
insertDollarInQuarters();
203+
insertFiftyCentsInQuarters();
204+
205+
vendingMachine.sodaButton();
206+
vendingMachine.returnCoins();
207+
208+
assertEquals((Double) 0.25, vendingMachine.getCoinReturnAmount());
209+
}
210+
211+
private void insertDollarInQuarters() {
212+
insertFiftyCentsInQuarters();
213+
insertFiftyCentsInQuarters();
214+
}
215+
216+
private void insertFiftyCentsInQuarters() {
217+
vendingMachine.insertQuarter();
218+
vendingMachine.insertQuarter();
219+
}
220+
}

0 commit comments

Comments
 (0)