Skip to content
18 changes: 18 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Exercise 1

### Class: `ShoppingBasket`

| Method | Member variables | Scenario | Result |
|--------------------------|---------------------|-------------------|-------------------------------------------------------------------------------|
| `double calculateCost()` | `List<Item> items` | Basket is empty | Return $0$ |
| | | Basket has items | Return total cost of the items ($>0$) |
| `void printReceipt()` | | Basket is empty | Print "empty" message |
| | | Basket has items | For every unique item, print name, price and quantity. Also print total cost. |

### Class: `Item`

| Method | Member variables | Scenario | Result |
|---------------------|------------------|----------|-------------------------------|
| `double getprice()` | `double price` | | Return the price of the item. |
| `String getName()` | `String name` | | Return the name of the item. |

25 changes: 25 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.booleanuk.core;

import java.util.HashMap;
import java.util.Map;

public class Basket {
Map<String, Integer> items = new HashMap<>();

public boolean add(String product, int price) {
if (this.items.containsKey(product)) {
return false;
}

this.items.put(product, price);
return true;
}

public int total() {
int total = 0;
for (int price : this.items.values()) {
total += price;
}
return total;
}
}
63 changes: 63 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;

public class BasketTest {
@Test
public void canAddNewItem() {
Basket basket = new Basket();
String product = "eggs";
int price = 20;
Assertions.assertTrue(basket.add(product, price));
}

@Test
public void cannotAddExistingItem() {
Basket basket = new Basket();
String product = "eggs";
int price = 20;
basket.add(product, price);
Assertions.assertFalse(basket.add(product, price));
}

@Test
public void emptyBasketShouldHaveZeroTotal() {
Basket basket = new Basket();
Assertions.assertEquals(0, basket.total());
}

@Test
public void correctCostOfSingleItem() {
Basket basket = new Basket();
String product = "eggs";
int price = 20;
basket.add(product, price);
Assertions.assertEquals(price, basket.total());
}

@Test
public void correctCostOfSeveralItems() {
Basket basket = new Basket();
String prod1 = "eggs";
String prod2 = "milk";
String prod3 = "flour";
int price1 = 20;
int price2 = 22;
int price3 = 17;
basket.add(prod1, price1);
basket.add(prod2, price2);
basket.add(prod3, price3);
Assertions.assertEquals(59, basket.total());
}

@Test
public void addingExistingItemShouldNotIncreaseTotal() {
Basket basket = new Basket();
String product = "eggs";
int price = 20;
basket.add(product, price);
basket.add(product, price);
Assertions.assertEquals(20, basket.total());
}
}
Loading