diff --git a/domain.md b/domain.md new file mode 100644 index 000000000..9343c2e36 --- /dev/null +++ b/domain.md @@ -0,0 +1,61 @@ +# class diagram + +``` +------------------ +| Product | +------------------ +| - sku: String | +| - name: String | +| - variant: String | +| - price: double | +------------------ +| + getPrice(): double | +| + getName(): String | +| + getVariant(): String | +| + getSku(): String | +------------------ + +------------------ +| Filling | +------------------ +| - inherits from Product | +------------------ + +------------------ +| Bagel | +------------------ +| - inherits from Product | +| - fillings: List | +------------------ +| + addFilling(Filling): void | +| + removeFilling(Filling): void | +| + getFillings(): List | +| + getTotalPrice(): double | +------------------ + +------------------ +| Basket | +------------------ +| - items: List | +| - capacity: int | +------------------ +| + addItem(Bagel): boolean | +| + removeItem(Bagel): boolean | +| + isFull(): boolean | +| + getTotalCost(): double | +| + getItems(): List | +| + setCapacity(int): void | +| + hasItem(Bagel): boolean | +------------------ + +------------------ +| Inventory | +------------------ +| - products: List | +------------------ +| + getProductBySku(String): Product | +| + isInStock(String): boolean | +| + getPrice(String): double | +| + getAll(): List | +------------------ +``` \ No newline at end of file diff --git a/src/main/java/com/booleanuk/core/Bagel.java b/src/main/java/com/booleanuk/core/Bagel.java new file mode 100644 index 000000000..ceaee142e --- /dev/null +++ b/src/main/java/com/booleanuk/core/Bagel.java @@ -0,0 +1,28 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class Bagel extends Item { + private double price; + private List fillings = new ArrayList<>(); + + public Bagel(String sku, String name, String variant, double price) { + super(sku, name, variant, price); + this.price = price; + } + + + @Override + public double getPrice() { + return price; + } + + public void addFilling(Filling filling) { + fillings.add(filling); + } + + public List getFillings() { + return fillings; + } +} diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java new file mode 100644 index 000000000..6437840f0 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,56 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class Basket { + private List items; + private int capacity; + + public Basket() { + this(5); + } + + public boolean addItem(Item item) { + if (this.items.size() >= this.capacity) { + return false; + } + return this.items.add(item); + } + + public List getItems() { + return this.items; + } + + public boolean removeItem(Item item) { + return this.items.remove(item); + } + + public Basket(int capacity) { + this.items = new ArrayList<>(); + this.capacity = capacity; + } + + public int getCapacity() { + return this.capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public double getTotalCost(){ + double total = 0.0; + for (Item item : this.items){ + total += item.getPrice(); + + if (item instanceof Bagel) { + Bagel bagel = (Bagel) item; + for (Filling filling : bagel.getFillings()) { + total += filling.getPrice(); + } + } + } + return total; + } +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/core/Filling.java b/src/main/java/com/booleanuk/core/Filling.java new file mode 100644 index 000000000..bb04c5106 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Filling.java @@ -0,0 +1,8 @@ +package com.booleanuk.core; + +public class Filling extends Item { + public Filling(String sku, String name, double price) { + super(sku, name, null, price); + } +} + diff --git a/src/main/java/com/booleanuk/core/Inventory.java b/src/main/java/com/booleanuk/core/Inventory.java new file mode 100644 index 000000000..1075af593 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -0,0 +1,34 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class Inventory { + private Map stock; + + public Inventory() { + this.stock = new HashMap<>(); + stock.put("BGLO", new Bagel("BGLO", "Bagel", "Onion", 0.49)); + stock.put("BGLP", new Bagel("BGLP", "Bagel", "Plain", 0.39)); + stock.put("BGLE", new Bagel("BGLE", "Bagel", "Everything", 0.49)); + stock.put("BGLS", new Bagel("BGLS", "Bagel", "Sesame", 0.49)); + stock.put("COFB", new Bagel("COFB", "Coffee", "Black", 0.99)); + stock.put("COFW", new Bagel("COFW", "Coffee", "White", 1.19)); + stock.put("COFC", new Bagel("COFC", "Coffee", "Capuccino", 1.29)); + stock.put("COFL", new Bagel("COFL", "Coffee", "Latte", 1.29)); + stock.put("FILB", new Filling("FILB", "Bacon", 0.12)); + stock.put("FILE", new Filling("FILE", "Egg", 0.12)); + stock.put("FILC", new Filling("FILC", "Cheese", 0.12)); + stock.put("FILX", new Filling("FILX", "Cream Cheese", 0.12)); + stock.put("FILS", new Filling("FILS", "Smoked Salmon", 0.12)); + stock.put("FILH", new Filling("FILH", "Ham", 0.12)); + } + + public double getPriceBySku(String sku) { + Item item = stock.get(sku); + if (item != null) { + return item.getPrice(); + } + throw new IllegalArgumentException("Item not found in inventory"); + } +} diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java new file mode 100644 index 000000000..9cf57ff51 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item.java @@ -0,0 +1,34 @@ +package com.booleanuk.core; + +public abstract class Item implements Product { + protected String sku; + protected String name; + protected double price; + protected String variant; + + public Item(String sku, String name, String variant, double price) { + this.sku = sku; + this.name = name; + this.variant = variant; + this.price = price; + } + @Override + public String getSku() { + return sku; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getVariant() { + return variant; + } + + @Override + public double getPrice() { + return price; + } +} diff --git a/src/main/java/com/booleanuk/core/PricedItem.java b/src/main/java/com/booleanuk/core/PricedItem.java new file mode 100644 index 000000000..5390c06b9 --- /dev/null +++ b/src/main/java/com/booleanuk/core/PricedItem.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public interface PricedItem { + double getPrice(); +} diff --git a/src/main/java/com/booleanuk/core/Product.java b/src/main/java/com/booleanuk/core/Product.java new file mode 100644 index 000000000..0cbdb4b93 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Product.java @@ -0,0 +1,7 @@ +package com.booleanuk.core; + +public interface Product extends PricedItem{ + String getSku(); + String getName(); + String getVariant(); +} diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java new file mode 100644 index 000000000..66c9bba30 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,138 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import java.util.List; + +public class BasketTest { + @Test + public void testAddBagelToBasket() { + Basket basket = new Basket(); + Bagel bagel = new Bagel("BGLO", "Bagel", "Onion", 0.49); + + boolean added = basket.addItem(bagel); + + Assertions.assertTrue(added); + Assertions.assertEquals(1, basket.getItems().size()); + Assertions.assertEquals("Onion", basket.getItems().get(0).getVariant()); + } + + @Test + public void testRemoveBagelFromBasket() { + Basket basket = new Basket(); + Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49); + Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39); + + basket.addItem(bagel1); + basket.addItem(bagel2); + + boolean removed = basket.removeItem(bagel1); + + Assertions.assertTrue(removed); + Assertions.assertEquals(1, basket.getItems().size()); + Assertions.assertEquals("Plain", basket.getItems().get(0).getVariant()); + } + + @Test + public void testBasketIsFull() { + Basket basket = new Basket(2); + Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49); + Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39); + Bagel bagel3 = new Bagel("BGLE", "Bagel", "Everything", 0.49); + + Assertions.assertTrue(basket.addItem(bagel1)); + Assertions.assertTrue(basket.addItem(bagel2)); + Assertions.assertFalse(basket.addItem(bagel3)); + } + + @Test + public void testChangeBasketCapacity() { + Basket basket = new Basket(); + + Assertions.assertEquals(5, basket.getCapacity()); + + basket.setCapacity(10); + + Assertions.assertEquals(10, basket.getCapacity()); + } + + @Test + public void testRemoveNonexistentBagelFromBasket() { + Basket basket = new Basket(); + Bagel onionBagel = new Bagel("BGLO", "Bagel", "Onion", 0.49); + Bagel plainBagel = new Bagel("BGLP", "Bagel", "Plain", 0.39); + + basket.addItem(onionBagel); + boolean result = basket.removeItem(plainBagel); + + Assertions.assertFalse(result); + Assertions.assertEquals(1, basket.getItems().size()); + } + + @Test + public void testGetTotalCostBasket(){ + Basket basket = new Basket(); + + Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49); + Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39); + Bagel bagel3 = new Bagel("BGLE", "Bagel", "Everything", 0.49); + Bagel bagel4 = new Bagel("BGLS","Bagel", "Sesame",0.49); + + basket.addItem(bagel1); + basket.addItem(bagel2); + basket.addItem(bagel3); + basket.addItem(bagel4); + + double total = basket.getTotalCost(); + + Assertions.assertEquals(1.86, total, 0.001); + + } + + @Test + public void testGetBagelPriceFromInventory() { + Inventory inventory = new Inventory(); + double price = inventory.getPriceBySku("BGLO"); // Onion Bagel + + Assertions.assertEquals(0.49, price, 0.001); + } + + @Test + public void testCannotAddMoreThanCapacity() { + Basket basket = new Basket(2); // max 2 items + Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49); + Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39); + Bagel bagel3 = new Bagel("BGLE", "Bagel", "Everything", 0.49); + + Assertions.assertTrue(basket.addItem(bagel1)); + Assertions.assertTrue(basket.addItem(bagel2)); + Assertions.assertFalse(basket.addItem(bagel3)); + + Assertions.assertEquals(2, basket.getItems().size()); + } + + @Test + public void testGetFillingPriceBeforeAdding() { + Filling bacon = new Filling("FILB", "Bacon", 0.12); + Assertions.assertEquals("Bacon", bacon.getName()); + Assertions.assertEquals(0.12, bacon.getPrice()); + Assertions.assertEquals("FILB", bacon.getSku()); + } + + @Test + public void testAddFillingToBagel() { + Basket basket = new Basket(); + Bagel bagel = new Bagel("BGLO", "Bagel", "Onion", 0.49); + Filling bacon = new Filling("FILB", "Bacon", 0.12); + Filling egg = new Filling("FILE", "Egg", 0.12); + + bagel.addFilling(bacon); + bagel.addFilling(egg); + basket.addItem(bagel); + + Assertions.assertEquals(2, bagel.getFillings().size()); + Assertions.assertEquals(0.73, basket.getTotalCost(), 0.001); // 0.49 + 0.12 + 0.12 + } + + +} \ No newline at end of file