diff --git a/EXERCISE1.md b/EXERCISE1.md index 3af2ea2..d4e4a22 100644 --- a/EXERCISE1.md +++ b/EXERCISE1.md @@ -44,5 +44,6 @@ 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 diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 0000000..d099e91 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,59 @@ +# Domain model + + +* [Domain model](#domain-model) + * [User stories](#user-stories) + * [User story 1](#user-story-1) + * [User story 2](#user-story-2) + * [Classes](#classes) + * [Product Class](#product-class) + * [Shopping Basket Class](#shopping-basket-class) + + +## User stories +### User story 1 +``` +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. +``` + +### User story 2 +``` +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. +``` + +## Classes + +### Product Class +| Variables | Description | +|--------------------|----------------------------| +| String productID | ID of the product | +| String productName | Name of the product | +| String description | Description of the product | +| Float price | Price of the product | + + +### Shopping Basket Class +| Variables | Description | +|----------------------------------------------|----------------------------------------------------------------------| +| List basket | Contains all the products in the user's basket | + | String customerID | Contains the ID of the customer | + | Boolean paymentCompleted | Is true if the payment of the basket is completed. False by default. | + + +| Methods | Scenario | Outputs | +|------------------------------------------------------|----------------------------------------------------|-------------------------| +| `Void addItem(Product product, Integer quantity)` | Customer adds product(s) to basket | - | +| `Void removeItem(Product product, Integer quantity)` | Customer removes product(s) from basket | - | +| `Float summarizeBasket(Basket basket)` | If basket is empty | Return 0 | +| | If basket is not empty | Return sum | +| `Void completePayment(Basket basket)` | If basket is empty | Return error | +| | If basket is not empty and payment is successful | - | +| | If basket is not empty and payment is unsuccessful | Return error | +| `String createReceipt(Basket basket)` | If basket.paymentCompleted == true | Return itemised receipt | +| | If basket.paymentCompleted == false | Return error | + 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..02543f4 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,27 @@ +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.containsKey(product)) + return false; + + items.put(product, price); + return true; + } + + public int total(){ + int sum = 0; + + for (Map.Entry item : items.entrySet()) { + sum += item.getValue(); + } + return sum; + } + +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/core/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..90323dc 100644 --- a/src/main/java/com/booleanuk/core/CohortManager.java +++ b/src/main/java/com/booleanuk/core/CohortManager.java @@ -3,3 +3,4 @@ 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..0373599 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,24 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + @Test + public void testAdd() { + Basket basket = new Basket(); + basket.add("Banana", 5); + Assertions.assertFalse(basket.add("Banana", 5)); + Assertions.assertTrue(basket.add("Pear", 3)); + } + + @Test + public void testTotal() { + Basket basket = new Basket(); + basket.add("Banana", 5); + basket.add("Apple", 4); + basket.add("Pear", 3); + Assertions.assertEquals(12, basket.total()); + } +} diff --git a/src/test/java/com/booleanuk/core/CohortManagerTest.java b/src/test/java/com/booleanuk/core/CohortManagerTest.java index 5dea868..ab1cfaf 100644 --- a/src/test/java/com/booleanuk/core/CohortManagerTest.java +++ b/src/test/java/com/booleanuk/core/CohortManagerTest.java @@ -5,4 +5,5 @@ class CohortManagerTest { + }