diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..355f640 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,35 @@ +# Domain Model + +## CohortManager Class + +| Method | Member Variables | Scenario | Result | +|------------------------------|------------------------------|---------------------------|--------------| +| | ArrayList cohortList | | | +| searchForCohort(String name) | | Name is in cohortList | Return true | +| | | Name is not in cohortList | Return false | + + +1. + +## SupermarketCashier Class + +| Method | Member Variables | Scenario | Result | +|------------------------------------------|------------------|---------------------------------------------------|------------------| +| | int totalCost | | | +| calculateCost(ArrayList groceries) | | Calculates the accumulated value of the groceries | Return totalCost | +| | | List is null | Return -1 | + + +2. +| Method | Member Variables | Scenario | Result | +|-----------------------------------------|--------------------|--------------------------------------------------------------------------|--------| +| | Receipt newReceipt | | | +| printReceipt(ArrayList groceries) | | Creates a receipt with createReceipt and prints the data to the terminal | | +| | | Is unable to create a receipt. Prints error-message to the terminal | | + +| Method | Member Variables | Scenario | Result | +|------------------------------------------|--------------------|---------------------------------------------------------------------|-------------------| +| | Receipt newReceipt | | | +| createReceipt(ArrayList groceries) | | Creates a new receipt with the requested information and returns it | Return newReceipt | +| | | List is null | Return null | + 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..403871b --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,19 @@ +package com.booleanuk.core; + +import java.util.HashMap; + +public class Basket { + HashMap items = new HashMap<>(); + + public boolean add(String product, int price) { + return items.put(product, price) == null; + } + + public int total() { + int totalCost = 0; + for(String key : items.keySet()) { + totalCost += items.get(key); + } + return totalCost; + } +} diff --git a/src/main/java/com/booleanuk/core/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..24f9699 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 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..b11dfac --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,38 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + @Test + public void addItemThatDoesNotExist() { + Basket basket = new Basket(); + Assertions.assertTrue(basket.add("milk", 2)); + } + + @Test + public void addItemThatAlreadyExists() { + Basket basket = new Basket(); + basket.items.put("butter", 4); + Assertions.assertFalse(basket.add("butter", 4)); + } + + @Test + public void getCorrectTotalCost() { + Basket basket = new Basket(); + basket.items.put("butter", 4); + basket.items.put("milk", 2); + basket.items.put("coffee", 7); + Assertions.assertEquals(13, basket.total()); + } + + @Test + public void getWrongTotalCost() { + Basket basket = new Basket(); + basket.items.put("butter", 4); + basket.items.put("milk", 2); + basket.items.put("coffee", 7); + Assertions.assertNotEquals(10, basket.total()); + } +} diff --git a/src/test/java/com/booleanuk/core/CohortManagerTest.java b/src/test/java/com/booleanuk/core/CohortManagerTest.java index 5dea868..98a2ecd 100644 --- a/src/test/java/com/booleanuk/core/CohortManagerTest.java +++ b/src/test/java/com/booleanuk/core/CohortManagerTest.java @@ -3,6 +3,41 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; + class CohortManagerTest { + @Test + public void searchForCohortThatExists() { + // 1. Setup + CohortManager cohortManager = new CohortManager(); + ArrayList cohorts = new ArrayList<>() {{ + add("JAEX1"); + add("JAEX2"); + add("JAEX3"); + add("JAEX4"); + add("JAEX5"); + }}; + String name = "JAEX5"; + + // 2. Execute / 3. Verify + Assertions.assertTrue(cohortManager.search(cohorts, name)); + } + + @Test + public void searchForCohortThatDoesNotExist() { + // 1. Setup + CohortManager cohortManager = new CohortManager(); + ArrayList cohorts = new ArrayList<>() {{ + add("JAEX1"); + add("JAEX2"); + add("JAEX3"); + add("JAEX4"); + add("JAEX5"); + }}; + String name = "JAEX6"; + + // 2. Execute / 3. Verify + Assertions.assertFalse(cohortManager.search(cohorts, name)); + } }