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..11e167ae7 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Bagel.java @@ -0,0 +1,32 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class Bagel extends Item{ + private final List fillings = new ArrayList<>(); + + public Bagel(String sku, float price, String variant) { + super(sku, price, "Bagel", variant); + } + + @Override + public float getPrice(){ + float total = 0f; + for(Item f: fillings){ + total += f.getPrice(); + } + return super.getPrice() + total; + } + + public List getFillings() { + return fillings; + } + + public boolean addFilling(Item filling){ + if(!(filling instanceof Filling)) + return false; + fillings.add(filling); + return true; + } +} 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..0db6a6362 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,66 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class Basket { + private List items = new ArrayList<>(); + private int capacity; + private static int stdCapacity = 5; + + public Basket() { + this.capacity = stdCapacity; + } + + public int add(String sku) { + if (items.size() >= capacity) + return 0; + if(!Stock.isInStock(sku)) + return -1; + items.add(Stock.getItem(sku)); + return 1; + } + + public int add(Item item) { + if (items.size() >= capacity) + return 0; + if(item == null || !Stock.isInStock(item.getSku())) + return -1; + items.add(item); + return 1; + } + + public boolean remove(String sku) { + for(Item item: items){ + if(item.getSku().equals(sku)) { + items.remove(item); + return true; + } + } + return false; + } + + public List getItems() { + return items; + } + + public int getCapacity() { + return capacity; + } + + public static int getStdCapacity() { + return stdCapacity; + } + + public static void setStdCapacity(int stdCapacity) { + Basket.stdCapacity = stdCapacity; + } + + public float getTotalCost(){ + float total = 0f; + for (Item item: items){ + total += item.getPrice(); + } + return total; + } +} 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..1dd19af67 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Coffee.java @@ -0,0 +1,8 @@ +package com.booleanuk.core; + +public class Coffee extends Item{ + + public Coffee(String sku, float price, String variant) { + super(sku, price, "Coffee", variant); + } +} 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..6b12d4a08 --- /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, float price, String variant) { + super(sku, price, "Filling", variant); + } +} 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..abc2728db --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item.java @@ -0,0 +1,31 @@ +package com.booleanuk.core; + +public abstract class Item { + private String sku; + private float price; + private String Name; + private String variant; + + public String getVariant() { + return variant; + } + + public String getName() { + return Name; + } + + public float getPrice() { + return price; + } + + public String getSku() { + return sku; + } + + public Item(String sku, float price, String name, String variant) { + this.sku = sku; + this.price = price; + Name = name; + this.variant = variant; + } +} diff --git a/src/main/java/com/booleanuk/core/Stock.java b/src/main/java/com/booleanuk/core/Stock.java new file mode 100644 index 000000000..df5c87220 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Stock.java @@ -0,0 +1,60 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Stock { + static private List stock = Arrays.asList( + new Bagel("BGLO", 0.49f, "Onion"), + new Bagel("BGLP", 0.39f, "Plain"), + new Bagel("BGLE", 0.49f, "Everything"), + new Bagel("BGLS", 0.49f, "Sesame"), + new Coffee("COFB", 0.99f, "Black"), + new Coffee("COFW", 1.19f, "White"), + new Coffee("COFC", 1.29f, "Cappuccino"), + new Coffee("COFL", 1.29f, "Latte"), + new Filling("FILB", 0.12f, "Bacon"), + new Filling("FILE", 0.12f, "Egg"), + new Filling("FILC", 0.12f, "Cheese"), + new Filling("FILX", 0.12f, "Cream Cheese"), + new Filling("FILS", 0.12f, "Smoked Salmon"), + new Filling("FILH", 0.12f, "Ham")); + + + public static boolean isInStock(String sku) { + for (Item item: stock){ + if (item.getSku().equals(sku)){ + return true; + } + } + return false; + } + + public static Item getItem(String sku) { + for (Item item: stock){ + if (item.getSku().equals(sku)){ + return copyItem(item); + } + } + return null; + } + + private static Item copyItem(Item item){ + if (item instanceof Bagel) + return new Bagel(item.getSku(), item.getPrice(), item.getVariant()); + if (item instanceof Coffee) + return new Coffee(item.getSku(), item.getPrice(), item.getVariant()); + else + return new Filling(item.getSku(), item.getPrice(), item.getVariant()); + } + + public static List getFillings() { + List fillings = new ArrayList<>(); + for (Item item: stock){ + if (item instanceof Filling) + fillings.add((Filling)copyItem(item)); + } + return fillings; + } +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/core/class_diagram.png b/src/main/java/com/booleanuk/core/class_diagram.png new file mode 100644 index 000000000..fc3533329 Binary files /dev/null and b/src/main/java/com/booleanuk/core/class_diagram.png differ diff --git a/src/main/java/com/booleanuk/core/domain-model.md b/src/main/java/com/booleanuk/core/domain-model.md new file mode 100644 index 000000000..fe9c093c6 --- /dev/null +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -0,0 +1,25 @@ +| Class | Member | Method | Scenario | Output | +|---------------|--------------------|------------------------------|------------------------------|------------| +| Basket | List items | add(String sku) | Correct code basket not full | 1 | +| | int capacity | | Correct code basket full | 0 | +| | static stdCapacity | | Incorrect code | -1 | +| | static Stock stock | remove(String sku) | Exists | True | +| | | | Doesn't exist | False | +| | | getTotalCost() | | float | +| Stock | List stock | | | | +| | | getPrice(String sku) | Exists | float | +| | | | Doesn't exist | -1 | +| | | getFillings() | | List | +| | | getItem(String sku) | Exists | Item | +| | | | Does not exist | null | +| Abstract Item | String sku | getSku() | | sku | +| | float price | getPrice() | | price | +| | String name | getName() | | name | +| | String variant | getVariant() | | variant | +| | | | | | +| Bagel | List | addFilling(String "variant") | | void | +| | | removeFilling("variant") | | | +| Coffee | | | | | +| Filling | | | | | + + diff --git a/src/main/java/com/booleanuk/extension/Bagel.java b/src/main/java/com/booleanuk/extension/Bagel.java new file mode 100644 index 000000000..18cc2b895 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Bagel.java @@ -0,0 +1,32 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.List; + +public class Bagel extends Item{ + private final List fillings = new ArrayList<>(); + + public Bagel(String sku, float price, String variant) { + super(sku, price, "Bagel", variant); + } + + @Override + public float getPrice(){ + float total = 0f; + for(Item f: fillings){ + total += f.getPrice(); + } + return super.getPrice() + total; + } + + public List getFillings() { + return fillings; + } + + public boolean addFilling(Item filling){ + if(!(filling instanceof Filling)) + return false; + fillings.add(filling); + return true; + } +} diff --git a/src/main/java/com/booleanuk/extension/Basket.java b/src/main/java/com/booleanuk/extension/Basket.java new file mode 100644 index 000000000..49d6f760f --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Basket.java @@ -0,0 +1,164 @@ +package com.booleanuk.extension; + +import java.util.*; + +public class Basket { + private List items = new ArrayList<>(); + private int capacity; + private static int stdCapacity = 5; + + public Basket() { + this.capacity = stdCapacity; + } + + public int add(String sku) { + if (items.size() >= capacity) + return 0; + if(!Stock.isInStock(sku)) + return -1; + items.add(Stock.getItem(sku)); + return 1; + } + + public int add(String sku, int count) { + if (items.size() + count > capacity) + return 0; + if(!Stock.isInStock(sku)) + return -1; + for (int i = 0; i < count; i++) { + items.add(Stock.getItem(sku)); + } + return 1; + } + + public int add(Item item) { + if (items.size() >= capacity) + return 0; + if(item == null || !Stock.isInStock(item.getSku())) + return -1; + items.add(item); + return 1; + } + + public boolean remove(String sku) { + for(Item item: items){ + if(item.getSku().equals(sku)) { + items.remove(item); + return true; + } + } + return false; + } + + public List getItems() { + return items; + } + + public int getCapacity() { + return capacity; + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + } + + public static int getStdCapacity() { + return stdCapacity; + } + + public static void setStdCapacity(int stdCapacity) { + Basket.stdCapacity = stdCapacity; + } + + public float getTotalCost(){ + float total = 0f; + for (Item item: items){ + total += item.getPrice(); + } + return total; + } + + public float getDiscountedTotal(){ + float total = 0; + for(PurchaseInfo info: getPurchaseInfo()){ + total += info.getPrice(); + } + return total; + } + + public Collection getPurchaseInfo(){ + Map countMap = getAmounts(); + Map info = new HashMap<>(); + // Check discounts for bagel + for (Map.Entry entry : countMap.entrySet()) { + String sku = entry.getKey(); + Integer n = entry.getValue(); + if(sku.substring(0, 3).equals("BGL")){ + float disc = getBagelDiscount(sku, n); + info.put(sku, new PurchaseInfo(sku, n, Stock.getItem(sku).getPrice()*n - disc, disc)); + countMap.put(sku, countMap.get(sku) - (n - n%6)); + } + else + info.put(sku, new PurchaseInfo(sku, n, Stock.getItem(sku).getPrice()*n, 0)); + } + // Check discount for Bagel & coffe combo + for (Map.Entry entry : countMap.entrySet()) { + String sku = entry.getKey(); + Integer n = entry.getValue(); + if(sku.substring(0, 3).equals("COF")){ + float disc = 0; + for (int i = 0; i < n; i++) { + disc += findBagelCoffeeCombo(sku, countMap); + } + info.get(sku).setDiscount(disc); + info.get(sku).setPrice(info.get(sku).getPrice()-disc); + } + } + return info.values(); + } + + private float getBagelDiscount(String sku, int count){ + float discount = 0; + while (count >= 12) { + discount += 12 * Stock.getItem(sku).getPrice() - 3.99f; + count -= 12; + } + while (count >= 6) { + discount += 6 * Stock.getItem(sku).getPrice() - 2.49f; + count -= 6; + } + return discount; + } + + private float findBagelCoffeeCombo(String coffeSku, Map count){ + float disc = 0; + for (Map.Entry entry : count.entrySet()) { + String sku = entry.getKey(); + Integer n = entry.getValue(); + if(sku.substring(0, 3).equals("BGL") && n >= 1){ + disc = Stock.getItem(sku).getPrice() + Stock.getItem(coffeSku).getPrice() - 1.25f; + count.put(sku, count.get(sku) - 1); + } + } + return disc; + } + + private Map getAmounts(){ + Map count = new HashMap<>(); + for(Item item: items){ + if (item instanceof Bagel){ + for(Item filling: ((Bagel) item).getFillings()){ + if (count.containsKey(filling.getSku())) + count.put(filling.getSku(), count.get(filling.getSku()) + 1); + else + count.put(filling.getSku(), 1); + } + } + if (count.containsKey(item.getSku())) + count.put(item.getSku(), count.get(item.getSku()) + 1); + else + count.put(item.getSku(), 1); + } + return count; + } +} diff --git a/src/main/java/com/booleanuk/extension/Coffee.java b/src/main/java/com/booleanuk/extension/Coffee.java new file mode 100644 index 000000000..92e84b44b --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Coffee.java @@ -0,0 +1,8 @@ +package com.booleanuk.extension; + +public class Coffee extends Item{ + + public Coffee(String sku, float price, String variant) { + super(sku, price, "Coffee", variant); + } +} diff --git a/src/main/java/com/booleanuk/extension/Filling.java b/src/main/java/com/booleanuk/extension/Filling.java new file mode 100644 index 000000000..8625a5a45 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Filling.java @@ -0,0 +1,8 @@ +package com.booleanuk.extension; + +public class Filling extends Item{ + + public Filling(String sku, float price, String variant) { + super(sku, price, "Filling", variant); + } +} diff --git a/src/main/java/com/booleanuk/extension/Item.java b/src/main/java/com/booleanuk/extension/Item.java new file mode 100644 index 000000000..81505a833 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Item.java @@ -0,0 +1,31 @@ +package com.booleanuk.extension; + +public abstract class Item { + private String sku; + private float price; + private String Name; + private String variant; + + public String getVariant() { + return variant; + } + + public String getName() { + return Name; + } + + public float getPrice() { + return price; + } + + public String getSku() { + return sku; + } + + public Item(String sku, float price, String name, String variant) { + this.sku = sku; + this.price = price; + Name = name; + this.variant = variant; + } +} diff --git a/src/main/java/com/booleanuk/extension/PurchaseInfo.java b/src/main/java/com/booleanuk/extension/PurchaseInfo.java new file mode 100644 index 000000000..a8564c35b --- /dev/null +++ b/src/main/java/com/booleanuk/extension/PurchaseInfo.java @@ -0,0 +1,62 @@ +package com.booleanuk.extension; + +public class PurchaseInfo { + private String sku; + private int count; + private float price; + private float discount; + + public PurchaseInfo(String sku, int count, float price, float discount) { + this.sku = sku; + this.count = count; + this.price = price; + this.discount = discount; + } + + public String getSku() { + return sku; + } + + public void setSku(String sku) { + this.sku = sku; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + + public float getDiscount() { + return discount; + } + + public void setDiscount(float discount) { + this.discount = discount; + } + + @Override + public String toString() { + Item item = Stock.getItem(sku); + StringBuilder str = new StringBuilder(); + str.append(String.format("%-18s%-4s%-7s", item.getVariant() + " " + item.getName(), getCount(), "£" + getPrice())); + if(getDiscount() > 0){ + str.append("\n" + String.format("%-19s%-7s"," ","(-£" + round(getDiscount()) + ")")); + } + return str.toString(); + } + + private double round(float f){ + return Math.round(f * Math.pow(10, 2)) / Math.pow(10, 2); + } +} diff --git a/src/main/java/com/booleanuk/extension/Receipt.java b/src/main/java/com/booleanuk/extension/Receipt.java new file mode 100644 index 000000000..52bad7632 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Receipt.java @@ -0,0 +1,41 @@ +package com.booleanuk.extension; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Map; + +public class Receipt { + Basket basket; + String timestamp; + + + public Receipt(Basket basket) { + this.basket = basket; + LocalDateTime now = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + this.timestamp = now.format(formatter); + } + + public void printReceipt(){ + System.out.println(generateReceipt()); + } + + public String generateReceipt(){ + StringBuilder receipt = new StringBuilder(); + receipt.append(" ~~~ Bob's Bagels ~~~\n \n " + timestamp + "\n \n" + "----------------------------\n\n"); + for(PurchaseInfo item: basket.getPurchaseInfo()){ + receipt.append(item.toString()).append("\n"); + } + receipt.append("\n----------------------------\n"); + float total = basket.getDiscountedTotal(); + receipt.append(String.format("%-22s%-7s\n","Total","£" + total)); + receipt.append("\n\n You saved a total of £").append(round(basket.getTotalCost() - total)).append("\n\t on this shop") + .append("\n\n\t Thank you\n\t for you order!"); + return receipt.toString(); + } + + private double round(float f){ + return Math.round(f * Math.pow(10, 2)) / Math.pow(10, 2); + } +} diff --git a/src/main/java/com/booleanuk/extension/Stock.java b/src/main/java/com/booleanuk/extension/Stock.java new file mode 100644 index 000000000..f07884a12 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Stock.java @@ -0,0 +1,60 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class Stock { + static private List stock = Arrays.asList( + new Bagel("BGLO", 0.49f, "Onion"), + new Bagel("BGLP", 0.39f, "Plain"), + new Bagel("BGLE", 0.49f, "Everything"), + new Bagel("BGLS", 0.49f, "Sesame"), + new Coffee("COFB", 0.99f, "Black"), + new Coffee("COFW", 1.19f, "White"), + new Coffee("COFC", 1.29f, "Cappuccino"), + new Coffee("COFL", 1.29f, "Latte"), + new Filling("FILB", 0.12f, "Bacon"), + new Filling("FILE", 0.12f, "Egg"), + new Filling("FILC", 0.12f, "Cheese"), + new Filling("FILX", 0.12f, "Cream Cheese"), + new Filling("FILS", 0.12f, "Smoked Salmon"), + new Filling("FILH", 0.12f, "Ham")); + + + public static boolean isInStock(String sku) { + for (Item item: stock){ + if (item.getSku().equals(sku)){ + return true; + } + } + return false; + } + + public static Item getItem(String sku) { + for (Item item: stock){ + if (item.getSku().equals(sku)){ + return copyItem(item); + } + } + return null; + } + + private static Item copyItem(Item item){ + if (item instanceof Bagel) + return new Bagel(item.getSku(), item.getPrice(), item.getVariant()); + if (item instanceof Coffee) + return new Coffee(item.getSku(), item.getPrice(), item.getVariant()); + else + return new Filling(item.getSku(), item.getPrice(), item.getVariant()); + } + + public static List getFillings() { + List fillings = new ArrayList<>(); + for (Item item: stock){ + if (item instanceof Filling) + fillings.add((Filling)copyItem(item)); + } + return fillings; + } +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/extension/class-diagram-ext.png b/src/main/java/com/booleanuk/extension/class-diagram-ext.png new file mode 100644 index 000000000..4bca2db13 Binary files /dev/null and b/src/main/java/com/booleanuk/extension/class-diagram-ext.png differ diff --git a/src/main/java/com/booleanuk/extension/domain-model.md b/src/main/java/com/booleanuk/extension/domain-model.md new file mode 100644 index 000000000..69433c97c --- /dev/null +++ b/src/main/java/com/booleanuk/extension/domain-model.md @@ -0,0 +1,53 @@ +| Class | Member | Method | Scenario | Output | +|---------------|------------------------|------------------------------|------------------------------|------------| +| Basket | List items | add(String sku) | Correct code basket not full | 1 | +| | int capacity | | Correct code basket full | 0 | +| | static stdCapacity | | Incorrect code | -1 | +| | static Stock stock | remove(String sku) | Exists | True | +| | | | Doesn't exist | False | +| | | getTotalCost() | | float | +| | | getDiscountedCost() | | float | +| Stock | List stock | | | | +| | | getPrice(String sku) | Exists | float | +| | | | Doesn't exist | -1 | +| | | getFillings() | | List | +| | | getItem(String sku) | Exists | Item | +| | | | Does not exist | null | +| Abstract Item | String sku | getSku() | | sku | +| | float price | getPrice() | | price | +| | String name | getName() | | name | +| | String variant | getVariant() | | variant | +| | | | | | +| Bagel | List fillings | addFilling(String "variant") | | void | +| | | removeFilling("variant") | | | +| Coffee | | | | | +| Filling | | | | | +| Receipt | Basket basket | printReceipt() | | | +| | TimeStamp timestamp | | | | +| PurchaseInfo | String sku | toString() | | String | +| | int count | | | | +| | float price | | | | +| | float discount | | | | + + + +### Extension 1 +``` +As a cutomer, +So I know what the price will be, +I'd like to know the total discounted price +``` + +### Extension 2 +``` +As a cutomer, +So I know what I bought and the cost, +I'd like to be able to print a recepit. +``` + +### Extension 3 +``` +As a cutomer, +So I know how much I saved, +I'd like to see the discounts and the total mount saved on the receipt +``` \ No newline at end of file diff --git a/src/test/java/com/booleanuk/core/BagelShopTest.java b/src/test/java/com/booleanuk/core/BagelShopTest.java new file mode 100644 index 000000000..05057a941 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BagelShopTest.java @@ -0,0 +1,97 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BagelShopTest { + @Test + public void testAddingBagelToBasket(){ + Basket basket = new Basket(); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertFalse(basket.getItems().isEmpty()); + } + + @Test + public void testRemovingBagel(){ + Basket basket = new Basket(); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertFalse(basket.remove("BGRF")); + Assertions.assertTrue(basket.remove("BGLO")); + Assertions.assertTrue(basket.getItems().isEmpty()); + } + + @Test + public void testAddingUntilFull(){ + Basket basket = new Basket(); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertEquals(0, basket.add("BGLO")); + Assertions.assertEquals(5, basket.getItems().size()); + } + + @Test + public void testChangeBasketStdCapacity(){ + Basket basket = new Basket(); + Assertions.assertEquals(5, basket.getCapacity()); + Basket.setStdCapacity(3); + Assertions.assertEquals(3, Basket.getStdCapacity()); + Basket basket2 = new Basket(); + Assertions.assertEquals(3, basket2.getCapacity()); + } + + @Test + public void testTotalPrice(){ + Basket basket = new Basket(); + Assertions.assertEquals(0, basket.getTotalCost()); + basket.add("BGLO"); + Assertions.assertEquals(0.49f, basket.getTotalCost()); + basket.add("BGLO"); + Assertions.assertEquals(0.98f, basket.getTotalCost()); + basket.add("COFB"); + Assertions.assertEquals(1.97f, basket.getTotalCost()); + } + + @Test + public void testTotalPriceWithFillings(){ + Basket basket = new Basket(); + Assertions.assertEquals(0, basket.getTotalCost()); + basket.add("BGLO"); + Assertions.assertEquals(0.49f, basket.getTotalCost()); + Bagel bagel = (Bagel) Stock.getItem("BGLS"); + Assertions.assertTrue(bagel.addFilling(Stock.getItem("FILE"))); + basket.add(bagel); + Assertions.assertEquals(1.1f, basket.getTotalCost()); + bagel.addFilling(Stock.getItem("FILE")); + Assertions.assertEquals(1.22f, basket.getTotalCost()); + } + + @Test + public void testAddingFilling(){ + Bagel bagel = (Bagel) Stock.getItem("BGLS"); + Assertions.assertTrue(bagel.addFilling(Stock.getItem("FILE"))); + Assertions.assertEquals(1, bagel.getFillings().size()); + } + + @Test + public void testAddingInvalidFilling(){ + Bagel bagel = (Bagel) Stock.getItem("BGLS"); + Assertions.assertFalse(bagel.addFilling(null)); + } + + @Test + public void testAddingInvalidItemsToBasket(){ + Basket basket = new Basket(); + Assertions.assertEquals(-1, basket.add("BGLH")); + Assertions.assertTrue(basket.getItems().isEmpty()); + Assertions.assertEquals(-1, basket.add((Item) null)); + Assertions.assertTrue(basket.getItems().isEmpty()); + } + + @Test + public void testGetCostOfFillings(){ + Assertions.assertEquals(0.12f, Stock.getFillings().getFirst().getPrice()); + } +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/extension/BagelExtTest.java b/src/test/java/com/booleanuk/extension/BagelExtTest.java new file mode 100644 index 000000000..68b3f0e25 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/BagelExtTest.java @@ -0,0 +1,49 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BagelExtTest { + @Test + public void testGettingDiscount(){ + Basket basket = new Basket(); + basket.setCapacity(25); + basket.add("BGLO", 2); + basket.add("BGLP", 12); + basket.add("BGLE", 6); + basket.add("COFB", 3); + Assertions.assertEquals(9.97f, basket.getDiscountedTotal(), 0.001); + } + + @Test + public void testGettingDiscount2(){ + Basket basket = new Basket(); + basket.setCapacity(25); + basket.add("BGLP", 16); + Assertions.assertEquals(5.55f, basket.getDiscountedTotal(), 0.001); + } + + @Test + public void testGettingDiscountwithFillings(){ + Basket basket = new Basket(); + basket.setCapacity(25); + basket.add("BGLP", 16); + ((Bagel)basket.getItems().getFirst()).addFilling(Stock.getItem("FILC")); + ((Bagel)basket.getItems().getFirst()).addFilling(Stock.getItem("FILS")); + Assertions.assertEquals(5.55f + 0.24f, basket.getDiscountedTotal(), 0.001); + } + + @Test + public void testprintReceipt(){ + Basket basket = new Basket(); + basket.setCapacity(25); + basket.add("BGLO", 2); + basket.add("BGLP", 12); + basket.add("BGLE", 6); + basket.add("COFB", 3); + Receipt receipt = new Receipt(basket); + receipt.printReceipt(); + } + + +}