Skip to content

Commit f4c7180

Browse files
committed
Use tables
1 parent a38a726 commit f4c7180

File tree

3 files changed

+17
-36
lines changed

3 files changed

+17
-36
lines changed

src/main/java/skeleton/Checkout.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,15 @@
44
import java.util.Map;
55

66
public class Checkout {
7-
private final Map<String,Integer> productPrices = new HashMap<String, Integer>();
7+
private Map<String,Integer> productPrices = new HashMap<String, Integer>();
88
private int total = 0;
99

10-
public void setPriceOfEspresso(int priceOfEspresso) {
11-
productPrices.put("espresso", priceOfEspresso);
10+
public void setProducts(Map<String, Integer> products) {
11+
this.productPrices = products;
1212
}
1313

14-
public void setPriceOfCroissant(int priceOfCroissant) {
15-
productPrices.put("croissant", priceOfCroissant);
16-
}
17-
18-
public void setQuantityOfEspresso(int quantityOfEspresso) {
19-
total += quantityOfEspresso * productPrices.get("espresso");
20-
}
21-
22-
public void setQuantityOfCroissant(int quantityOfCroissant) {
23-
total += quantityOfCroissant * productPrices.get("croissant");
14+
public void setQuantity(int quantityOfCroissant, String product) {
15+
total += quantityOfCroissant * productPrices.get(product);
2416
}
2517

2618
public int getTotal() {

src/test/java/skeleton/Stepdefs.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
package skeleton;
22

3-
import cucumber.api.PendingException;
43
import cucumber.api.java.en.Given;
54
import cucumber.api.java.en.Then;
65
import cucumber.api.java.en.When;
76

7+
import java.util.Map;
8+
89
import static org.junit.Assert.assertEquals;
910

1011
public class Stepdefs {
1112
Checkout checkout = new Checkout();
1213

13-
@Given("^an espresso costs £(\\d+)$")
14-
public void an_espresso_costs_£(int price) throws Throwable {
15-
checkout.setPriceOfEspresso(price);
16-
}
17-
18-
@Given("^a croissant costs £(\\d+)$")
19-
public void a_croissant_costs_£(int price) throws Throwable {
20-
checkout.setPriceOfCroissant(price);
21-
}
22-
23-
@When("^I sell (\\d+) espresso$")
24-
public void i_sell_espresso(int quantity) throws Throwable {
25-
checkout.setQuantityOfEspresso(quantity);
14+
@Given("^the following products:$")
15+
public void the_following_products(Map<String,Integer> products) throws Throwable {
16+
checkout.setProducts(products);
2617
}
2718

28-
@When("^I sell (\\d+) croissants$")
29-
public void i_sell_croissants(int quantity) throws Throwable {
30-
checkout.setQuantityOfCroissant(quantity);
19+
@When("^I sell (\\d+) (.*)?$")
20+
public void i_sell_espresso(int quantity, String product) throws Throwable {
21+
checkout.setQuantity(quantity, product);
3122
}
3223

3324
@Then("^the total should be £(\\d+)$")
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
Feature: Checkout
22

33
Background:
4-
Given an espresso costs £2
5-
And a croissant costs £1
6-
# Given the following products:
7-
# | espresso | 2 |
8-
# | croissant | 1 |
4+
Given the following products:
5+
| espresso | 2 |
6+
| croissant | 1 |
97

108
Scenario: Sell only espresso
119
When I sell 3 espresso
1210
Then the total should be £6
1311

1412
Scenario: Sell espresso and croissants
1513
When I sell 2 espresso
16-
And I sell 3 croissants
14+
And I sell 3 croissant
1715
Then the total should be £7
1816

1917
#Scenario: Espresso costs 1.80

0 commit comments

Comments
 (0)