diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..35c520d --- /dev/null +++ b/domain-model.md @@ -0,0 +1,18 @@ +# Exercise 1 + +### Class: `ShoppingBasket` + +| Method | Member variables | Scenario | Result | +|--------------------------|---------------------|-------------------|-------------------------------------------------------------------------------| +| `double calculateCost()` | `List 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. | + diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java new file mode 100644 index 0000000..6897c42 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,25 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class Basket { + Map 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; + } +} diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java new file mode 100644 index 0000000..1b80f1a --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -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()); + } +}