From 9a428d6d0c8b3067c22d6321c54971572b191360 Mon Sep 17 00:00:00 2001 From: Ludwig J Lundborg Date: Wed, 14 Aug 2024 11:36:27 +0200 Subject: [PATCH] max, leo ludwig --- EXERCISE1.md | 10 +++- src/main/java/com/booleanuk/core/Basket.java | 33 +++++++++++ .../com/booleanuk/core/CohortManager.java | 2 + .../java/com/booleanuk/core/BasketTest.java | 55 +++++++++++++++++++ 4 files changed, 99 insertions(+), 1 deletion(-) 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/EXERCISE1.md b/EXERCISE1.md index 3af2ea2..461fc69 100644 --- a/EXERCISE1.md +++ b/EXERCISE1.md @@ -21,6 +21,9 @@ Here is how one might design a domain model for the above user story: | `CohortManager` | `search(List cohorts, String name)` | If name is in list | true | | | | If name is not in list | false | +| Classes | Methods | Scenario | Outputs | | +|---------|---------|----------|---------|---| + > **Time to analyse** > > Evaluate the user story and the domain model above. What assumptions did the developer have to make and what would you do differently? @@ -44,5 +47,10 @@ I'd like to see an itemised receipt that includes the name and price of the prod 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. \ No newline at end of file +- 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 methods member variables scenario outcomes + 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..33399fa --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,33 @@ +package com.booleanuk.core; + +import java.util.HashMap; + +public class Basket { + + private HashMap items; + + public Basket(){ + this.items = new HashMap<>(); + } + + public boolean add(String product, int price){ + if(!items.containsKey(product)){ + this.items.put(product, price); + + return true; + } + return false; + } + + public int total(){ + + int total = 0; + for (int i : this.items.values()){ + total += i; + } + return total; + } + + + +} diff --git a/src/main/java/com/booleanuk/core/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..901fe09 100644 --- a/src/main/java/com/booleanuk/core/CohortManager.java +++ b/src/main/java/com/booleanuk/core/CohortManager.java @@ -2,4 +2,6 @@ public class CohortManager { + + } 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..48f2983 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,55 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + @Test + public void testBasketExist() { + Basket basket = new Basket(); + String product = "Cucumber"; + int price = 399; + + Assertions.assertTrue(basket.add(product, price)); + } + + @Test + public void testBasketAddItem() { + Basket basket = new Basket(); + String product = "Cucumber"; + int price = 399; + + Assertions.assertTrue(basket.add(product, price)); + Assertions.assertFalse(basket.add(product, price)); + } + + @Test + public void testTotalExist(){ + Basket basket = new Basket(); + + Assertions.assertEquals(0, basket.total()); + } + + @Test + public void testTotalValueOfBasketOneItem(){ + Basket basket = new Basket(); + String product = "Cucumber"; + int price = 399; + basket.add(product, price); + + Assertions.assertEquals(399, basket.total()); + } + + @Test + public void testTotalValueOfBasketManyItems(){ + Basket basket = new Basket(); + basket.add("Cucumber", 10); + basket.add("Tomato", 20); + basket.add("Coffee", 30); + basket.add("Red bull", 40); + + Assertions.assertEquals(100, basket.total()); + } + +}