diff --git a/EXERCISE1.md b/EXERCISE1.md index 3af2ea2..ed5e829 100644 --- a/EXERCISE1.md +++ b/EXERCISE1.md @@ -37,6 +37,17 @@ 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 | Methods | Scenario | Result | +|----------|----------------------------------|------------------------|---------------------------| +| `Basket` | `addItem(String item, int cost)` | If basket isn't full | Adds item to basket | +| | | If basket is full | Cant add item to basket | +| | | | | +| | `calculateCost()` | If basket isn't empty | Returns cost of all items | +| | | If basket is empty | Return 0 | +| | | | | + + + ``` As an organised individual, So that I can evaluate my shopping habits, @@ -44,5 +55,12 @@ 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. ``` +| Classes | Methods | Scenario | Result | +|------------|-----------------------------|------------------------------|-----------------------------| +| `Receipt` | `getReceipt(Basket basket)` | Get a receipt after shopping | Returns info about the trip | +| | | | | +| | | | | + + - 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 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..d061180 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,27 @@ +package com.booleanuk.core; + +import java.util.HashMap; + +public class Basket { + + HashMap items = new HashMap<>(); + + public boolean add(String product, int price){ + if (!items.containsKey(product)){ + items.put(product, price); + return true; + } + return false; + } + + public int total(){ + int totalCost = 0; + + for (Integer price : items.values()){ + totalCost += price; + } + + return totalCost; + } + +} diff --git a/src/main/java/com/booleanuk/core/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..eb943b7 100644 --- a/src/main/java/com/booleanuk/core/CohortManager.java +++ b/src/main/java/com/booleanuk/core/CohortManager.java @@ -1,5 +1,12 @@ package com.booleanuk.core; +import java.lang.reflect.Array; +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..d3d7ab7 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,45 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + @Test + public void addProductNotInBasket(){ + // Setup + Basket basket = new Basket(); + String productName = "crisps"; + int price = 30; + + // Execute and verify + Assertions.assertTrue(basket.add(productName, price)); + } + + @Test + public void addProductAlreadyInBasket(){ + // Setup + Basket basket = new Basket(); + String productName = "crisps"; + int price = 30; + + basket.add(productName, price); + + // Execute and verify + Assertions.assertFalse(basket.add(productName, price)); + } + + @Test + public void totalCostBasket(){ + Basket basket = new Basket(); + + basket.add("crisps", 30); + basket.add("fanta", 25); + basket.add("haribo", 30); + + int totalCost = basket.total(); + + Assertions.assertEquals(85, totalCost); + } + +} diff --git a/src/test/java/com/booleanuk/core/CohortManagerTest.java b/src/test/java/com/booleanuk/core/CohortManagerTest.java index 5dea868..2dffe8f 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 { + @Test + public void searchForCohortsThatExists(){ + // Setup + CohortManager cohortManager = new CohortManager(); + ArrayList cohorts = new ArrayList<>(){{ + add("Liverpool"); + add("Tottenham"); + add("Arsenal"); + add("Newcastle"); + }}; + String name = "Liverpool"; + + // Execute and verify + Assertions.assertTrue(cohortManager.search(cohorts, name)); + } + + @Test + public void searchForCohortsThatDoesNotExists(){ + // Setup + CohortManager cohortManager = new CohortManager(); + ArrayList cohorts = new ArrayList<>(){{ + add("Liverpool"); + add("Tottenham"); + add("Arsenal"); + add("Newcastle"); + }}; + String name = "Burnley"; + + // Execute and verify + Assertions.assertFalse(cohortManager.search(cohorts, name)); + } + }