From bc1947096dd814a7d29422c89cc5221727549031 Mon Sep 17 00:00:00 2001 From: Thomas Wiik Date: Wed, 14 Aug 2024 09:54:05 +0200 Subject: [PATCH 1/5] Added beautiful domain model --- EXERCISE1.md | 1 + domain-model.md | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 domain-model.md 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..f8c2530 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,56 @@ +# 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 | +|---------------------------------------------|----------------------------------------------------|-------------------------------------| +| `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 | Set basket.paymentCompleted to true | +| | 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 | From 6d55e0be5dd77dc1a9e381efc767d9142a98f0f1 Mon Sep 17 00:00:00 2001 From: Thomas Wiik Date: Wed, 14 Aug 2024 10:06:43 +0200 Subject: [PATCH 2/5] Refurbishing --- domain-model.md | 21 +++++++++++-------- .../com/booleanuk/core/CohortManager.java | 2 +- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/domain-model.md b/domain-model.md index f8c2530..92ccf19 100644 --- a/domain-model.md +++ b/domain-model.md @@ -45,12 +45,15 @@ I bought as well as the quantity, and a total cost of my basket. | Boolean paymentCompleted | Is true if the payment of the basket is completed. False by default. | -| Methods | Scenario | Outputs | -|---------------------------------------------|----------------------------------------------------|-------------------------------------| -| `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 | Set basket.paymentCompleted to true | -| | 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 | +| 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 | - | +| | If basket.paymentCompleted == false | Return error | +| `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 | Set basket.paymentCompleted to true | +| | 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/CohortManager.java b/src/main/java/com/booleanuk/core/CohortManager.java index 48a1b26..58a49bc 100644 --- a/src/main/java/com/booleanuk/core/CohortManager.java +++ b/src/main/java/com/booleanuk/core/CohortManager.java @@ -2,4 +2,4 @@ public class CohortManager { -} +} \ No newline at end of file From 26d156f73add06ad988a223798dab371bdab6e92 Mon Sep 17 00:00:00 2001 From: Thomas Wiik Date: Wed, 14 Aug 2024 10:07:52 +0200 Subject: [PATCH 3/5] Further refining --- domain-model.md | 1 - 1 file changed, 1 deletion(-) diff --git a/domain-model.md b/domain-model.md index 92ccf19..1188894 100644 --- a/domain-model.md +++ b/domain-model.md @@ -49,7 +49,6 @@ I bought as well as the quantity, and a total cost of my basket. |------------------------------------------------------|----------------------------------------------------|-------------------------------------| | `Void addItem(Product product, Integer quantity)` | Customer adds product(s) to basket | - | | `Void removeItem(Product product, Integer quantity)` | Customer removes product(s) from basket | - | -| | If basket.paymentCompleted == false | Return error | | `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 | From 0789eb36ae5914835ed6026d003e6e72c4530669 Mon Sep 17 00:00:00 2001 From: Thomas Wiik Date: Wed, 14 Aug 2024 10:09:52 +0200 Subject: [PATCH 4/5] Further refining --- domain-model.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/domain-model.md b/domain-model.md index 1188894..31f48b2 100644 --- a/domain-model.md +++ b/domain-model.md @@ -45,14 +45,14 @@ I bought as well as the quantity, and a total cost of my basket. | 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 | Set basket.paymentCompleted to true | -| | 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 | +| 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 | From 49b998612427c8212688b64f5299b4429b605b14 Mon Sep 17 00:00:00 2001 From: Thomas Wiik Date: Wed, 14 Aug 2024 11:01:33 +0200 Subject: [PATCH 5/5] Finished implementation of class and tests --- domain-model.md | 1 + src/main/java/com/booleanuk/core/Basket.java | 27 +++++++++++++++++++ .../com/booleanuk/core/CohortManager.java | 3 ++- .../java/com/booleanuk/core/BasketTest.java | 24 +++++++++++++++++ .../com/booleanuk/core/CohortManagerTest.java | 1 + 5 files changed, 55 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/domain-model.md b/domain-model.md index 31f48b2..d099e91 100644 --- a/domain-model.md +++ b/domain-model.md @@ -56,3 +56,4 @@ I bought as well as the quantity, and a total cost of my basket. | | 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 58a49bc..90323dc 100644 --- a/src/main/java/com/booleanuk/core/CohortManager.java +++ b/src/main/java/com/booleanuk/core/CohortManager.java @@ -2,4 +2,5 @@ public class CohortManager { -} \ No newline at end of file +} + 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 { + }