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 | 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..652c59e --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,29 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class Basket { + HashMap items = new HashMap<>(); + + public Boolean add(String product, int price){ + if (!items.isEmpty() && items.containsKey(product)){ + return false; + } else { + items.put(product, price); + return true; + } + } + + public Integer total(){ + int total = 0; + if (items.isEmpty()){ + return 0; + }else { + 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..9ad5c3e --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,28 @@ +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 { + + + @Test + public void testItemInBasket(){ + Basket basket = new Basket(); + Assertions.assertTrue(basket.add("new", 40)); + Assertions.assertFalse(basket.add("new", 30)); + } + + @Test + public void testTotal(){ + Basket basket = new Basket(); + Assertions.assertEquals(0, basket.total()); + + basket.add("new", 30); + basket.add("new2", 30); + Assertions.assertEquals(60, basket.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")); + } + + }