From a089b3bf67491ca0faf07495442b88013b1ecd3b Mon Sep 17 00:00:00 2001 From: VeroniqueDM Date: Mon, 11 Sep 2023 11:22:10 +0200 Subject: [PATCH 1/5] added domain models --- EXERCISE1.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/EXERCISE1.md b/EXERCISE1.md index 3af2ea2..bc27ee9 100644 --- a/EXERCISE1.md +++ b/EXERCISE1.md @@ -24,7 +24,7 @@ Here is how one might design a domain model for the above user story: > **Time to analyse** > > Evaluate the user story and the domain model above. What assumptions did the developer have to make and what would you do differently? -> +> > Create your own domain model for the user story above, try to come up with a different solution than the model provided. You can use a table like the one above, a spreadsheet, pen and paper, whatever you'd like. Share your work in your cohorts classroom channel when you're done. ### Exercise @@ -36,6 +36,16 @@ 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. ``` +| Classes | Member Variables | Methods | Scenario | Outputs | +|----------|-------------------------|----------------------------------------------------|-------------------------------------|---------| +| | List Inventory | `sumBasket(HashMap basketItems)` | If list is empty | 0.00 | +| | | (key=product, value=amountPurchased) | If list is not empty | Double | +| | | | Partial check-out (returned items?) | Double | +| | | `addToBasket(Product product)` | If name is in inventory list | true | +| `Basket` | | | If name is not in inventory list | false | +| | | `removeFromBasket(Product product)` | If name is in buyer's list | true | +| | | | If name is not in buyer's list | false | + ``` As an organised individual, @@ -43,6 +53,11 @@ 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 | Member Variables | Methods | Scenario | Outputs | +|-------------|----------------------------------------|---------------|--------------------------------------------------------------|---------| +| `Receipt` | HashMap basketItems | `viewItems()` | If list is empty | 0.00 | +| | | | If list is not empty | Double | +| | | | Partial check-out (returned items? reimbursements? bonuses?) | Double | - 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 From 7f2fa554cf1ab2897eb925f53adb62f2d3cc70d9 Mon Sep 17 00:00:00 2001 From: VeroniqueDM Date: Mon, 11 Sep 2023 14:45:26 +0200 Subject: [PATCH 2/5] added solutions --- EXERCISE1.md | 6 +- EXERCISE2.md | 6 +- src/main/java/com/booleanuk/core/Basket.java | 28 +++++++++ .../java/com/booleanuk/core/BasketTest.java | 57 +++++++++++++++++++ 4 files changed, 92 insertions(+), 5 deletions(-) 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/EXERCISE1.md b/EXERCISE1.md index bc27ee9..4169c99 100644 --- a/EXERCISE1.md +++ b/EXERCISE1.md @@ -55,9 +55,9 @@ I bought as well as the quantity, and a total cost of my basket. ``` | Classes | Member Variables | Methods | Scenario | Outputs | |-------------|----------------------------------------|---------------|--------------------------------------------------------------|---------| -| `Receipt` | HashMap basketItems | `viewItems()` | If list is empty | 0.00 | -| | | | If list is not empty | Double | -| | | | Partial check-out (returned items? reimbursements? bonuses?) | Double | +| `Receipt` | HashMap basketItems | `viewItems()` | If list is empty | String | +| | | | If list is not empty | String | +| | | | Partial check-out (returned items? reimbursements? bonuses?) | String | - 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/EXERCISE2.md b/EXERCISE2.md index 2141e72..44adecd 100644 --- a/EXERCISE2.md +++ b/EXERCISE2.md @@ -1,10 +1,12 @@ # 2. Use the Red Green Refactor workflow to implement a solution -There is a class in `./src/main/java/com.booleanuk/code/CohortManager.java` and a test class in `./src/test/java/com.booleanuk/core/CohortManagerTest.java`. Your morning teacher will demonstrate the process of creating tests from the domain model below, and then using those tests to develop robust source code. +There is a class in `./src/main/java/com.booleanuk/code/CohortManager.java` and a test class in `./src/test/java/com.booleanuk/core/CohortManagerTest.java`. +Your morning teacher will demonstrate the process of creating tests from the domain model below, and then using those tests to develop robust source code. This is known as the **Red, Green, Refactor** workflow; an important discipline to practice. Simple to learn yet difficult to master. -When writing software using this Test Driven Development approach, we don't write a complete test or complete method at a time. We write *just enough* of a test for it to fail, then write *just enough* source code to make the test pass, then refactor until the test fails and repeat this cycle. +When writing software using this Test Driven Development approach, we don't write a complete test or complete method at a time. +We write *just enough* of a test for it to fail, then write *just enough* source code to make the test pass, then refactor until the test fails and repeat this cycle. What we are creating here are known as *unit tests*, tests that verify a single unit of our application is working correctly. They should be as small as possible, usually broken down into three parts: 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..a1ae330 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,28 @@ +package com.booleanuk.core; + +import java.util.HashMap; + +public class Basket { + public HashMap items; + + public Basket() { + this.items = new HashMap(); + } + + public boolean add(String product, int price) { +// if (this.items.containsKey(product)) { +// return false; +// } +// this.items.put(product,price); +// return true; + return this.items.putIfAbsent(product, price) == null; + } + + public int total() { + return this.items.values() + .stream() + .mapToInt(Integer::intValue) + .sum(); + + } +} 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..067daf3 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,57 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + @Test + public void testAddProductToBasket() { + Basket basket = new Basket(); + Assertions.assertTrue(basket.add("Apple", 2)); + Assertions.assertEquals(1, basket.items.size()); + Assertions.assertTrue(basket.items.containsKey("Apple")); + Assertions.assertEquals(2, basket.items.get("Apple").intValue()); + } + + @Test + public void testAddDuplicateProductToBasket() { + Basket basket = new Basket(); + basket.add("Banana", 3); + Assertions.assertFalse(basket.add("Banana", 2)); + Assertions.assertEquals(1, basket.items.size()); + Assertions.assertTrue(basket.items.containsKey("Banana")); + Assertions.assertEquals(3, basket.items.get("Banana").intValue()); + } + + @Test + public void testTotalWithEmptyBasket() { + Basket basket = new Basket(); + Assertions.assertEquals(0, basket.total()); + } + @Test + public void testAddMultipleProductsToBasket() { + Basket basket = new Basket(); + Assertions.assertTrue(basket.add("Orange", 4)); + Assertions.assertTrue(basket.add("Grapes", 5)); + Assertions.assertEquals(2, basket.items.size()); + } + @Test + public void testAddAndGetTotalWithEmptyBasket() { + Basket basket = new Basket(); + Assertions.assertEquals(0, basket.total()); + } + + @Test + public void testTotalWithProductsInBasket() { + Basket basket = new Basket(); + basket.add("Orange", 4); + basket.add("Grapes", 5); + Assertions.assertEquals(9, basket.total()); + } + @Test + public void testAddAndGetTotalWithNegativePrice() { + Basket basket = new Basket(); + basket.add("Chips", -2); + Assertions.assertEquals(-2, basket.total()); + } +} From 2510dc33d1ae5a6b1156757402f6f2f3e4e02995 Mon Sep 17 00:00:00 2001 From: VeroniqueDM Date: Mon, 11 Sep 2023 14:49:39 +0200 Subject: [PATCH 3/5] handled negative values --- src/main/java/com/booleanuk/core/Basket.java | 3 +++ src/test/java/com/booleanuk/core/BasketTest.java | 11 +++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index a1ae330..5b9a5e9 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -15,6 +15,9 @@ public boolean add(String product, int price) { // } // this.items.put(product,price); // return true; + if (price < 0) { + return false; + } return this.items.putIfAbsent(product, price) == null; } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 067daf3..42f0923 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -28,6 +28,7 @@ public void testTotalWithEmptyBasket() { Basket basket = new Basket(); Assertions.assertEquals(0, basket.total()); } + @Test public void testAddMultipleProductsToBasket() { Basket basket = new Basket(); @@ -35,6 +36,7 @@ public void testAddMultipleProductsToBasket() { Assertions.assertTrue(basket.add("Grapes", 5)); Assertions.assertEquals(2, basket.items.size()); } + @Test public void testAddAndGetTotalWithEmptyBasket() { Basket basket = new Basket(); @@ -48,10 +50,11 @@ public void testTotalWithProductsInBasket() { basket.add("Grapes", 5); Assertions.assertEquals(9, basket.total()); } + @Test - public void testAddAndGetTotalWithNegativePrice() { + public void testAddProductWithNegativePriceToBasket() { Basket basket = new Basket(); - basket.add("Chips", -2); - Assertions.assertEquals(-2, basket.total()); + Assertions.assertFalse(basket.add("Chips", -2)); // Negative price should not be accepted + Assertions.assertEquals(0, basket.items.size()); } -} +} \ No newline at end of file From 7faaad224ca92952938a6502026b0fe80edeec6d Mon Sep 17 00:00:00 2001 From: VeroniqueDM Date: Mon, 11 Sep 2023 14:55:17 +0200 Subject: [PATCH 4/5] updated tests --- src/main/java/com/booleanuk/core/Basket.java | 2 +- src/test/java/com/booleanuk/core/BasketTest.java | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 5b9a5e9..bd73bd3 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -15,7 +15,7 @@ public boolean add(String product, int price) { // } // this.items.put(product,price); // return true; - if (price < 0) { + if (price < 0 || product.strip().length()==0) { return false; } return this.items.putIfAbsent(product, price) == null; diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 42f0923..60587ff 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -23,12 +23,6 @@ public void testAddDuplicateProductToBasket() { Assertions.assertEquals(3, basket.items.get("Banana").intValue()); } - @Test - public void testTotalWithEmptyBasket() { - Basket basket = new Basket(); - Assertions.assertEquals(0, basket.total()); - } - @Test public void testAddMultipleProductsToBasket() { Basket basket = new Basket(); @@ -57,4 +51,10 @@ public void testAddProductWithNegativePriceToBasket() { Assertions.assertFalse(basket.add("Chips", -2)); // Negative price should not be accepted Assertions.assertEquals(0, basket.items.size()); } + @Test + public void testAddProductWithMissingNameInBasket() { + Basket basket = new Basket(); + Assertions.assertFalse(basket.add(" ", 2)); // Negative price should not be accepted + Assertions.assertEquals(0, basket.items.size()); + } } \ No newline at end of file From 08f3a7b0ab0f14112e3f03c4e642a048ccda7603 Mon Sep 17 00:00:00 2001 From: VeroniqueDM Date: Mon, 11 Sep 2023 15:05:30 +0200 Subject: [PATCH 5/5] refactored add method --- src/main/java/com/booleanuk/core/Basket.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index bd73bd3..3f62077 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -10,15 +10,8 @@ public Basket() { } public boolean add(String product, int price) { -// if (this.items.containsKey(product)) { -// return false; -// } -// this.items.put(product,price); -// return true; - if (price < 0 || product.strip().length()==0) { - return false; - } - return this.items.putIfAbsent(product, price) == null; + return price >= 0 && product.strip().length() != 0 + && this.items.putIfAbsent(product, price) == null; } public int total() {