-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVendingMachineStepDefinition.java
More file actions
109 lines (82 loc) · 3.64 KB
/
Copy pathVendingMachineStepDefinition.java
File metadata and controls
109 lines (82 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package resources;
import cucumber.api.java.After;
import cucumber.api.java.en.*;
import drewclasses.*;
import static org.junit.Assert.*;
public class VendingMachineStepDefinition {
VendingMachine vendingMachine;
@After()
public void tearDown(){
vendingMachine = null;
}
@Given("^I want to use a vending machine$")
public void iWantToUseAVendingMachine(){
vendingMachine = new VendingMachine();
}
@Given("^I want to use a vending machine that only accepts exact change$")
public void iWantToUseAVendingMachineThatOnlyAcceptsExactChange(){
vendingMachine = new VendingMachine(50, 50, 50, 0, "EXACT CHANGE ONLY");
}
@Given("^I want to use a vending machine that is sold out of (\\w+)$")
public void iWantToUseAVendingMachineThatIsSoldOutOfAProduct(String product){
if(product.equalsIgnoreCase("cola"))
vendingMachine = new VendingMachine(0, 50, 50, 25, "INSERT COIN");
else if(product.equalsIgnoreCase("chips"))
vendingMachine = new VendingMachine(50, 0, 50, 25, "INSERT COIN");
else if(product.equalsIgnoreCase("candy"))
vendingMachine = new VendingMachine(50, 50, 0, 25, "INSERT COIN");
}
@When("^I insert a (\\w+) into the coin slot$")
public void insertCoinInSlot(String coin){
if(coin.equalsIgnoreCase("quarter"))
vendingMachine.insertCoin(new Coin("6g", "24mm"));
else if(coin.equalsIgnoreCase("dime"))
vendingMachine.insertCoin(new Coin("2g", "18mm"));
else if(coin.equalsIgnoreCase("nickel"))
vendingMachine.insertCoin(new Coin("5g", "21mm"));
else
vendingMachine.insertCoin(new Coin("3g", "19mm"));
}
@When("^there are no coins inserted into the vending machine$")
public void noCoinsInVendingMachine(){
double insertedIntoMachine = vendingMachine.getAmountInsertedIntoMachine();
assertTrue("Actual: " + insertedIntoMachine, insertedIntoMachine == 0);
}
@When("^I insert \\$(\\d{0,2}.\\d{2}) into the machine$")
public void insertMoneyIntoTheMachine(String amount){
double amountInsertedMachine = Double.parseDouble(amount);
double amountCurrenlyInMachine = vendingMachine.getAmountInsertedIntoMachine();
while(amountInsertedMachine != amountCurrenlyInMachine){
if((amountInsertedMachine-amountCurrenlyInMachine) >= 0.25)
vendingMachine.insertCoin(new Coin("6g", "24mm"));
else if((amountInsertedMachine-amountCurrenlyInMachine) >= 0.10)
vendingMachine.insertCoin(new Coin("2g", "18mm"));
else if((amountInsertedMachine-amountCurrenlyInMachine) >= 0.05)
vendingMachine.insertCoin(new Coin("5g", "21mm"));
amountCurrenlyInMachine = vendingMachine.getAmountInsertedIntoMachine();
}
}
@Then("^all coins are returned in the change slot$")
public void allCoinsReturnedInTheChangeSlot(){
double insertedIntoMachine = vendingMachine.getAmountInsertedIntoMachine();
assertTrue("Actual: " + insertedIntoMachine, insertedIntoMachine == 0);
}
@Then("^\\$(\\d{0,2}.\\d{2}) is returned in the change slot$")
public void allRemainingAmountIsReturnedInChangeSlot(String amount){
double returnedCoins = vendingMachine.getChangeAmountFromReturnedCoins();
assertTrue("Actual: "+ returnedCoins, returnedCoins == Double.parseDouble(amount));
}
@And("^the vending machine display shows (.*)$")
public void vendingMachineDisplay(String displayValue){
String returnedDispayValue = vendingMachine.getDisplayValue();
assertTrue("Actual: "+ returnedDispayValue, displayValue.equals(returnedDispayValue));
}
@And("^I select the (\\w+) option$")
public void selectingAProductOption(String product){
vendingMachine.selectProduct(product);
}
@And("^I choose the return coins option$")
public void returnCoinsOption(){
vendingMachine.returnAllRemainingCoins();
}
}