From f1ef825aa9d28df632840722ee2341b0753cf0be Mon Sep 17 00:00:00 2001 From: Tuva Aarseth Date: Wed, 14 Aug 2024 09:54:12 +0200 Subject: [PATCH 1/3] Created simple domain model --- EXERCISE1.md | 14 ++++++++++++++ domain-model.md | 11 +++++++++++ 2 files changed, 25 insertions(+) create mode 100644 domain-model.md diff --git a/EXERCISE1.md b/EXERCISE1.md index 3af2ea2..1ab65d2 100644 --- a/EXERCISE1.md +++ b/EXERCISE1.md @@ -27,6 +27,13 @@ Here is how one might design a domain model for the above user story: > > Create your own domain model for the user story above, try to come up with a different solution than the model provided. You can use a table like the one above, a spreadsheet, pen and paper, whatever you'd like. Share your work in your cohorts classroom channel when you're done. +| Classes | Member variables | Methods | Scenario | Outcomes | +|----------------|-----------------------------|-----------------------|--------------|------| +| `CohortManager` | `ArrayList cohorts` | `search(String name)` | Name in list | True | +| | | | | | + + + ### Exercise Follow the same process as above to translate these two user stories into domain models. @@ -37,6 +44,13 @@ 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. ``` +| Classes | Member variables | Methods | Scenario | Outcomes | +|---------------|------------------|---------|----------|----------| +| `Supermarket` | | | | | +| | | | | | +| | | | | | + + ``` As an organised individual, So that I can evaluate my shopping habits, diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..14727bb --- /dev/null +++ b/domain-model.md @@ -0,0 +1,11 @@ +| Classes | Member variables | Methods | Scenario | Outcomes | +|---------------|-------------------------------------------------|--------------------------------------------------------------------|----------------------------------|-------------------------------------------------------| +| `Supermarket` | `HashMap products` | `calculateTotal(HashMap basket)` | Empty basket
Items in basket | Return "Basket is empty"
Return total cost of items | + + + + + +| Classes | Member variables | Methods | Scenario | Outcomes | +|-----------|----------------------------------------------------------------------------------------------------------|-------------------------------------------|-----------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------| +| `Receipt` | `HashMap products`
`HashMap basket` | `calculateTotal()`

`itemizeReceipt()` | Empty basket
Products in basket
Payment not complete
Payment complete | Return "Basket is empty"
Return total cost of products
Return "Payment is not complete"
Return itemized receipt | From 88408127f5e93059f0bf8f0ae5f858909f4d646c Mon Sep 17 00:00:00 2001 From: Tuva Aarseth Date: Wed, 14 Aug 2024 11:34:25 +0200 Subject: [PATCH 2/3] Added tests --- src/main/java/com/booleanuk/core/Basket.java | 26 ++++++++++++++ .../com/booleanuk/core/CohortManager.java | 6 ++++ .../java/com/booleanuk/core/BasketTest.java | 31 +++++++++++++++++ .../com/booleanuk/core/CohortManagerTest.java | 34 +++++++++++++++++++ 4 files changed, 97 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..8417b42 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,26 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class Basket { + HashMap items; + + public Basket(){ + this.items = new HashMap<>(); + this.items.put("Something", 20); + this.items.put("Some2", 30); + } + + public Boolean add(String product, int price){ + return !items.containsKey(product); + } + + public Integer total(){ + int total = 0; + for (Map.Entry set : items.entrySet()){ + total+=set.getValue(); + } + return total; + } +} diff --git a/src/main/java/com/booleanuk/core/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..99b1ff0 100644 --- a/src/main/java/com/booleanuk/core/CohortManager.java +++ b/src/main/java/com/booleanuk/core/CohortManager.java @@ -1,5 +1,11 @@ package com.booleanuk.core; +import java.util.ArrayList; + public class CohortManager { + public Boolean search(ArrayList cohorts, String name){ + return cohorts.contains(name); + } + } 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..38885f6 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,31 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.HashMap; + +public class BasketTest { + HashMap items; + + public BasketTest(){ + this.items = new HashMap<>(); + this.items.put("Something", 20); + this.items.put("Some2", 30); + } + + @Test + public void testItemInBasket(){ + Basket basket = new Basket(); + Assertions.assertTrue(basket.add("new", 40)); + Assertions.assertFalse(basket.add("Some2", 30)); + } + + @Test + public void testTotal(){ + Basket basket = new Basket(); + int total=basket.total(); + Assertions.assertEquals(50, total); + } +} diff --git a/src/test/java/com/booleanuk/core/CohortManagerTest.java b/src/test/java/com/booleanuk/core/CohortManagerTest.java index 5dea868..7509e99 100644 --- a/src/test/java/com/booleanuk/core/CohortManagerTest.java +++ b/src/test/java/com/booleanuk/core/CohortManagerTest.java @@ -3,6 +3,40 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; + class CohortManagerTest { + // One way of doing it + /* + CohortManager cohortManager; + + public CohortManagerTest(){ + this.cohortManager = new CohortManager(); + } + */ + + // Another way of doing it + ArrayList cohorts; + + public CohortManagerTest(){ + this.cohorts = new ArrayList<>(); + this.cohorts.add("Something"); + this.cohorts.add("Some2"); + } + + @Test + public void testSearchExists(){ + CohortManager cohortManager = new CohortManager(); + Assertions.assertFalse(cohortManager.search(cohorts, "")); + } + + @Test + public void testSearchFindName(){ + CohortManager cohortManager = new CohortManager(); + Assertions.assertTrue(cohortManager.search(cohorts, "Some2")); + Assertions.assertFalse(cohortManager.search(cohorts, "Some3")); + } + + } From 4a2145fc3ca3ae21e5dbbb54ee3c38ffcf422634 Mon Sep 17 00:00:00 2001 From: Tuva Aarseth Date: Wed, 14 Aug 2024 11:58:33 +0200 Subject: [PATCH 3/3] Changes to tests --- src/main/java/com/booleanuk/core/Basket.java | 23 +++++++++++-------- .../java/com/booleanuk/core/BasketTest.java | 15 +++++------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 8417b42..652c59e 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -4,22 +4,25 @@ import java.util.Map; public class Basket { - HashMap items; - - public Basket(){ - this.items = new HashMap<>(); - this.items.put("Something", 20); - this.items.put("Some2", 30); - } + HashMap items = new HashMap<>(); public Boolean add(String product, int price){ - return !items.containsKey(product); + if (!items.isEmpty() && items.containsKey(product)){ + return false; + } else { + items.put(product, price); + return true; + } } public Integer total(){ int total = 0; - for (Map.Entry set : items.entrySet()){ - total+=set.getValue(); + if (items.isEmpty()){ + return 0; + }else { + for (Map.Entry set : items.entrySet()){ + total+=set.getValue(); + } } return total; } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 38885f6..9ad5c3e 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -7,25 +7,22 @@ import java.util.HashMap; public class BasketTest { - HashMap items; - public BasketTest(){ - this.items = new HashMap<>(); - this.items.put("Something", 20); - this.items.put("Some2", 30); - } @Test public void testItemInBasket(){ Basket basket = new Basket(); Assertions.assertTrue(basket.add("new", 40)); - Assertions.assertFalse(basket.add("Some2", 30)); + Assertions.assertFalse(basket.add("new", 30)); } @Test public void testTotal(){ Basket basket = new Basket(); - int total=basket.total(); - Assertions.assertEquals(50, total); + Assertions.assertEquals(0, basket.total()); + + basket.add("new", 30); + basket.add("new2", 30); + Assertions.assertEquals(60, basket.total()); } }