diff --git a/model.md b/model.md new file mode 100644 index 000000000..fc2d5d595 --- /dev/null +++ b/model.md @@ -0,0 +1,105 @@ +```mermaid +classDiagram + class Inventory { + -Stock map + + +getPrice() + +getVarient() + +getName() + +getDiscountCondition() + +getDiscount() + + } + + class Stock { + hasItem(String sku) + getPrice(String sku) + getVarient(String sku) + getName(String sku) + getDiscountCondition(String sku) + etDiscount(String sku) + } + + class Basket { + -int size + -Array items + -Inventory inventory + + +Basket(Inventory) + +addBagel(Bagel) void + +addFilling(sku) void + +addCoffee(sku) void + +costOf(sku) float + +removeItem(sku) boolean + +totalCost() + +getBasket() String (what the basket has) + + } + + class Manager { + -name + + +changeBasketSize() void + } + + class Item { + -price + -sku + -varient + -name + -discountCondition + -discount + -inventory + + +getters() + + } + + class Bagel{ + -type + -fillings + -totalPrice + + +addFilling() void + +removeFilling() void + +getPrice() float + +getSize() int + + } + + class Coffee{ + -type + + } + + class Filling{ + -type + + } + + class Customer{ + -basket + + +getBasket() + + } + + + Customer o-- Basket : has a + + Basket o-- Item : has + + Manager o-- Basket : change values + + Bagel o-- Item: extends + Coffee o-- Item: extends + Filling o-- Item: extends + Bagel o-- Filling: has + + Item o-- Stock: uses + Basket o-- Stock: uses + + Inventory o-- Stock: implements + + +``` 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..aa59a6501 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,162 @@ +package com.booleanuk.core; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; + +import com.booleanuk.core.item_types.*; + +public class Basket { + private static int size = 30; + private int curSize; + private Item[] arr; + private final Stock stock; // Should be static? + + public Basket(Stock stock){ + this.arr = new Item[Basket.size]; + this.curSize = 0; + this.stock = stock; + + } + + public void addBagel(Bagel bag) throws Exception{ // only one using acuall bagel, only because bagel has its own "basket" of fillings + if(curSize + bag.getSize() >= getBasketSize()) // could do String... n + throw new Exception("Exceeds maximum basket size!"); + this.arr[this.curSize] = bag; + this.curSize += bag.getSize() + 1; // + 1 for bagel + } + + public void addCoffee(String sku) throws Exception{ + if(curSize >= getBasketSize()) + throw new Exception("Exceeds maximum basket size!"); + if(!this.stock.hasItem(sku)){ + throw new Exception("Not a valid SKU"); + } + this.arr[this.curSize] = new Coffee(sku, this.stock); + this.curSize++; + } + + public void addFilling(String sku) throws Exception{ + if(curSize >= getBasketSize()) + throw new Exception("Exceeds maximum basket size!"); + if(!this.stock.hasItem(sku)){ + throw new Exception("Not a valid SKU"); + } + this.arr[this.curSize] = new Filling(sku, this.stock); + this.curSize++; + } + + public boolean removeItem(String sku){ + for (int i = 0; i < this.curSize; i++){ + if(!(this.arr[i].getSKU() == sku)){ + continue; + } + for (int j = i; j < this.curSize; j++){ + if (j+1 >= this.curSize){ + this.arr[j] = null; + break; + } + this.arr[j] = this.arr[j+1]; + } + this.arr[i] = this.arr[i+1]; + this.curSize--; + return true; + + } + return false; + } + + public float costOf(String sku){ + return stock.getPrice(sku); + } + + public int getBasketSize(){ + return Basket.size; + } + + protected static void setBasketSize(int size){ + Basket.size = size; + } + + private record Pair(int count, float discount){} + + private String padding(int max, String str){ + return " ".repeat((max/2) - (str.length()/2)) + str; + } + + private String paddTwo(int max, String str1, String str2){ + int padd = max- str1.length() - str2.length(); + return str1 +" ".repeat(padd) + str2; + } + + private String buildPrint(Map skuDiscounts, float sum){ + String print =""; + String bobString = "~~~ Bob's Bagels ~~~"; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + String dateTime = LocalDateTime.now().format(formatter); + int maxStringSize = 28; + String div = "-".repeat(maxStringSize) + "\n"; + + print += padding(maxStringSize, bobString) + "\n" + padding(maxStringSize, dateTime) + "\n" + div; + String tmp = ""; + float totDisc = 0f; + for (Map.Entry entry : skuDiscounts.entrySet()) { + String sku = entry.getKey(); + Pair pair = entry.getValue(); + String nameVarient = this.stock.getName(sku) + " " + this.stock.getVarient(sku); + String price = "£" + String.format("%.2f", this.stock.getPrice(sku)*pair.count); + String amountPrice = paddTwo(10, ""+ pair.count, price); + tmp += paddTwo(maxStringSize, nameVarient, amountPrice)+ "\n"; + if(pair.discount != 0){ + tmp += paddTwo(maxStringSize, "", "(-£" + Math.abs(pair.discount) + ")") + "\n"; + totDisc += pair.discount; + } + + } + print += tmp; + print += div + paddTwo(maxStringSize, "Total", "£"+ String.format("%.2f", sum))+ "\n"; + print += "\n" + padding(maxStringSize, "You saved a total of " + "£"+ String.format("%.2f", Math.abs(totDisc))) + "\n"; + print += "\n" + padding(maxStringSize, "Thank you for buying!"); + return print; + } + + public float getTotalCost(){ + float sum = 0f; + Map skuDiscounts = new HashMap(); + + for (int i = 0; i < this.curSize; i++){ + String sku = this.arr[i].getSKU(); + int discountCondition = this.arr[i].getDiscountCondition(); + boolean containsSKU = skuDiscounts.containsKey(sku); + // System.err.println(discountCondition); + + if(!containsSKU){ + skuDiscounts.put(sku, new Pair(1, 0f)); + }else{ + skuDiscounts.computeIfPresent(sku, (s, p) -> new Pair(p.count() + 1, p.discount())); + // System.err.println(skuDiscounts.get(sku).count); + } + if(containsSKU && discountCondition != 0 && skuDiscounts.get(sku).count % discountCondition == 0){ + float discount = this.arr[i].getDiscount(); + skuDiscounts.computeIfPresent(sku, (s, p) -> new Pair(p.count(), p.discount() + discount)); + sum += discount; + } + sum += this.arr[i].getPrice(); + } + + System.out.println(this.buildPrint(skuDiscounts, sum)); + // System.err.println(this.buildPrint(skuDiscounts, sum)); + return sum; + } + + @Override + public String toString(){ + String tmp = ""; + for (int i = 0; i < this.curSize; i++){ + tmp += this.arr[i].getSKU() + " " + this.arr[i].getName() + " " + this.arr[i].getVarient() + "\n"; + } + return tmp.trim(); + } + +} diff --git a/src/main/java/com/booleanuk/core/Customer.java b/src/main/java/com/booleanuk/core/Customer.java new file mode 100644 index 000000000..dc2a7d1a7 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Customer.java @@ -0,0 +1,13 @@ +package com.booleanuk.core; + +public class Customer { + private Basket basket; + + public Customer(Inventory inventory){ + this.basket = new Basket(inventory); + } + + public Basket getBasket(){ + return this.basket; + } +} 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..553491153 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -0,0 +1,78 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class Inventory implements Stock{ + private Map inventory; + + public Inventory(){ + this.inventory = new HashMap(); + // this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion", 6, -0.45f}); + // this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain", 12, -0.69f}); + // this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything", 6, -0.45f}); + // this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame", null}); + // this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black", null}); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + // this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White", null}); + // this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino", null}); + // this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte", null}); + // this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon", null}); + // this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg", null}); + // this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese", null}); + // this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese", null}); + // this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon", null}); + // this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham", null}); + } + + @Override + public float getPrice(String sku){ + return (float) this.inventory.get(sku)[0]; + } + + @Override + public String getVarient(String sku){ + return (String) this.inventory.get(sku)[1]; + } + + @Override + public String getName(String sku){ + return (String) this.inventory.get(sku)[2]; + } + + @Override + public int getDiscountCondition(String sku){ + Object dsc = this.inventory.get(sku)[3]; + if(dsc != null){ + return (int) dsc; + }else{ + return 0; + } + } + + @Override + public float getDiscount(String sku){ + Object dsc = this.inventory.get(sku)[3]; + if(dsc != null){ + return (float) this.inventory.get(sku)[4]; + }else{ + return 0f; + } + } + + public void addEntry(String sku, float price, String varient, String name, int discountCondition, float discount){ + this.inventory.put(sku, new Object[]{price, varient, name, discountCondition, discount}); + } + + public void addEntry(String sku, float price, String varient, String name){ + this.inventory.put(sku, new Object[]{price, varient, name, null}); + } + + public void removeEntry(String sku){ + this.inventory.remove(sku); + } + + @Override + public boolean hasItem(String sku){ + return this.inventory.containsKey(sku); + } +} 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..91710e1e2 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item.java @@ -0,0 +1,85 @@ +package com.booleanuk.core; + +public class Item { + protected float price; + private String sku; + private String varient; + private String name; + private int discountCondition; + private float discount; + private Stock stock; + + public Item(String sku, Stock stock) { + setStock(stock); + try{ + setSKU(sku); + setPrice(this.stock.getPrice(sku)); + setVarient(this.stock.getVarient(sku)); + setName(this.stock.getName(sku)); + setDiscountCondition(this.stock.getDiscountCondition(sku)); + setDiscount(this.stock.getDiscount(sku)); + } + catch (Exception e){ + System.out.println("Not a valid SKU"); + throw e; + } + + } + + public float getPrice(){ + return this.price; + } + + public String getSKU(){ + return this.sku; + } + + public String getVarient(){ + return this.varient; + } + + public String getName(){ + return this.name; + } + + public int getDiscountCondition(){ + return this.discountCondition; + } + + public float getDiscount(){ + return this.discount; + } + + protected Stock getStock(){ + return this.stock; + } + + private void setPrice(float price){ + this.price = price; + } + + private void setSKU(String sku){ + this.sku = sku; + } + + private void setVarient(String varient){ + this.varient = varient; + } + + private void setName(String name){ + this.name = name; + } + + private void setDiscountCondition(int discountCondition){ + this.discountCondition = discountCondition; + } + + private void setDiscount(float discount){ + this.discount = discount; + } + + private void setStock(Stock stock){ + this.stock = stock; + } + +} diff --git a/src/main/java/com/booleanuk/core/Manger.java b/src/main/java/com/booleanuk/core/Manger.java new file mode 100644 index 000000000..8b82f4e00 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Manger.java @@ -0,0 +1,15 @@ +package com.booleanuk.core; + +public class Manger extends Basket{ + + public Manger(Inventory inventory) { + super(inventory); + //TODO Auto-generated constructor stub + } + + public static void changeBasketSize(int size){ + Basket.setBasketSize(size); + } + + //public +} 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..14fb4ca46 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Stock.java @@ -0,0 +1,10 @@ +package com.booleanuk.core; + +public interface Stock { + boolean hasItem(String sku); + public float getPrice(String sku); + public String getVarient(String sku); + public String getName(String sku); + public int getDiscountCondition(String sku); + public float getDiscount(String sku); +} diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java new file mode 100644 index 000000000..2dc4486d7 --- /dev/null +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -0,0 +1,46 @@ +package com.booleanuk.core.item_types; + +import com.booleanuk.core.Stock; +import com.booleanuk.core.Item; + +import java.util.ArrayList; +import java.lang.Exception; +import java.util.List; + +public class Bagel extends Item{ + private List filling; + private float totalPrice; + + public Bagel(String sku, Stock stock) throws Exception{ + super(sku, stock); + boolean contains = stock.hasItem(sku) & sku.contains("BGL"); + if(!contains){ + throw new Exception("Must be a valid bagel SKU!"); + } + this.filling = new ArrayList(); + + } + + public int getSize(){ + return this.filling.size(); + } + + public void addFilling(String sku) throws Exception{ + Filling filling = new Filling(sku, this.getStock()); + this.filling.add(filling); + this.totalPrice += filling.getPrice(); + } + + public void removeFIlling(String sku){ + for(int i = 0; i < this.filling.size(); i++){ + if(this.filling.get(i).getSKU() == sku) + this.filling.remove(i); + } + } + + @Override + public float getPrice(){ + return this.price + this.totalPrice; + } + +} diff --git a/src/main/java/com/booleanuk/core/item_types/Coffee.java b/src/main/java/com/booleanuk/core/item_types/Coffee.java new file mode 100644 index 000000000..0e134a85e --- /dev/null +++ b/src/main/java/com/booleanuk/core/item_types/Coffee.java @@ -0,0 +1,18 @@ +package com.booleanuk.core.item_types; + +import com.booleanuk.core.Stock; +import com.booleanuk.core.Item; + +import java.lang.Exception; + +public class Coffee extends Item { + + public Coffee(String sku, Stock stock) throws Exception{ + super(sku, stock); + boolean contains = stock.hasItem(sku) & sku.contains("COF"); + if(!contains){ + throw new Exception("Must be a valid coffee SKU!"); + } + + } +} diff --git a/src/main/java/com/booleanuk/core/item_types/Filling.java b/src/main/java/com/booleanuk/core/item_types/Filling.java new file mode 100644 index 000000000..92a0fccc0 --- /dev/null +++ b/src/main/java/com/booleanuk/core/item_types/Filling.java @@ -0,0 +1,19 @@ +package com.booleanuk.core.item_types; + +import com.booleanuk.core.Stock; +import com.booleanuk.core.Item; + +import java.lang.Exception; + +public class Filling extends Item { + + + public Filling(String sku, Stock stock) throws Exception{ + super(sku, stock); + boolean contains = stock.hasItem(sku)& sku.contains("FIL"); + if(!contains){ + throw new Exception("Must be a valid filling SKU!"); + } + + } +} 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..02550319a --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,159 @@ +package com.booleanuk.core; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.booleanuk.core.item_types.Bagel; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.lang.Exception; + +public class BasketTest { + + private Basket b; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + private Inventory inventory; + + public BasketTest() { + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + this.b = new Basket(inventory); + } + + @Test + public void addBagelTest() throws Exception{ + Bagel bag = new Bagel("BGLO", inventory); + b.addBagel(bag); + Assertions.assertEquals( "BGLO Onion Bagel", b.toString()); + } + + @Test + public void addFillingTest() throws Exception{ + b.addFilling("FILB"); + Assertions.assertEquals("FILB Bacon Filling", b.toString()); + } + + @Test + public void addCoffeeTest() throws Exception{ + b.addCoffee("COFB"); + Assertions.assertEquals("COFB Black Coffee", b.toString()); + } + + @Test + public void getCostTest(){ + //"BGLP" Bagel plain 0.39 + Assertions.assertEquals(0.39, b.costOf("BGLP"), 0.0001); + } + + @Test + public void removeItemTest() throws Exception{ + b.addCoffee("COFB"); + Assertions.assertTrue(b.toString().contains("Black")); + Assertions.assertTrue(b.removeItem("COFB")); + Assertions.assertEquals("", b.toString()); + Assertions.assertFalse(b.removeItem("COFB")); + } + + @Test + public void getTotalCostTest() throws Exception{ + Bagel bag = new Bagel("BGLP", inventory); + b.addBagel(bag); // 0.39 + b.addCoffee("COFB"); // 0.99 + bag.addFilling("FILB"); // 0.12 + Assertions.assertEquals(0.39 + 0.99 + 0.12, b.getTotalCost(), 0.0001); + } + + @Test + void throwsWhenBasketIsFull() throws Exception { + Bagel bag = new Bagel("BGLO", inventory); + + for (int i = 0; i < b.getBasketSize(); i++){ + b.addBagel(bag); + } + Exception exception = assertThrows(Exception.class, () -> b.addBagel(bag)); + assertEquals("Exceeds maximum basket size!", exception.getMessage()); + } + + @Test + void throwsWhenBasketIsFull_GigaBagel() throws Exception{ + Bagel bag = new Bagel("BGLO", inventory); + for (int i = 0; i < b.getBasketSize(); i++){ + bag.addFilling("FILB"); + } + Exception exception = assertThrows(Exception.class, () -> b.addBagel(bag)); + assertEquals("Exceeds maximum basket size!", exception.getMessage()); + } + + @Test + public void discountAppliedTest() throws Exception{ + Bagel bagO = new Bagel("BGLO", inventory); + Bagel bagP = new Bagel("BGLP", inventory); + Bagel bagE = new Bagel("BGLE", inventory); + + + for(int i = 0; i < 16; i++){ + b.addBagel(bagP); + } + Assertions.assertEquals(5.55, b.getTotalCost(), 0.0001); + for(int i = 0; i < 6; i++){ + if(i < 2) + b.addBagel(bagO); + if(i < 3) + b.addCoffee("COFB"); + if(i < 4) + b.removeItem("BGLP"); + b.addBagel(bagE); + } + Assertions.assertEquals(10.43, b.getTotalCost(), 0.0001); + } + + @BeforeEach + void setUp() { + System.setOut(new PrintStream(outContent)); + } + + @AfterEach + void tearDown() { + System.setOut(originalOut); + } + + @Test + void printsExpectedReceipt() throws Exception { + Bagel bagP = new Bagel("BGLP", inventory); + for(int i = 0; i < 16; i++){ + b.addBagel(bagP); + } + b.getTotalCost(); + String printed = outContent.toString(); + Assertions.assertTrue(printed.contains("~~~ Bob's Bagels ~~~")); + Assertions.assertTrue(printed.contains("----------------------------")); + Assertions.assertTrue(printed.contains("Plain Bagel")); + Assertions.assertTrue(printed.contains("16")); + Assertions.assertTrue(printed.contains("£6.24")); + Assertions.assertTrue(printed.contains("(-£0.69)")); + Assertions.assertTrue(printed.contains("Total")); + Assertions.assertTrue(printed.contains("£5.55")); + Assertions.assertTrue(printed.contains("You saved a total of £0.69")); + Assertions.assertTrue(printed.contains("Thank you for buying!")); + } +} diff --git a/src/test/java/com/booleanuk/core/CustomerTest.java b/src/test/java/com/booleanuk/core/CustomerTest.java new file mode 100644 index 000000000..9497e0671 --- /dev/null +++ b/src/test/java/com/booleanuk/core/CustomerTest.java @@ -0,0 +1,35 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class CustomerTest { + private Customer c; + private Inventory inventory; + + public CustomerTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + this.c = new Customer(this.inventory); + } + + @Test + public void getBasketTest(){ + Assertions.assertInstanceOf(Basket.class, c.getBasket()); + } + +} diff --git a/src/test/java/com/booleanuk/core/ItemTest.java b/src/test/java/com/booleanuk/core/ItemTest.java new file mode 100644 index 000000000..80e82d975 --- /dev/null +++ b/src/test/java/com/booleanuk/core/ItemTest.java @@ -0,0 +1,60 @@ +package com.booleanuk.core; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Assertions; + +import org.junit.jupiter.api.Test; + +public class ItemTest { + + private Inventory inventory; + + public ItemTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } + + @Test + void throwsWhenSkuDoesNotExistAtAll() { + assertThrows(NullPointerException.class, () -> new Item("ZZZZ", inventory)); + } + + @Test + public void getPriceTest(){ + Item i = new Item("COFB", inventory); + Assertions.assertEquals(0.99, i.getPrice(), 0.0001); + } + + @Test + public void getSKUTest(){ + Item i = new Item("COFB", inventory); + Assertions.assertEquals("COFB", i.getSKU()); + } + + @Test + public void getVarientTest(){ + Item i = new Item("COFB", inventory); + Assertions.assertEquals("Coffee", i.getVarient()); + } + + @Test + public void getNameTest(){ + Item i = new Item("COFB", inventory); + Assertions.assertEquals("Black", i.getName()); + } +} diff --git a/src/test/java/com/booleanuk/core/MangerTest.java b/src/test/java/com/booleanuk/core/MangerTest.java new file mode 100644 index 000000000..7d9e326d9 --- /dev/null +++ b/src/test/java/com/booleanuk/core/MangerTest.java @@ -0,0 +1,34 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class MangerTest { + private Manger m; + private Inventory inventory; + + public MangerTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + this.m = new Manger(this.inventory); + } + + @Test + public void changeBasketSizeTest(){ + Manger.changeBasketSize(35); + Assertions.assertEquals(35, m.getBasketSize()); + } +} diff --git a/src/test/java/com/booleanuk/core/StockTest.java b/src/test/java/com/booleanuk/core/StockTest.java new file mode 100644 index 000000000..6b92b3bf5 --- /dev/null +++ b/src/test/java/com/booleanuk/core/StockTest.java @@ -0,0 +1,92 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Assertions; + +import com.booleanuk.core.item_types.Bagel; + +public class StockTest { + + private Stock stock; + private Inventory inventory; + private Basket b; + + public class MiniStock implements Stock{ + private String sku; + + public MiniStock(String sku){ + this.sku = sku; + } + + @Override + public boolean hasItem(String sku){ + return this.sku == sku; + } + + @Override + public float getPrice(String sku){ + return 0.49f; + } + + @Override + public String getVarient(String sku){ + return "Bagel"; + } + + @Override + public String getName(String sku){ + return "Whole Wheat"; + } + + @Override + public int getDiscountCondition(String sku){ + return 4; + } + + @Override + public float getDiscount(String sku){ + return -0.15f; + } + } + + public StockTest(){ + this.stock = new MiniStock("BGLW"); + + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + this.b = new Basket(stock); + } + + @Test + public void stockTest() throws Exception{ + Bagel bag = new Bagel("BGLW", this.stock); + b.addBagel(bag); + Assertions.assertEquals("BGLW Whole Wheat Bagel", b.toString()); + } + + @Test + void throwsSKUinvalid() throws Exception{ + assertThrows(NullPointerException.class, () -> new Bagel("BGLW", this.inventory)); + + Exception exception = assertThrows(Exception.class, () -> b.addFilling("FILB")); + assertEquals("Not a valid SKU", exception.getMessage()); + } +} diff --git a/src/test/java/com/booleanuk/core/item_types/BagelTest.java b/src/test/java/com/booleanuk/core/item_types/BagelTest.java new file mode 100644 index 000000000..3c170e392 --- /dev/null +++ b/src/test/java/com/booleanuk/core/item_types/BagelTest.java @@ -0,0 +1,66 @@ +package com.booleanuk.core.item_types; + + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Assertions; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import com.booleanuk.core.Inventory; + +public class BagelTest{ + + private Inventory inventory; + + public BagelTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } + + @Test + void throwsWhenSkuIsValidButNotABagel() { + Exception exception = assertThrows(Exception.class, () -> new Bagel("COFB", inventory)); + assertEquals("Must be a valid bagel SKU!", exception.getMessage()); + } + + @Test + public void getSize_addFillingTest() throws Exception{ + Bagel bag = new Bagel("BGLO", inventory); + Assertions.assertEquals(0, bag.getSize()); + bag.addFilling("FILB"); + Assertions.assertEquals(1, bag.getSize()); + } + + @Test + public void removeFillingTest() throws Exception{ + Bagel bag = new Bagel("BGLO", inventory); + bag.addFilling("FILB"); + Assertions.assertEquals(1, bag.getSize()); + bag.removeFIlling("FILB"); + Assertions.assertEquals(0, bag.getSize()); + } + + @Test + public void getPriceTest() throws Exception{ + Bagel bag = new Bagel("BGLO", inventory); //0.49 + bag.addFilling("FILB"); // 0.12 + Assertions.assertEquals(0.61, bag.getPrice(), 0.0001); + } + +} diff --git a/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java b/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java new file mode 100644 index 000000000..832021f23 --- /dev/null +++ b/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java @@ -0,0 +1,38 @@ +package com.booleanuk.core.item_types; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import com.booleanuk.core.Inventory; + +class CoffeeTest { + + private Inventory inventory; + + public CoffeeTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } + + @Test + void throwsWhenSkuIsValidButNotACoffee() { + Exception exception = assertThrows(Exception.class, () -> new Coffee("BGLO", inventory)); + assertEquals("Must be a valid coffee SKU!", exception.getMessage()); + } + +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/core/item_types/FillingTest.java b/src/test/java/com/booleanuk/core/item_types/FillingTest.java new file mode 100644 index 000000000..9dcec1609 --- /dev/null +++ b/src/test/java/com/booleanuk/core/item_types/FillingTest.java @@ -0,0 +1,37 @@ +package com.booleanuk.core.item_types; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +import com.booleanuk.core.Inventory; + +public class FillingTest { + + private Inventory inventory; + + public FillingTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } + + @Test + void throwsWhenSkuIsValidButNotAFilling() { + Exception exception = assertThrows(Exception.class, () -> new Filling("BGLO", inventory)); + assertEquals("Must be a valid filling SKU!", exception.getMessage()); + } +}