diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..917d6b3 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,28 @@ + +| Methods | Members | Scenario | Result | +|--------------------------------------|---------|------------------------|----------------------------------------------| +| `calculate(List itemCosts)` | | No item prices in list | total = 0 | +| | | n items prices in list | total = sum of n item prices | +| `addItem(List itemCosts, int newItemCost)` | | Valid itemCost | Item price gets added at the end of the list | +| | | Invalid itemCost | Item doesn't get added + error message | + + + + + + + +| Methods | Members | Scenario | Result | +|-------------------------------------------------|------------------------------|------------------------------------------------------------------------------|-------------------------------------| +| `getNameAndPrice(HashMap map)` | Hashmap map | Valid key-value pairs | return the name and price correctly | +| | int quantity | Missing keys or value | return error | +| `getQuantity()` | | Valid quantity (not -1) | return quantity | +| | | Invalid quantity | return error | +| `calculateTotal(String itemName, int quantity)` | | itemName exists in the hashmap as a key and a valid value, quantity is valid | return the total price | +| | | Missing or invalid paramets | return error | + + + + + + 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..8e247fd --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,24 @@ +package com.booleanuk.core; + +import java.util.HashMap; + +public class Basket { + + HashMap items = new HashMap<>(); + int total = 0; + + public boolean add(String product, int price) { + if (!items.containsKey(product)) { + items.put(product, price); + return true; + } + return false; + } + + public int total() { + for (String key : items.keySet()) { + total += items.get(key); + } + return this.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..1b821aa --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,38 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + @Test + public void itemIsNotAlreadyInBasket() { + Basket basket = new Basket(); + basket.items.put("Apple", 1); + basket.items.put("Banana", 20); + Assertions.assertTrue(basket.add("TestItem1", 1)); + } + + @Test + public void itemIsAlreadyInBasket() { + Basket basket = new Basket(); + basket.items.put("Apple", 1); + basket.items.put("Banana", 20); + basket.items.put("TestItem2", 1); + Assertions.assertFalse(basket.add("TestItem2", 1)); + } + + @Test + public void totalShouldBeZero() { + Basket basket = new Basket() {}; + Assertions.assertEquals(0, basket.total()); + } + + @Test + public void totalShouldIncrease() { + Basket basket = new Basket() {}; + basket.add("Banana", 20); + Assertions.assertEquals(20, basket.total()); + } + +}