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..3679a692c --- /dev/null +++ b/src/main/java/com/booleanuk/core/Bagel.java @@ -0,0 +1,39 @@ +package com.booleanuk.core; + +public class Bagel extends Item{ + private Filling filling; + + public Bagel(String variant, String sku){ + super("Bagel", variant, sku); + } + + public String getFillingSku(){ + return filling.getSku(); + } + + public boolean gotFilling(){ + if (filling != null){ + return true; + } + return false; + } + + public String getCombiSku(){ + if (filling != null){ + return super.getSku() + "-" + filling.getSku(); + } + return getSku(); + } + + public boolean setFilling(Filling filling){ + this.filling = filling; + return true; + } + + @Override + public float getTotalCost(PriceList priceList){ + if(this.gotFilling()) + return priceList.getPrice(super.getSku()) + priceList.getPrice(filling.getSku()); + return priceList.getPrice(super.getSku()); + } +} 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..bffd7143d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,101 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Basket { + private Map basketList; + private PriceList priceList; + private Inventory inventory; + private int capacity; + + public Basket(int capacity, PriceList priceList, Inventory inventory) { + this.capacity = capacity; + this.priceList = priceList; + this.inventory = inventory; + this.basketList = new HashMap(); + } + + public boolean addItem(Item item, int quantity) { + String key = getBasketKey(item); + if (basketList.containsKey(key)) { + BasketItem DupItem = basketList.get(key); + DupItem.setQuantity(DupItem.getQuantity() + quantity); + } else { + BasketItem basketItem = new BasketItem(item, quantity); + basketList.put(key, basketItem); + } + return true; + } + + public boolean removeItem(Item item) { + String key = getBasketKey(item); + + if(!basketList.containsKey(key)){ + return false; + } + + int currQuantity = basketList.get(key).getQuantity(); + + if(currQuantity > 1){ + basketList.get(key).setQuantity(currQuantity - 1); + }else{ + basketList.remove(key); + } + return true; + } + + public boolean isFull() { + int total = 0; + for(BasketItem basketItem: basketList.values()){ + total+= basketItem.getQuantity(); + if(total>=capacity) + return true; + } + return false; + } + + public boolean itemInList(Item item) { + if(basketList.containsKey(getBasketKey(item))){ + return true; + } + return false; + } + + public float getTotalCost() { + float total = 0.0f; + for(BasketItem item : basketList.values()){ + total+= item.getTotalCost(priceList); + } + return total; + } + + public Map getBasketList() { + return basketList; + } + + private String getBasketKey(Item item) { + if (item instanceof Bagel) { + return ((Bagel) item).getCombiSku(); + } + return item.getSku(); + } + + public int getCapacity() { + return capacity; + } + + public boolean setCapacity(int capacity, String role) { + if (role == "Manager" && capacity>=0){ + this.capacity = capacity; + return true; + } + return false; + } + + public boolean isInInventory(Item item){ + return inventory.getItems().containsKey(item.getSku()); + } +} diff --git a/src/main/java/com/booleanuk/core/BasketItem.java b/src/main/java/com/booleanuk/core/BasketItem.java new file mode 100644 index 000000000..03774647e --- /dev/null +++ b/src/main/java/com/booleanuk/core/BasketItem.java @@ -0,0 +1,34 @@ +package com.booleanuk.core; + +public class BasketItem { + private Item item; + private int quantity; + + public BasketItem(Item item, int quantity){ + this.item = item; + this.quantity = quantity; + } + + public Item getItem(){ + return item; + } + + public int getQuantity(){ + return quantity; + } + + public boolean setQuantity(int quantity){ + this.quantity = quantity; + return false; + } + + public String getSku(){ + if (item instanceof Bagel) + return ((Bagel) item).getCombiSku(); + return item.getSku(); + } + + public float getTotalCost(PriceList priceList){ + return item.getTotalCost(priceList)*quantity; + } +} diff --git a/src/main/java/com/booleanuk/core/Coffee.java b/src/main/java/com/booleanuk/core/Coffee.java new file mode 100644 index 000000000..520cfe390 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Coffee.java @@ -0,0 +1,7 @@ +package com.booleanuk.core; + +public class Coffee extends Item{ + public Coffee(String variant, String sku){ + super("Coffee", variant, sku); + } +} 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..d5cbfd66a --- /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 variant, String sku){ + super("Filling", variant, sku); + } + +} 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..7ed7f8b4a --- /dev/null +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -0,0 +1,28 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class Inventory { + private Map items; + + public Inventory() { + this.items = new HashMap(); + } + + public boolean addItem(Item item){ + items.put(item.getSku(), item); + return false; + } + + public boolean removeItem(Item item){ + items.remove(item.getSku()); + return false; + } + + public Map getItems(){ + return items; + } +} 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..1aac83c13 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item.java @@ -0,0 +1,29 @@ +package com.booleanuk.core; + +public class Item { + private String name; + private String variant; + private String sku; + + public Item(String name, String variant, String sku){ + this.name = name; + this.variant = variant; + this.sku = sku; + } + + public String getName() { + return name; + } + + public String getVariant(){ + return variant; + } + + public String getSku(){ + return sku; + } + + public float getTotalCost(PriceList priceList){ + return priceList.getPrice(sku); + } +} diff --git a/src/main/java/com/booleanuk/core/PriceList.java b/src/main/java/com/booleanuk/core/PriceList.java new file mode 100644 index 000000000..3ba33971f --- /dev/null +++ b/src/main/java/com/booleanuk/core/PriceList.java @@ -0,0 +1,20 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class PriceList { + private Map priceList; + + public PriceList(){ + priceList = new HashMap(); + } + + public float getPrice(String sku){ + return priceList.get(sku); + } + + public void setPrice(String sku, float price){ + priceList.put(sku, price); + } +} 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..8be577421 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,180 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest{ + @Test + public void ShouldAddDifferentItemsToBasket() { + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(5, prices, inventory); + + Item bagel = new Bagel("Onion", "BGLO"); + Item coffee = new Coffee("Black", "COFB"); + + Assertions.assertTrue(basket.addItem(bagel, 1)); + Assertions.assertTrue(basket.addItem(coffee, 2)); + Assertions.assertEquals(2, basket.getBasketList().size()); + Assertions.assertTrue(basket.itemInList(bagel)); + Assertions.assertTrue(basket.itemInList(coffee)); + Assertions.assertTrue(basket.getBasketList().containsKey(bagel.getSku())); + Assertions.assertTrue(basket.getBasketList().containsKey(coffee.getSku())); + + } + + @Test + public void shouldRemoveExistingItem() { + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(5, prices, inventory); + Item bagel = new Bagel("Onion", "BGLO"); + Item bagel2 = new Bagel("Plain", "BGLP"); + basket.addItem(bagel, 1); + + Assertions.assertFalse(basket.removeItem(bagel2)); + Assertions.assertTrue(basket.removeItem(bagel)); + Assertions.assertTrue(basket.getBasketList().isEmpty()); + + basket.addItem(bagel, 2); + + Assertions.assertTrue(basket.removeItem(bagel)); + Assertions.assertEquals(1, basket.getBasketList().get("BGLO").getQuantity()); + + } + + @Test + public void ShouldAddFilling() { + Bagel bagel = new Bagel("Onion", "BGLO"); + Filling filling = new Filling("Bacon", "FILB"); + + Assertions.assertTrue(bagel.setFilling(filling)); + Assertions.assertTrue(bagel.gotFilling()); + Assertions.assertEquals("FILB", bagel.getFillingSku()); + } + + @Test + public void ShouldAddSameItemToBasket(){ + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(5, prices, inventory); + + Bagel bagel = new Bagel("Onion", "BGLO"); + Filling filling = new Filling("Bacon", "FILB"); + bagel.setFilling(filling); + + basket.addItem(bagel,1); + Assertions.assertTrue(basket.addItem(bagel,1)); + Assertions.assertEquals(2, basket.getBasketList().get("BGLO-FILB").getQuantity()); + } + + @Test + public void ShouldTellIfItemIsInList(){ + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(5, prices, inventory); + + Item bagel = new Bagel("Onion", "BGLO"); + Bagel bagel2 = new Bagel("Onion", "BGLO"); + Coffee coffee = new Coffee("Black", "COFB"); + Filling filling = new Filling("Bacon", "FILB"); + + bagel2.setFilling(filling); + + basket.addItem(bagel, 1); + basket.addItem(coffee, 1); + + Assertions.assertTrue(basket.itemInList(bagel)); + Assertions.assertTrue(basket.itemInList(coffee)); + Assertions.assertFalse(basket.itemInList(bagel2)); + + basket.addItem(bagel2, 1); + + Assertions.assertTrue(basket.itemInList(bagel2)); + } + + @Test + public void ShouldWarnWhenBasketIsFull() { + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(2, prices, inventory); + Item bagel = new Bagel("Onion", "BGLO"); + Item bagel2 = new Bagel("Plain", "BGLP"); + + Assertions.assertFalse(basket.isFull()); + + basket.addItem(bagel, 1); + + Assertions.assertFalse(basket.isFull()); + + basket.addItem(bagel2, 1); + + Assertions.assertTrue(basket.isFull()); + + } + + @Test + public void ShouldChangeCapacity() { + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(2, prices, inventory); + + Assertions.assertTrue(basket.setCapacity(4, "Manager")); + Assertions.assertFalse(basket.setCapacity(4, "Customer")); + Assertions.assertFalse(basket.setCapacity(-2, "Manager")); + } + + @Test + public void ShouldGetPriceOfItem(){ + PriceList prices = new PriceList(); + prices.setPrice("BGLO", 0.49f); + prices.setPrice("FILB", 0.12f); + + Bagel bagel = new Bagel("Onion", "BGLO"); + Filling filling = new Filling("Bacon", "FILB"); + + Assertions.assertEquals(0.49f, bagel.getTotalCost(prices)); + + bagel.setFilling(filling); + Assertions.assertEquals(0.61f, bagel.getTotalCost(prices)); + } + + @Test + public void ShouldCalculateTotalCost() { + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(5, prices, inventory); + + Item bagel = new Bagel("Onion", "BGLO"); + Bagel bagel2 = new Bagel("Plain", "BGLP"); + Filling filling = new Filling("Bacon", "FILB"); + + prices.setPrice("BGLO", 0.49f); + prices.setPrice("BGLP", 0.39f); + prices.setPrice("FILB", 0.12f); + + bagel2.setFilling(filling); + + Assertions.assertEquals(0, basket.getTotalCost()); + + basket.addItem(bagel, 1); + basket.addItem(bagel2, 2); + + Assertions.assertEquals(1.51f, basket.getTotalCost()); + } + + @Test + public void ShouldCheckIfItemIsInInventory() { + PriceList prices = new PriceList(); + Inventory inventory = new Inventory(); + Basket basket = new Basket(2, prices, inventory); + + Item bagel = new Bagel("Onion", "BGLO"); + Item bagel2 = new Bagel("Plain", "BGLP"); + + inventory.addItem(bagel); + + Assertions.assertFalse(basket.isInInventory(bagel2)); + Assertions.assertTrue(basket.isInInventory(bagel)); + } +} \ No newline at end of file