From ea452df7c51d24baf77ffb73fcab1152c5f319ec Mon Sep 17 00:00:00 2001 From: Simon Fredriksson Date: Wed, 14 Aug 2024 10:03:24 +0200 Subject: [PATCH 1/5] Domain model solution. --- domain-model.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 domain-model.md diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..90c3bdc --- /dev/null +++ b/domain-model.md @@ -0,0 +1,19 @@ +| Classes | Variables | Methods | Scenario | Outcomes | +|----------------|----------------------------------|---------------------------------------|-----------------------|----------------------| +| `CashRegister` | `Map weightItem` | `getPriceForWeightItem(int PLUCode)` | Product is in map | Add price to total | +| | `Map barcodeItem` | `getPriceForBarcodeItem(int barcode)` | Product is not in map | Return error message | +| | | | | | +| | `ìnt totalCost` | `getTotalCost()` | Total cost is > 0 | Return total cost | +| | | | Total cost is < 0 | Return error message | +| | | | | | + +| Classes | Variables | Methods | Scenario | Outcomes | +|---------------|-------------------------------------------------------------|-----------------------------------------|--------------------------|--------------------------| +| `MembersDesk` | `Map>> shoppingHistory` | `getShoppingHistory(int membersNumber)` | Customer is a member | Return list of purchases | +| | | | Customer is not a member | Return error message | +| | | | | | +| | | `getItemisedReceipt(int receiptNumber)` | Valid receipt number | Return receipt | +| | | | In valid receipt number | Return error message | +| | | | | | + + From 2b0bddc549cd8e2a1dc4340458c0ac460eb0a5fc Mon Sep 17 00:00:00 2001 From: Simon Fredriksson Date: Wed, 14 Aug 2024 10:09:00 +0200 Subject: [PATCH 2/5] Domain model solution. --- domain-model.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/domain-model.md b/domain-model.md index 90c3bdc..b9c12d8 100644 --- a/domain-model.md +++ b/domain-model.md @@ -6,6 +6,8 @@ | | `ìnt totalCost` | `getTotalCost()` | Total cost is > 0 | Return total cost | | | | | Total cost is < 0 | Return error message | | | | | | | +| | | `addToTotalCost(long price)` | Price is > 0 | Add to totalCost | +| | | | Price is < 0 | Return | | Classes | Variables | Methods | Scenario | Outcomes | |---------------|-------------------------------------------------------------|-----------------------------------------|--------------------------|--------------------------| From 9b2d3729fd7275a4418f1f2645995e5ae6f6c9b2 Mon Sep 17 00:00:00 2001 From: Simon Fredriksson Date: Wed, 14 Aug 2024 10:13:52 +0200 Subject: [PATCH 3/5] Domain model solution. --- domain-model.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/domain-model.md b/domain-model.md index b9c12d8..290ef9a 100644 --- a/domain-model.md +++ b/domain-model.md @@ -1,13 +1,13 @@ -| Classes | Variables | Methods | Scenario | Outcomes | -|----------------|----------------------------------|---------------------------------------|-----------------------|----------------------| -| `CashRegister` | `Map weightItem` | `getPriceForWeightItem(int PLUCode)` | Product is in map | Add price to total | -| | `Map barcodeItem` | `getPriceForBarcodeItem(int barcode)` | Product is not in map | Return error message | -| | | | | | -| | `ìnt totalCost` | `getTotalCost()` | Total cost is > 0 | Return total cost | -| | | | Total cost is < 0 | Return error message | -| | | | | | -| | | `addToTotalCost(long price)` | Price is > 0 | Add to totalCost | -| | | | Price is < 0 | Return | +| Classes | Variables | Methods | Scenario | Outcomes | +|----------------|-------------------------------------|---------------------------------------|-----------------------|----------------------| +| `CashRegister` | `Map weightItem` | `getPriceForWeightItem(int PLUCode)` | Product is in map | Add price to total | +| | `Map barcodeItem` | `getPriceForBarcodeItem(int barcode)` | Product is not in map | Return error message | +| | | | | | +| | `ìnt totalCost` | `getTotalCost()` | Total cost is > 0 | Return total cost | +| | | | Total cost is < 0 | Return error message | +| | | | | | +| | | `addToTotalCost(int price)` | Price is > 0 | Add to totalCost | +| | | | Price is < 0 | Return | | Classes | Variables | Methods | Scenario | Outcomes | |---------------|-------------------------------------------------------------|-----------------------------------------|--------------------------|--------------------------| From 5a7eaf7a19689aa19f9268767e7f96c35a93ecbf Mon Sep 17 00:00:00 2001 From: Simon Fredriksson Date: Wed, 14 Aug 2024 11:28:33 +0200 Subject: [PATCH 4/5] TDD solution. --- src/main/java/com/booleanuk/core/Basket.java | 24 +++++++++++++ .../java/com/booleanuk/core/BasketTest.java | 34 +++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/main/java/com/booleanuk/core/Basket.java create mode 100644 src/test/java/com/booleanuk/core/BasketTest.java 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..18cadf1 --- /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 { + private final HashMap items = new HashMap<>(); + + public boolean add(String product, int price) { + if (product.isEmpty() || items.containsKey(product)) { + return false; + } + items.put(product, price); + return true; + } + + public int total() { + int total = 0; + + for (int price : 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..ec717a1 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,34 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + Basket basket = new Basket(); + + @Test + public void addMethodExistTest() { + Assertions.assertFalse(basket.add("", 0)); + } + + @Test + public void addItemTest() { + Assertions.assertFalse(basket.add("", 0)); + Assertions.assertTrue(basket.add("Milk", 5)); + Assertions.assertFalse(basket.add("Milk", 5)); + Assertions.assertTrue(basket.add("Bread", 2)); + Assertions.assertTrue(basket.add("Eggs", 3)); + } + + @Test + public void totalTest() { + Assertions.assertTrue(basket.add("Milk", 5)); + Assertions.assertTrue(basket.add("Bread", 2)); + Assertions.assertTrue(basket.add("Eggs", 3)); + Assertions.assertNotEquals(0, basket.total()); + Assertions.assertEquals(10, basket.total()); + } + + +} From a716d7a68e1245e6a993761c6da0917e5198a7d8 Mon Sep 17 00:00:00 2001 From: Simon Fredriksson Date: Wed, 14 Aug 2024 11:44:43 +0200 Subject: [PATCH 5/5] TDD solution. --- src/main/java/com/booleanuk/core/Basket.java | 2 +- src/test/java/com/booleanuk/core/BasketTest.java | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 18cadf1..20c7a7d 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -21,4 +21,4 @@ public int total() { } return total; } -} +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index ec717a1..e8289ae 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -28,6 +28,10 @@ public void totalTest() { Assertions.assertTrue(basket.add("Eggs", 3)); Assertions.assertNotEquals(0, basket.total()); Assertions.assertEquals(10, basket.total()); + + Assertions.assertTrue(basket.add("Apple", 5)); + Assertions.assertEquals(15, basket.total()); + }