diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index df7a20aa..899e592d 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,74 @@ package com.booleanuk.core; + +import java.util.HashMap; + public class Basket { + // TODO: Difference between ArrayList and simple List?? + // TODO: Should this be private variables? + HashMap products; + int maxCapacity; + int size = 0; + + public Basket() { + this.products = new HashMap<>(); + this.maxCapacity = 3; + } + + public boolean add(String productName) { + // Check if max capacity + if (this.size >= maxCapacity) { + System.out.println("Basket is full, can't add more items."); + return false; + } + + // List properties + int numOfProducts; + + // Update numOfProducts if already exist, otherwise just add + if (products.containsKey(productName)) { + numOfProducts = products.get(productName) + 1; + } else { + numOfProducts = 1; + } + + // Add product + products.put(productName, numOfProducts); + + // Update size + size++; + + return true; + } + + public boolean remove(String productName) { + + // Check if product does not exist + if (!this.products.containsKey(productName)) { + System.out.println("Product does not exist. Can't remove."); + return false; + } + + // Update quantity if already in list, remove if it is the last product in this type/category + int newNumOfProducts = this.products.get(productName) - 1; + if (newNumOfProducts == 0) { + this.products.remove(productName); + } else { + this.products.put(productName, newNumOfProducts); + } + + // Update size + size--; + + return true; + } + public boolean changeCapacity(int newMaxCapacity) { + if (newMaxCapacity < 1) { + System.out.println("Invalid max capacity. Can't be less than 1"); + return false; + } + this.maxCapacity = newMaxCapacity; + return true; + } } diff --git a/src/main/java/com/booleanuk/core/Domain.md b/src/main/java/com/booleanuk/core/Domain.md new file mode 100644 index 00000000..d0a2b75d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Domain.md @@ -0,0 +1,9 @@ +| Classes | Variables | Methods | Scenario | Outcome | +|--------------------------------|---------------------------------------|-----------------------------------|-----------------------------------------------|-----------------------------------| +| `Basket` | `HashMap products` | `add(String productName)` | Item succesfully added. | true | +| | | | Basket is full. Item could not be added. | false, Error: Basket is full | +| | | `remove(String productName)` | Item is in basket and can be removed. | true | +| | | | Item is not in basket and can not be removed. | false, Error: Item does not exist | +| | `int maxCapacity` | `changeCapacity(int newCapacity)` | If newCapacity is more than 0. | true | +| | | | If newCapacity is less than 1. | false | + diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index e35771b3..2a85783c 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -3,6 +3,53 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.HashMap; + class BasketTest { + // User story 1. + @Test + public void add() { + Basket basket = new Basket(); + Assertions.assertTrue(basket.add("Unicorn Bagel")); + Assertions.assertEquals(1, basket.size); + + basket.add("Unicorn Bagel"); + Assertions.assertEquals(2, basket.products.get("Unicorn Bagel")); // Index 0 is numOfProducts + + basket.add("Sunhaven Bagel"); + Assertions.assertFalse(basket.add("Moon Bagel")); // Exceed maxCapacity + } + + @Test + public void remove() { + Basket basket = new Basket(); + basket.add("Small Bagel"); + basket.add("Small Bagel"); + + // Remove first item, 1 left + Assertions.assertTrue(basket.remove("Small Bagel")); + Assertions.assertEquals(1, basket.products.get("Small Bagel")); + + // Remove second item, 0 left (should be removed from list) + Assertions.assertTrue(basket.remove("Small Bagel")); + Assertions.assertFalse(basket.products.containsKey("Small Bagel")); + + // Remove not existing item + Assertions.assertFalse(basket.remove("Small Bagel")); + } + + @Test + public void changeCapacity() { + Basket basket = new Basket(); + + // Change to valid max capacity + Assertions.assertTrue(basket.changeCapacity(5)); + Assertions.assertEquals(5, basket.maxCapacity); + + // Change to invalid max capacity + Assertions.assertFalse(basket.changeCapacity(0)); + Assertions.assertFalse(basket.changeCapacity(-1)); + } }