diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 00000000..e5a76eb6 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,39 @@ +# Domain model Bobs Bagels + +### class Basket +``` +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. +``` + +| Variables | Methods | Scenario | Output | +|--------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------| +| `HashMap Basket`
`HashMap Bagel` | `add(String bagelType, double price)`
`remove(String bagelType)`
`exists(String bagelType)` | if there is capacity, adds bagel to basket, else basket is full
remove bagel only, if bagel exists in basket
| "Added to basket."
"Basket is full."
" is not in basket, can't remove. "
Removed
from basket." | +| `HashMap basketCapacity` | `changeCap(int cap)`
`isManager(int passWord)` | if managercode is correct, change capacity.
| "Password correct, capacity is updated!"
"Password inccorect, capacity is not updated!" | + diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..3c437492 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,66 @@ package com.booleanuk.core; +import java.util.HashMap; +import java.util.Map; +import java.util.function.BooleanSupplier; + public class Basket { + private Map items; + private int capacity; + private int managerCode = 1234; + + public Basket() { + this.items = new HashMap<>(); + this.capacity = 6; + } + + public void add(String bagel, double price) { + if (!isFull()); + { + items.put(bagel, price); + System.out.println("Added " + bagel +" to basket"); + } + System.out.println("Basket is full."); + } + public boolean isFull() { + return items.size() >= capacity; + } + + public boolean isNotEmpty(String bagel){ + if (items.containsKey(bagel)) { + return true; + } else { + return false; + } + } + public void remove(String bagel) { + if (items.containsKey(bagel)) { + items.remove(bagel); + System.out.println("Removed " + bagel + " from basket."); + } System.out.println(bagel + " not in basket."); + } + public Map getItems() { + return items; + } + + public boolean isManager(int password) { + return password == managerCode; + } + + public int changeCap(int newCapacity, int password) { + if (isManager(password)) { + this.capacity = newCapacity; + System.out.println("Password correct, capacity is updated!"); + return newCapacity; + } else { + System.out.println("Password incorrect, capacity is not updated!"); + return this.capacity; // Return the current capacity if the password is incorrect + } + } + + public int getCapacity() { + return capacity; + } } + diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index e35771b3..bc0d247c 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -4,5 +4,84 @@ import org.junit.jupiter.api.Test; class BasketTest { + @Test + public void addBagelToBasket() { + Basket basket = new Basket(); + String sesame = "Sesame Bagel"; + double price = 4.50; + basket.add(sesame, price); + Assertions.assertFalse(basket.isFull()); + Assertions.assertEquals(basket.getItems().get(sesame), price); + } + @Test + public void checkIfBasketIsFull(){ + Basket basket = new Basket(); + String sesame = "Sesame Bagel"; + double price1 = 4.50; + String redvelvet = "Everything Bagel"; + double price2 = 5.50; + String jalapenoCheese = "jalapenoCheese Bagel"; + double price3 = 5.50; + String cinnamon = "Cinnamon Bagel"; + double price4 = 4.50; + String flaxseed = "Flaxseed Bagel"; + double price5 = 3.50; + String oats = "Oat Bagel"; + double price6 = 5.50; + String whole = "WholeGrain Bagel"; + double price7 = 6.50; + + basket.add(sesame, price1); + basket.add(redvelvet, price2); + basket.add(jalapenoCheese, price3); + basket.add(cinnamon, price4); + basket.add(flaxseed, price5); + basket.add(oats, price6); + basket.add(whole, price7); + + Assertions.assertTrue(basket.isFull()); + } + @Test + public void removeBagelFromBasket(){ + Basket basket = new Basket(); + String sesame = "Sesame Bagel"; + double price = 4.50; + + basket.add(sesame, price); + Assertions.assertTrue(basket.isNotEmpty(sesame)); + basket.remove(sesame); + Assertions.assertFalse(basket.isNotEmpty(sesame)); + } + @Test + public void existsBagelInCart(){ + Basket basket = new Basket(); + String sesame = "Sesame Bagel"; + double price1 = 4.50; + String jalapenoCheese = "Jalapeno Cheese Bagel"; + double price2 = 5.50; + + basket.add(sesame, price1); + Assertions.assertTrue(basket.isNotEmpty(sesame)); + Assertions.assertFalse(basket.isNotEmpty(jalapenoCheese)); + + + } + @Test + public void isAuthorizedToChangeCap(){ + Basket basket = new Basket(); + int managerCode = 1234; + + Assertions.assertTrue(basket.isManager(managerCode)); + } + + @Test + public void capacityIsChanged(){ + Basket basket = new Basket(); + int newCapacity = 10; + int managerCode = 1234; + basket.changeCap(newCapacity, managerCode); + + Assertions.assertEquals(newCapacity, basket.getCapacity()); + } }