diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 00000000..3e0bfaa3 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,18 @@ + + +| Classes | Methods | Scenario | Outputs | +|----------|-------------------------|------------------------------------------------------------------|---------| +| `Basket` | `addBagel(String bagel` | `If the begal is not already in the basket` | `true` | +| | | `If the bagel is already in the basket` | `false` | +| | | `If the basket is full` | `false` | +| | | | | +| | `removeBagel()` | `Removes the bagel from my basket if the bagel is in the basket` | `True` | +| | | `Removes the bagel from my basket if the bagel is not in basket` | `False` | +| | | | | +| | `Basket(int capacity)` | `Setting the capacity of the basket` | `int` | +| | | | | +| | `CheckDuplicates` | `If bagel is in already in the basket` | `True` | +| | | `If basket is not in the basket` | `False` | +| | | | | +| | `isFull()` | `If the basket is full` | `True` | +| | | `If the basket is not full` | `False` | diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..d02687bd 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,52 @@ package com.booleanuk.core; +import java.util.ArrayList; + public class Basket { + private ArrayList items; + private int capacity; + + + public Basket(int capacity) { + this.items = new ArrayList<>(); + this.capacity = capacity; + } + + + public boolean isFull() { + return items.size() >=capacity; + + } + + + public boolean addBagel(String bagel){ + if (isFull()) { + return false; + } + + if (items.contains(bagel)){ + return false; + } + + items.add(bagel); + return true; + } + + public boolean removeBagel(String bagel){ + if (items.contains(bagel)) { + items.remove(bagel); + return true; + } + + return false; + } + + + public boolean checkDuplicates(String bagel) { + return items.contains(bagel); + } + } + + diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index e35771b3..165f68b0 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -3,6 +3,54 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; + class BasketTest { + + @Test + public void testAddBagelToBasketIfNotFull(){ + Basket basket = new Basket(10); + Assertions.assertTrue(basket.addBagel("Crispy Bagel")); + } + + + + @Test + public void testAddBagelToBasketIfFull(){ + Basket basket = new Basket(1); + basket.addBagel("Plain Bagel"); + boolean result = basket.addBagel("Crispy Bagel"); + Assertions.assertFalse(result); + } + + @Test + public void testRemoveExistingBagelFromBasket() { + Basket basket = new Basket(5); + basket.addBagel("Plain Bagel"); + basket.addBagel("Crispy Bagel"); + boolean result = basket.removeBagel("Plain Bagel"); + Assertions.assertTrue(result); + + } + + @Test + public void testRemoveNonExistingBagelFromBasket() { + Basket basket = new Basket(5); + basket.addBagel("Plain Bagel"); + basket.addBagel("Crispy Bagel"); + boolean result = basket.removeBagel("Cheese Bagel"); + Assertions.assertFalse(result); + + } + + @Test + public void testShouldNotAddDuplicateBagel() { + Basket basket = new Basket(10); + basket.addBagel("Plain Bagel"); + boolean result = basket.checkDuplicates("Plain Bagel"); + Assertions.assertTrue(result); + } + + }