diff --git a/DOMAIN_MODEL.md b/DOMAIN_MODEL.md new file mode 100644 index 00000000..031e3e5a --- /dev/null +++ b/DOMAIN_MODEL.md @@ -0,0 +1,11 @@ +| Classes | Variables | Methods | Scenario | Outcome | +|----------|----------------------------|------------------------------------------------|-----------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------| +| 'Basket' | 'ArrayList basket' | 'addBagel(String type)' | Add bagel to not full basket. | Return 'true' | +| | | | Tried to add bagel to full basket. | Return 'false' and print "Basket is full" | +| | | 'removeBagel(String type)' | Remove bagel when bagel with 'type' exists and basket is not empty | Return 'true' | +| | | | Tried to remove bagel with 'type' that do not exist in basket | Return 'false' and print "No such bagel exists in your basket" | +| | | | Tried to remove bagel from empty basket | Return 'false' and print "Basket is empty" | +| | 'int capacity' | 'setCapacity(int capacity, boolean isManager)' | Tried to set the basket capacity to a number smaller than the number of bagels the basket is currently holding. | Return 'false' and print "Basket contains to many bagels. Please empty the basket first" | +| | | | Set the capacity to a non-problematic number | Return 'true' | +| | | | Set capacity to a non-positive number. | Return 'false' and print "Capacity must be a positive number" | +| | | | Tried to set capacity without being Bob | Return 'false' and print "You're not Bob" | diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..151cd8d9 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,59 @@ package com.booleanuk.core; +import java.util.ArrayList; + public class Basket { + public int capacity; + public ArrayList basket; + + public Basket(int capacity) { + this.basket = new ArrayList<>(); + if (capacity > 0) { + this.capacity = capacity; + } else { + this.capacity = 1; + } + } + + public boolean addBagel(String type){ + if (this.basket.size() < this.capacity) { + this.basket.add(type); + return true; + } + System.out.println("Basket is full"); + return false; + } + + public boolean removeBagel(String type) { + if (this.basket.isEmpty()) { + System.out.println("Basket is empty"); + return false; + } else if (this.basket.contains(type)) { + // Remove only one of type if multiple exists + for (int i = 0; i < this.basket.size(); i++) { + if (this.basket.get(i).equals(type)) { + this.basket.remove(i); + return true; + } + } + } + System.out.println("No such bagel exists in your basket"); + return false; + } + public boolean setCapacity(int capacity, boolean isBob) { + if (!isBob) { + System.out.println("You're not Bob"); + return false; + } + if (capacity < 1) { + System.out.println("Capacity must be a positive number"); + return false; + } else if (capacity < basket.size()) { + System.out.println("Basket contains to many bagels. Please empty the basket first"); + return false; + } + this.capacity = capacity; + return true; + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index e35771b3..83b0357b 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -1,8 +1,87 @@ package com.booleanuk.core; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + class BasketTest { + @Test + public void testCreateBasket() { + // Test so that you can only initiate a basket with a positive number. + Basket basket1 = new Basket(1); + Basket basketM1 = new Basket(-1); + Basket basket5 = new Basket(5); + + assertEquals(1, basket1.capacity); + assertEquals(1, basketM1.capacity); + assertEquals(5, basket5.capacity); + } + + @Test + public void testAddToBasket() { + // Test if you can add more bagels to basket than the capacity + Basket basket = new Basket(2); + + boolean b1 = basket.addBagel("1"); + boolean b2 = basket.addBagel("2"); + boolean b3 = basket.addBagel("3"); + + assertTrue(b1); + assertTrue(b2); + assertFalse(b3); + } + + + @Test + public void testRemoveFromBasket() { + Basket basket = new Basket(2); + + // Test removal from empty basket + boolean b1 = basket.removeBagel("1"); + assertFalse(b1); + + // Test removal of the same bagel twice + basket.addBagel("1"); + boolean b2 = basket.removeBagel("1"); + boolean b3 = basket.removeBagel("1"); + + assertTrue(b2); + assertFalse(b3); + + // Test removal of non-existent bagel + basket.addBagel("1"); + boolean b4 = basket.removeBagel("2"); + assertFalse(b4); + + // Test that only one bagel is removed at a time + basket.addBagel("1"); + basket.addBagel("1"); + boolean b5 = basket.removeBagel("1"); + boolean b6 = basket.removeBagel("1"); + assertTrue(b5); + assertTrue(b6); + } + + @Test + public void testSetCapacity() { + // Test that capacity changes + Basket basket = new Basket(2); + basket.setCapacity(3, true); + assertEquals(3, basket.capacity); + + // Test that capacity needs to be positive + boolean b1 = basket.setCapacity(-1, true); + assertFalse(b1); + + // Test that capacity cant be lower than the current amount of bagels in basket + basket.addBagel("1"); + basket.addBagel("2"); + boolean b2 = basket.setCapacity(1, true); + assertFalse(b2); + + // Test that you need to be Bob + boolean b3 = basket.setCapacity(5, false); + assertFalse(b3); + } }