diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 00000000..837a3185 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,57 @@ +## Domain Model for User Stories + +``` +1. +As a member of the public, +So I can order a bagel before work, +I'd like to add a specific type of bagel to my basket. +``` + +``` +2. +As a member of the public, +So I can change my order, +I'd like to remove a bagel from my basket. +``` + +``` +3. +As a member of the public, +So that I can not overfill my small bagel basket +I'd like to know when my basket is full when I try adding an item beyond my basket capacity. +``` + +``` +4. +As a Bob's Bagels manager, +So that I can expand my business, +I’d like to change the capacity of baskets. +``` + +``` +5. +As a member of the public +So that I can maintain my sanity +I'd like to know if I try to remove an item that doesn't exist in my basket. +``` + +The following class will be used in all the user stories. + +| Class | Members | Types | +|----------|---------|------------------------| +| `Basket` | `items` | `Map` | +| | `max` | `Integer` | + + +| User Story | Methods | Return | Notes | +|------------|---------------------------------------|-----------|---------------------------------------------| +| 1 | `addBagel(String type, int quantity)` | `void` | | +| 2 | `removeBagel(String type)` | `void` | | +| 3 | `hasCapacity()` | `boolean` | Max limit (5) | +| 4 | `changeBasketCapacity(int limit)` | `void` | | +| 5 | `removeBagel(String type)` | `boolean` | Updated method in story 2 to return boolean | + + + + + diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..c44659a5 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,40 @@ package com.booleanuk.core; +import java.util.HashMap; +import java.util.Map; + public class Basket { + public Map items; + public int max = 5; + + public Basket() { + this.items = new HashMap<>(); + } + + public void addBagel(String type, int quantity) { + this.items.put(type, quantity); + } + + public boolean removeBagel(String key) { + if (items.get(key) != null) { + items.remove(key); + return true; + } + else { + return false; + } + } + + public boolean hasCapacity() { + int totalItemsInBasket = 0; + for (Map.Entry item : items.entrySet()) { + totalItemsInBasket += item.getValue(); + } + return totalItemsInBasket < max; + } + + public void changeBasketCapacity(int limit) { + this.max = limit; + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index e35771b3..0a8dd211 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -5,4 +5,54 @@ class BasketTest { + @Test + public void testAddBagelAddsBagelToBasket() { + Basket basket = new Basket(); + basket.addBagel("normal", 1); + + Assertions.assertFalse(basket.items.isEmpty()); + } + + @Test + public void testRemoveBagelRemovesBagelFromBasket() { + Basket basket = new Basket(); + basket.addBagel("normal", 1); + + // Then we remove the bagel. + basket.removeBagel("normal"); + + Assertions.assertTrue(basket.items.isEmpty()); + } + + @Test + public void testHasCapacity() { + Basket basket = new Basket(); + basket.addBagel("normal", 1); + basket.addBagel("special", 2); + + // Check if it returns true when not full. + boolean hasMoreSpace = basket.hasCapacity(); + Assertions.assertTrue(hasMoreSpace); + + // Check if it returns false when full. + basket.addBagel("tasteless", 3); + Assertions.assertFalse(basket.hasCapacity()); + } + + @Test + public void changeBasketCapacity() { + Basket basket = new Basket(); + basket.changeBasketCapacity(10); + + Assertions.assertEquals(10, basket.max); + } + + @Test + public void testRemoveBagelFromBasketIndicatesIfItemExists() { + Basket basket = new Basket(); + basket.addBagel("normal", 1); + boolean itemDoesNotExists = basket.removeBagel("special"); + + Assertions.assertFalse(itemDoesNotExists); + } }