diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..0b37608 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,39 @@ + + +### Exercise + +Follow the same process as above to translate these two user stories into domain models. + +``` +As a supermarket shopper, +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. +``` + +``` +As an organised individual, +So that I can evaluate my shopping habits, +I'd like to see an itemised receipt that includes the name and price of the products +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. + +## Domain-model for shopping cart + +| Classes | Variables | Methods | Scenario | Outcome | +|--------------|-------------------------------------------------|----------------------------------|-------------------------------------------------------------------|------------------------------------------------------------------------------------------| +| `CartManager` | `private HashMap shoppingCart` | | | | +| | ` private int totalCost ` | | | | +| | `static HashMap itemCostMap` | | | | +| | | `addItem(String item)` | Shopper adds an item to cart | Item is added to cart. If it exists, increment quantity counter. totalCost is updated. | +| | | `addItem(String item, int x)` | Add x items to cart | x items is added to cart. update totalCost | +| | | `removeItem(String item, int x)` | Shopper removes x items from cart | x items are removed from cart. update totalCost | +| | | `removeItem(String item)` | Shopper removes an item from the cart | If item is in cart, decrement the quantity counter. totalCost is updated. | +| | | `removeAllOfItem(String item)` | Shopper removes all quantities of item from cart | Item entry is removed from the cart map. totalCost is updated. | +| | | `getTotalCost()` | Shopper wants to know the total cost | Shopper gets the total cost of items in the cart. | +| | | `getQuantityOfItem(String item)` | Shopper wants to know the quantity of an item in the cart | Shopper gets the quantity of a given item from the cart. | +| | | `getItemizedReciept()` | Shopper wants to see an overview of all items in cart and total cost | Shopper gets the total cost of all items, and a list of how many items and the per-price | + + 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..200f48d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,21 @@ +package com.booleanuk.core; + +import java.util.HashMap; + +public class Basket { + public HashMap items = new HashMap<>(); + + public boolean add(String product, int price){ + boolean alreadyInBasket = items.containsKey(product); + items.put(product, price); + return alreadyInBasket; + } + + public int total(){ + int total = 0; + for(int price: items.values()){ + total = total + price; + } + return total; + } +} diff --git a/src/main/java/com/booleanuk/core/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..0ff20f0 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 true; + } } 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..6409463 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,40 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + public Basket getTestBasket(){ + return new Basket(); + } + + + @Test + public void testBasketConstructor(){ + Basket basket = getTestBasket(); + } + + @Test + void testadd(){ + Basket basket = getTestBasket(); + basket.add("Banana", 10); + basket.add("Coca Cola", 20); + Assertions.assertTrue(basket.items.containsKey("Banana")); + Assertions.assertTrue(basket.items.containsKey("Coca Cola")); + Assertions.assertFalse(basket.items.containsKey("Unknown item")); + } + + @Test + void testTotal(){ + Basket basket = getTestBasket(); + basket.add("Banana", 10); + basket.add("Coca Cola", 20); + + Assertions.assertEquals(basket.total(), 30); + + Assertions.assertNotEquals(basket.total(), 45); // should not be 45 yet + basket.add("kaptein sabeltann is", 15); + Assertions.assertEquals(basket.total(), 45); // should now be 45 + + } +} diff --git a/src/test/java/com/booleanuk/core/CohortManagerTest.java b/src/test/java/com/booleanuk/core/CohortManagerTest.java index 5dea868..57ccd35 100644 --- a/src/test/java/com/booleanuk/core/CohortManagerTest.java +++ b/src/test/java/com/booleanuk/core/CohortManagerTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -class CohortManagerTest { +import java.lang.reflect.Method; +class CohortManagerTest { }