From 2ae96aca3eeb2391d0cbfb8b7f4ecd2d8e165e4f Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 09:22:01 +0200 Subject: [PATCH 01/13] added domain-model.md and a model for the shopping problem --- domain-model.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 domain-model.md diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..f5ba54e --- /dev/null +++ b/domain-model.md @@ -0,0 +1,34 @@ + + +### Exercise + +Follow the same process as above to translate these two user stories into domain models. + +``` +As a supermarket shopper, +So that I can pay for products at checkout, +I'd like to be able to know the total cost of items in my basket. +``` + +``` +As an organised individual, +So that I can evaluate my shopping habits, +I'd like to see an itemised receipt that includes the name and price of the products +I bought as well as the quantity, and a total cost of my basket. +``` + +- Add your domain models to this repository as a file named `domain-model`. This should either be a `.md` file like this one, or a screenshot / picture of your work. +- Your model doesn't have to look like the example provided in this file. If you feel like you need more or less columns, feel free to go with that. There is no "right way" to do this kind of thing, we're just designing a system to make our lives easier when it comes to the coding part. + + +| Classes | Variables | Methods | Scenario | Outcome | +|--------------|-------------------------------------|--------------------------------|----------------------------------------------------------------------|------------------------------------------------------------------------------------------| +| `CartManager` | `List shoppingCart` | | | | +| | `int totalCost ` | | | | +| | `HashMap itemCostMap` | | | | +| | | `addItem(String item)` | Shopper adds an item to list | Item is added to cart | +| | | `getTotalCost()` | Shopper wants to know the total cost | Shopper gets the total cost of items in the cart | +| | | `getQuantityOfItem(String item)` | Shopper wants to know the quantity of an item in the cart | Shopper gets the quantity of a given item from the cart. | +| | | `getItemizedReciept()` | Shopper wants to see an overview of all items in cart and total cost | Shopper gets the total cost of all items, and a list of how many items and the per-price | + + From 4d2ba0ef4d14e9705d3c74e91c22ca3d827f1985 Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 09:37:41 +0200 Subject: [PATCH 02/13] added more scenarios and updated description --- domain-model.md | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/domain-model.md b/domain-model.md index f5ba54e..fa5f0b4 100644 --- a/domain-model.md +++ b/domain-model.md @@ -20,15 +20,18 @@ I bought as well as the quantity, and a total cost of my basket. - Add your domain models to this repository as a file named `domain-model`. This should either be a `.md` file like this one, or a screenshot / picture of your work. - Your model doesn't have to look like the example provided in this file. If you feel like you need more or less columns, feel free to go with that. There is no "right way" to do this kind of thing, we're just designing a system to make our lives easier when it comes to the coding part. - -| Classes | Variables | Methods | Scenario | Outcome | -|--------------|-------------------------------------|--------------------------------|----------------------------------------------------------------------|------------------------------------------------------------------------------------------| -| `CartManager` | `List shoppingCart` | | | | -| | `int totalCost ` | | | | -| | `HashMap itemCostMap` | | | | -| | | `addItem(String item)` | Shopper adds an item to list | Item is added to cart | -| | | `getTotalCost()` | Shopper wants to know the total cost | Shopper gets the total cost of items in the cart | -| | | `getQuantityOfItem(String item)` | Shopper wants to know the quantity of an item in the cart | Shopper gets the quantity of a given item from the cart. | -| | | `getItemizedReciept()` | Shopper wants to see an overview of all items in cart and total cost | Shopper gets the total cost of all items, and a list of how many items and the per-price | +## Domain-model for shopping cart + +| Classes | Variables | Methods | Scenario | Outcome | +|--------------|-------------------------------------------------|----------------------------------|----------------------------------------------------------------------|------------------------------------------------------------------------------------------| +| `CartManager` | `private HashMap shoppingCart` | | | | +| | ` private int totalCost ` | | | | +| | `static HashMap itemCostMap` | | | | +| | | `addItem(String item)` | Shopper adds an item to cart | Item is added to cart. If it exists, increment quantity counter. totalCost is updated. | +| | | `removeItem(String item)` | Shopper removes an item from the cart | If item is in cart, decrement the quanity counter. totalCost is updated. | +| | | `removeAllOfItem(String item)` | Shopper removes all quantities of item from cart | Item entry is removed from the cart map. totalCost is updated. | +| | | `getTotalCost()` | Shopper wants to know the total cost | Shopper gets the total cost of items in the cart. | +| | | `getQuantityOfItem(String item)` | Shopper wants to know the quantity of an item in the cart | Shopper gets the quantity of a given item from the cart. | +| | | `getItemizedReciept()` | Shopper wants to see an overview of all items in cart and total cost | Shopper gets the total cost of all items, and a list of how many items and the per-price | From 85010dc9306d0b292ebdd289615288a6054dc76b Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 09:40:45 +0200 Subject: [PATCH 03/13] wording --- domain-model.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain-model.md b/domain-model.md index fa5f0b4..82fc76a 100644 --- a/domain-model.md +++ b/domain-model.md @@ -28,7 +28,7 @@ I bought as well as the quantity, and a total cost of my basket. | | ` private int totalCost ` | | | | | | `static HashMap itemCostMap` | | | | | | | `addItem(String item)` | Shopper adds an item to cart | Item is added to cart. If it exists, increment quantity counter. totalCost is updated. | -| | | `removeItem(String item)` | Shopper removes an item from the cart | If item is in cart, decrement the quanity counter. totalCost is updated. | +| | | `removeItem(String item)` | Shopper removes an item from the cart | If item is in cart, decrement the quantity counter. totalCost is updated. | | | | `removeAllOfItem(String item)` | Shopper removes all quantities of item from cart | Item entry is removed from the cart map. totalCost is updated. | | | | `getTotalCost()` | Shopper wants to know the total cost | Shopper gets the total cost of items in the cart. | | | | `getQuantityOfItem(String item)` | Shopper wants to know the quantity of an item in the cart | Shopper gets the quantity of a given item from the cart. | From 6a948240eb8c558fc4099adf81717e0dc28a26fd Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 10:05:41 +0200 Subject: [PATCH 04/13] wording 2 --- domain-model.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/domain-model.md b/domain-model.md index 82fc76a..081d7f8 100644 --- a/domain-model.md +++ b/domain-model.md @@ -26,7 +26,7 @@ I bought as well as the quantity, and a total cost of my basket. |--------------|-------------------------------------------------|----------------------------------|----------------------------------------------------------------------|------------------------------------------------------------------------------------------| | `CartManager` | `private HashMap shoppingCart` | | | | | | ` private int totalCost ` | | | | -| | `static HashMap itemCostMap` | | | | +| | `static HashMap itemCostMap` | | | | | | | `addItem(String item)` | Shopper adds an item to cart | Item is added to cart. If it exists, increment quantity counter. totalCost is updated. | | | | `removeItem(String item)` | Shopper removes an item from the cart | If item is in cart, decrement the quantity counter. totalCost is updated. | | | | `removeAllOfItem(String item)` | Shopper removes all quantities of item from cart | Item entry is removed from the cart map. totalCost is updated. | From 4762136ce5556537e193ffa44fc642201933efac Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 10:09:17 +0200 Subject: [PATCH 05/13] added funcs --- domain-model.md | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/domain-model.md b/domain-model.md index 081d7f8..0b37608 100644 --- a/domain-model.md +++ b/domain-model.md @@ -22,16 +22,18 @@ I bought as well as the quantity, and a total cost of my basket. ## Domain-model for shopping cart -| Classes | Variables | Methods | Scenario | Outcome | -|--------------|-------------------------------------------------|----------------------------------|----------------------------------------------------------------------|------------------------------------------------------------------------------------------| -| `CartManager` | `private HashMap shoppingCart` | | | | -| | ` private int totalCost ` | | | | -| | `static HashMap itemCostMap` | | | | -| | | `addItem(String item)` | Shopper adds an item to cart | Item is added to cart. If it exists, increment quantity counter. totalCost is updated. | -| | | `removeItem(String item)` | Shopper removes an item from the cart | If item is in cart, decrement the quantity counter. totalCost is updated. | -| | | `removeAllOfItem(String item)` | Shopper removes all quantities of item from cart | Item entry is removed from the cart map. totalCost is updated. | -| | | `getTotalCost()` | Shopper wants to know the total cost | Shopper gets the total cost of items in the cart. | -| | | `getQuantityOfItem(String item)` | Shopper wants to know the quantity of an item in the cart | Shopper gets the quantity of a given item from the cart. | +| Classes | Variables | Methods | Scenario | Outcome | +|--------------|-------------------------------------------------|----------------------------------|-------------------------------------------------------------------|------------------------------------------------------------------------------------------| +| `CartManager` | `private HashMap shoppingCart` | | | | +| | ` private int totalCost ` | | | | +| | `static HashMap itemCostMap` | | | | +| | | `addItem(String item)` | Shopper adds an item to cart | Item is added to cart. If it exists, increment quantity counter. totalCost is updated. | +| | | `addItem(String item, int x)` | Add x items to cart | x items is added to cart. update totalCost | +| | | `removeItem(String item, int x)` | Shopper removes x items from cart | x items are removed from cart. update totalCost | +| | | `removeItem(String item)` | Shopper removes an item from the cart | If item is in cart, decrement the quantity counter. totalCost is updated. | +| | | `removeAllOfItem(String item)` | Shopper removes all quantities of item from cart | Item entry is removed from the cart map. totalCost is updated. | +| | | `getTotalCost()` | Shopper wants to know the total cost | Shopper gets the total cost of items in the cart. | +| | | `getQuantityOfItem(String item)` | Shopper wants to know the quantity of an item in the cart | Shopper gets the quantity of a given item from the cart. | | | | `getItemizedReciept()` | Shopper wants to see an overview of all items in cart and total cost | Shopper gets the total cost of all items, and a list of how many items and the per-price | From eae659ab3917b06144d72e5f2c7f0a7522a82edd Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:15:43 +0200 Subject: [PATCH 06/13] red testadd --- src/main/java/com/booleanuk/core/Basket.java | 4 ++++ .../com/booleanuk/core/CohortManager.java | 6 ++++- .../java/com/booleanuk/core/BasketTest.java | 24 +++++++++++++++++++ .../com/booleanuk/core/CohortManagerTest.java | 3 ++- 4 files changed, 35 insertions(+), 2 deletions(-) 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..4aab45e --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,4 @@ +package com.booleanuk.core; + +public class Basket { +} diff --git a/src/main/java/com/booleanuk/core/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..0ff20f0 100644 --- a/src/main/java/com/booleanuk/core/CohortManager.java +++ b/src/main/java/com/booleanuk/core/CohortManager.java @@ -1,5 +1,9 @@ package com.booleanuk.core; -public class CohortManager { +import java.util.ArrayList; +public class CohortManager { + public boolean search(ArrayList cohorts, String name){ + return true; + } } 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..fec4a89 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,24 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + public Basket getTestBasket(){ + return new Basket(); + } + + + @Test + public void testBasketConstructor(){ + Basket basket = getTestBasket(); + } + + @Test + void testadd(){ + Basket basket = getTestBasket(); + basket.add("Banana", 10); + basket.add("Coca Cola", 20); + Assertions.assertTrue(basket.items.contains("Banana")); + } +} diff --git a/src/test/java/com/booleanuk/core/CohortManagerTest.java b/src/test/java/com/booleanuk/core/CohortManagerTest.java index 5dea868..57ccd35 100644 --- a/src/test/java/com/booleanuk/core/CohortManagerTest.java +++ b/src/test/java/com/booleanuk/core/CohortManagerTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -class CohortManagerTest { +import java.lang.reflect.Method; +class CohortManagerTest { } From 168a2e0132dd028271d40291cc7706b41bc1fab9 Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:20:14 +0200 Subject: [PATCH 07/13] green. Refactor --- src/main/java/com/booleanuk/core/Basket.java | 8 ++++++++ src/test/java/com/booleanuk/core/BasketTest.java | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 4aab45e..1e1790e 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,4 +1,12 @@ package com.booleanuk.core; +import java.util.HashMap; + public class Basket { + public HashMap items = new HashMap<>(); + + public boolean add(String product, int price){ + return true; + } + } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index fec4a89..11060dd 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -19,6 +19,7 @@ void testadd(){ Basket basket = getTestBasket(); basket.add("Banana", 10); basket.add("Coca Cola", 20); - Assertions.assertTrue(basket.items.contains("Banana")); + Assertions.assertTrue(basket.items.containsKey("Banana")); + Assertions.assertTrue(basket.items.containsKey("Coca Cola")); } } From 4119913e9f2c3d5757f49dccb0dab0609209dd2c Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:21:04 +0200 Subject: [PATCH 08/13] green, refactored. --- src/main/java/com/booleanuk/core/Basket.java | 4 +++- src/test/java/com/booleanuk/core/BasketTest.java | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 1e1790e..999011b 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -6,7 +6,9 @@ public class Basket { public HashMap items = new HashMap<>(); public boolean add(String product, int price){ - return true; + boolean alreadyInBasket = items.containsKey(product); + items.put(product, price); + return alreadyInBasket; } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 11060dd..c90fb08 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -21,5 +21,6 @@ void testadd(){ basket.add("Coca Cola", 20); Assertions.assertTrue(basket.items.containsKey("Banana")); Assertions.assertTrue(basket.items.containsKey("Coca Cola")); + Assertions.assertFalse(basket.items.containsKey("Unknown item")); } } From e7ae595a4eceb2a71fd73e0204534cb7e5666c52 Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:24:31 +0200 Subject: [PATCH 09/13] red. new test --- src/test/java/com/booleanuk/core/BasketTest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index c90fb08..1aa500c 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -23,4 +23,14 @@ void testadd(){ Assertions.assertTrue(basket.items.containsKey("Coca Cola")); Assertions.assertFalse(basket.items.containsKey("Unknown item")); } + + @Test + void testTotal(){ + Basket basket = getTestBasket(); + basket.add("Banana", 10); + basket.add("Coca Cola", 20); + + Assertions.assertEquals(testTotal(), 30); + + } } From 3abf5408bc78d5ea6835ef018022ccbd9c9b8388 Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:25:14 +0200 Subject: [PATCH 10/13] still red. --- src/main/java/com/booleanuk/core/Basket.java | 3 +++ src/test/java/com/booleanuk/core/BasketTest.java | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 999011b..63abac3 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -11,4 +11,7 @@ public boolean add(String product, int price){ return alreadyInBasket; } + public int total(){ + return 0; + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 1aa500c..57bca51 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -30,7 +30,7 @@ void testTotal(){ basket.add("Banana", 10); basket.add("Coca Cola", 20); - Assertions.assertEquals(testTotal(), 30); + Assertions.assertEquals(basket.total(), 30); } } From 1eedc99aefdb034be8a80cfc344d543df190d630 Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:27:04 +0200 Subject: [PATCH 11/13] green --- src/main/java/com/booleanuk/core/Basket.java | 6 +++++- 1 file 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 63abac3..200f48d 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -12,6 +12,10 @@ public boolean add(String product, int price){ } public int total(){ - return 0; + int total = 0; + for(int price: items.values()){ + total = total + price; + } + return total; } } From cea4276c410c9fd121e7aa339cfb46178743a198 Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:29:23 +0200 Subject: [PATCH 12/13] green. refactored --- src/test/java/com/booleanuk/core/BasketTest.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 57bca51..75e2040 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -32,5 +32,9 @@ void testTotal(){ Assertions.assertEquals(basket.total(), 30); + Assertions.assertNotEquals(basket.total(), 45); + basket.add("kaptein sabeltann is", 15); + Assertions.assertEquals(basket.total(), 45); + } } From 0e3f0ec51e09f40c133ccc3c5fcb35b6506bd505 Mon Sep 17 00:00:00 2001 From: Dag Andreas Date: Wed, 14 Aug 2024 11:30:33 +0200 Subject: [PATCH 13/13] done baskettest --- src/test/java/com/booleanuk/core/BasketTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 75e2040..6409463 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -32,9 +32,9 @@ void testTotal(){ Assertions.assertEquals(basket.total(), 30); - Assertions.assertNotEquals(basket.total(), 45); + Assertions.assertNotEquals(basket.total(), 45); // should not be 45 yet basket.add("kaptein sabeltann is", 15); - Assertions.assertEquals(basket.total(), 45); + Assertions.assertEquals(basket.total(), 45); // should now be 45 } }