From 2120b23f773a59fc297bb4b53d563ef2a55bb5c0 Mon Sep 17 00:00:00 2001 From: TMajlu Date: Wed, 14 Aug 2024 14:34:30 +0200 Subject: [PATCH 1/4] Finished the core exercises. --- domain.md | 11 ++++ src/main/java/com/booleanuk/core/Basket.java | 56 ++++++++++++++++ .../java/com/booleanuk/core/BasketTest.java | 65 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 domain.md diff --git a/domain.md b/domain.md new file mode 100644 index 00000000..976892ed --- /dev/null +++ b/domain.md @@ -0,0 +1,11 @@ +| `Classes` | `Member` | `Methods` | `Scenario` | `Outcome` | +|-----------|-----------------------------------|---------------------------------|--------------------------|-------------------------------------------------| +| `Basket` | `Hashmap bagels` | `add(String bagel, int num)` | Bagel-key already exists | Return true, Bagel number is incremented by num | +| | `int capacity` | | Bagel-key does not exist | Return true, Bagel key is created with value | +| | | | Basket is full | Return false +| | | `remove(String bagel, int num)` | Bagel-key exists | Return true, Bagel value is decremented by num | +| | | | Bagel-key does not exist | Return false | +| | | `checkCapacity()` | Basket is full | Return false | +| | | | Basket is not full | Return true | +| | | `incCapacity(int num)` | Capacity value exists | Capacity increases by num | + diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..2f055991 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,61 @@ package com.booleanuk.core; +import java.util.HashMap; public class Basket { + HashMap bagels = new HashMap(); + int capacity; + public Basket(int num){ + capacity=num; + } + + public boolean add(String bagel, int num){ + if (!(bagels.containsKey(bagel) & checkCapacity())){ + bagels.put(bagel, 0); + } + + for (int i=0; i=capacity){ + return false; + } + } + return true; + + } + + public boolean incCapacity(int num){ + capacity+=num; + return true; + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index e35771b3..2be8afbd 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -4,5 +4,70 @@ import org.junit.jupiter.api.Test; class BasketTest { + public BasketTest(){ + + } + + @Test + public void testAddExists(){ + Basket basket= new Basket(10); + Assertions.assertTrue(basket.add("Nice bagel", 5)); + } + + @Test + public void testRemoveExists(){ + Basket basket= new Basket(10); + Assertions.assertFalse(basket.remove("Nice bagel", 5)); + } + + @Test + public void testCapacityExists(){ + Basket basket= new Basket(10); + Assertions.assertTrue(basket.checkCapacity()); + } + + @Test + public void testIncCapacityExists(){ + Basket basket= new Basket(10); + Assertions.assertTrue(basket.incCapacity(10)); + } + + @Test + public void testAdd(){ + Basket basket= new Basket(10); + Assertions.assertTrue(basket.add("Nice bagel", 5)); + Assertions.assertTrue(basket.add("Nice bagel", 5)); + } + + @Test + public void testRemove(){ + Basket basket= new Basket(10); + basket.add("Nice bagel", 5); + Assertions.assertTrue(basket.remove("Nice bagel", 1)); + Assertions.assertFalse(basket.remove("Fine bagel", 1)); + } + + @Test + public void testCheckCapacity(){ + Basket basket= new Basket(10); + basket.add("Nice bagel", 5); + basket.add("Nice bagel", 5); + Assertions.assertFalse(basket.checkCapacity()); + } + + @Test + public void testIncCapacity(){ + Basket basket= new Basket(10); + Assertions.assertTrue(basket.incCapacity(5)); + } + + @Test + public void testAddingtoFull(){ + Basket basket= new Basket(10); + basket.add("Nice bagel", 9); + Assertions.assertFalse(basket.add("Nice bagel", 2)); + } + + } From 63de05e27e26f3c64759aeb80660c0ccaa669c60 Mon Sep 17 00:00:00 2001 From: TMajlu Date: Wed, 14 Aug 2024 14:38:19 +0200 Subject: [PATCH 2/4] Finished the core exercises. --- src/test/java/com/booleanuk/core/BasketTest.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 2be8afbd..50d28481 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -47,6 +47,13 @@ public void testRemove(){ Assertions.assertFalse(basket.remove("Fine bagel", 1)); } + @Test + public void testAddingtoFull(){ + Basket basket= new Basket(10); + basket.add("Nice bagel", 9); + Assertions.assertFalse(basket.add("Nice bagel", 2)); + } + @Test public void testCheckCapacity(){ Basket basket= new Basket(10); @@ -61,12 +68,7 @@ public void testIncCapacity(){ Assertions.assertTrue(basket.incCapacity(5)); } - @Test - public void testAddingtoFull(){ - Basket basket= new Basket(10); - basket.add("Nice bagel", 9); - Assertions.assertFalse(basket.add("Nice bagel", 2)); - } + From b031fdac602a4b03d79d2c3ed0d7521d58831fff Mon Sep 17 00:00:00 2001 From: TMajlu Date: Wed, 14 Aug 2024 14:43:29 +0200 Subject: [PATCH 3/4] Finished the core exercises. --- src/test/java/com/booleanuk/core/BasketTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 50d28481..7dfecce8 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -20,18 +20,21 @@ public void testRemoveExists(){ Assertions.assertFalse(basket.remove("Nice bagel", 5)); } + @Test public void testCapacityExists(){ Basket basket= new Basket(10); Assertions.assertTrue(basket.checkCapacity()); } + @Test public void testIncCapacityExists(){ Basket basket= new Basket(10); Assertions.assertTrue(basket.incCapacity(10)); } + // testing User story 1 @Test public void testAdd(){ Basket basket= new Basket(10); @@ -39,6 +42,7 @@ public void testAdd(){ Assertions.assertTrue(basket.add("Nice bagel", 5)); } + // testing User story 2 & 5 @Test public void testRemove(){ Basket basket= new Basket(10); @@ -47,6 +51,7 @@ public void testRemove(){ Assertions.assertFalse(basket.remove("Fine bagel", 1)); } + // testing User story 3 @Test public void testAddingtoFull(){ Basket basket= new Basket(10); @@ -54,6 +59,7 @@ public void testAddingtoFull(){ Assertions.assertFalse(basket.add("Nice bagel", 2)); } + // testing User story 3 @Test public void testCheckCapacity(){ Basket basket= new Basket(10); @@ -62,6 +68,7 @@ public void testCheckCapacity(){ Assertions.assertFalse(basket.checkCapacity()); } + // testing User story 4 @Test public void testIncCapacity(){ Basket basket= new Basket(10); From 4044ae89b184844682eb5d5b8a5f7a9a274c0408 Mon Sep 17 00:00:00 2001 From: TMajlu Date: Wed, 14 Aug 2024 15:54:58 +0200 Subject: [PATCH 4/4] Finished the core exercises. --- src/main/java/com/booleanuk/core/Basket.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 2f055991..f3414853 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -32,6 +32,11 @@ public boolean add(String bagel, int num){ public boolean remove(String bagel, int num){ if (bagels.containsKey(bagel)){ + if (bagels.get(bagel)==0){ + System.out.println("The basket does not contain this bagle type"); + return false; + } + bagels.replace(bagel, bagels.get(bagel)-num); return true; }