diff --git a/Class_diagram.png b/Class_diagram.png new file mode 100644 index 000000000..fc3533329 Binary files /dev/null and b/Class_diagram.png differ diff --git a/domain-model.md b/domain-model.md new file mode 100644 index 000000000..248c1ef08 --- /dev/null +++ b/domain-model.md @@ -0,0 +1,131 @@ +| Module | Class | Member | Method | Scenario | Output | +|--------|------------------|--------------------|-------------------------------|------------------------------|------------| +| Core | 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 | price | +| | | | | 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(Filling filling) | | void | +| | | | removeFilling(String variant) | Filling exists in list | true | +| | Coffee | | | Filling does not exist | false | +| | Filling | | | | | +| | | | | | | +Extension 1: + As a user I want to take part of the special offers so that I can save money. + +| Module | Class | Member | Method | Scenario | Output | +|--------|---------------|--------------------|-------------------------------|------------------------------|---------------| +| Core | 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 | +| | | | getDiscountetCost() | | | +| | Stock | List stock | | | | +| | | | getPrice(String sku) | Exists | price | +| | | | | 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(Filling filling) | | void | +| | | | removeFilling(String variant) | Filling exists in list | true | +| | | | | Filling does not exist | false | +| | | | getFillings() | | List | +| | Coffee | | | | | +| | Filling | | | | | +| | | | | | | + +Extension 2: +As a user I want to get a reciept with my order so that I can know what I have bought. + +| Module | Class | Member | Method | Scenario | Output | +|--------|---------------|--------------------|-------------------------------|------------------------------|-----------------| +| Core | 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 | +| | | | getDiscountetCost() | | | +| | | | getPriceInfo() | | List | +| | Stock | List stock | | | | +| | | | getPrice(String sku) | Exists | price | +| | | | | 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(Filling filling) | | void | +| | | | removeFilling(String variant) | Filling exists in list | true | +| | | | | Filling does not exist | false | +| | | | getFillings() | | List | +| | Coffee | | | | | +| | Filling | | | | | +| | | | | | | +| | Reciept | Basket basket | getReciept() | | String | +| | | | | | | +| | PriceInfo | String name | | | | +| | | float cost | | | | +| | | float discount | | | | +| | | int count | | | | + +Extension 3: +As a user I want to get a reciept with my order and discounts so that I can know what I have bought andhow much I have saved. + +| Module | Class | Member | Method | Scenario | Output | +|--------|---------------|--------------------|-------------------------------|------------------------------|-----------------| +| Core | 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 | +| | | | getDiscountetCost() | | | +| | | | getPriceInfo() | | List | +| | Stock | List stock | | | | +| | | | getPrice(String sku) | Exists | price | +| | | | | 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(Filling filling) | | void | +| | | | removeFilling(String variant) | Filling exists in list | true | +| | | | | Filling does not exist | false | +| | | | getFillings() | | List | +| | Coffee | | | | | +| | Filling | | | | | +| | | | | | | +| | Reciept | Basket basket | getReciept() | | String | +| | | | getDiscountedReciept() | | String | +| | PriceInfo | String name | | | | +| | | float cost | | | | +| | | float discount | | | | +| | | int count | | | | + 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..59d3ff7c4 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Bagel.java @@ -0,0 +1,40 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class Bagel extends Item{ + List fillings; + + public Bagel(String sku, float price, String variant){ + super(sku, price, "Bagel", variant); + this.fillings = new ArrayList<>(); + } + + public void addFilling(Filling filling){ + this.fillings.add(filling); + } + + public boolean removeFilling(String variant){ + for(int i = 0; i < this.fillings.size(); i++){ + if(this.fillings.get(i).getVariant().equals(variant)){ + this.fillings.remove(i); + return true; + } + } + return false; + } + + public List getFillings(){ + return this.fillings; + } + + @Override + public float getPrice(){ + float acc = 0; + for(Filling filling : this.fillings){ + acc += filling.getPrice(); + } + return super.getPrice() + acc; + } +} 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..ed9a29eeb --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,58 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class Basket { + private List items; + private int capacity; + static private int stdCapacity = 3; + static private Stock stock = new Stock(); + + public Basket(){ + this.capacity = stdCapacity; + items = new ArrayList<>(); + } + + public Basket(int capacity){ + this.capacity = capacity; + items = new ArrayList<>(); + } + + public List getItems(){ + return this.items; + } + + public int add(String sku) { + if(items.size() >= capacity){ + return 0; + } + + Item addItem = stock.getItem(sku); + + if(addItem == null){ + return -1; + } + + items.add(addItem); + return 1; + } + + public boolean remove(String sku){ + for(int i = 0; i < this.items.size(); i++){ + if(this.items.get(i).getSku().equals(sku)){ + this.items.remove(i); + return true; + } + } + return false; + } + + public float getTotalCost(){ + float acc = 0.0f; + for(Item curr : this.items){ + acc = acc + curr.getPrice(); + } + return acc; + } +} \ No newline at end of file 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..0f87bbabd --- /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 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..bbeffab61 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Filling.java @@ -0,0 +1,10 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +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..a85bd9223 --- /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 Item(String sku, float price, String name, String variant){ + this.sku = sku; + this.price = price; + this.name = name; + this.variant = variant; + } + + public String getSku(){ + return this.sku; + } + + public float getPrice(){ + return this.price; + } + + public String getName(){ + return this.name; + } + + public String getVariant(){ + return this.variant; + } +} \ No newline at end of file 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..530728f14 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Stock.java @@ -0,0 +1,56 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.List; + +public class Stock { + private List stock; + + public Stock(){ + stock = new ArrayList<>(); + + stock.addAll(List.of(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 float getPrice(String sku){ + for(Item item : this.stock){ + if(item.getSku().equals(sku)){ + return item.getPrice(); + } + } + return -1f; + } + + public List getFillings(){ + List retList = new ArrayList<>(); + + for(Item item : this.stock){ + if(item.getName().equals("Filling")){ + retList.add(item); + } + } + return retList; + } + + public Item getItem(String sku){ + for(Item item : this.stock){ + if(item.getSku().equals(sku)){ + return item; + } + } + return null; + } +} \ No newline at end of file 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..3f0e6dfaa --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Bagel.java @@ -0,0 +1,40 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.List; + +public class Bagel extends Item{ + List fillings; + + public Bagel(String sku, float price, String variant){ + super(sku, price, "Bagel", variant); + this.fillings = new ArrayList<>(); + } + + public void addFilling(Filling filling){ + this.fillings.add(filling); + } + + public boolean removeFilling(String variant){ + for(int i = 0; i < this.fillings.size(); i++){ + if(this.fillings.get(i).getVariant().equals(variant)){ + this.fillings.remove(i); + return true; + } + } + return false; + } + + public List getFillings(){ + return this.fillings; + } + + @Override + public float getPrice(){ + float acc = 0; + for(Filling filling : this.fillings){ + acc += filling.getPrice(); + } + return super.getPrice() + acc; + } +} 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..6eea1849c --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Basket.java @@ -0,0 +1,187 @@ +package com.booleanuk.extension; + +import java.util.*; + +public class Basket { + private List items; + private int capacity; + static private int stdCapacity = 3; + static private Stock stock = new Stock(); + + public Basket(){ + this.capacity = stdCapacity; + this.initAttributes(); + } + + public Basket(int capacity){ + this.capacity = capacity; + this.initAttributes(); + } + + private void initAttributes(){ + items = new ArrayList<>(); + } + + public List getItems(){ + return this.items; + } + + public int add(String sku) { + if(items.size() >= capacity){ + return 0; + } + + Item addItem = stock.getItem(sku); + + if(addItem == null){ + return -1; + } + + items.add(addItem); + return 1; + } + + public boolean remove(String sku){ + for(int i = 0; i < this.items.size(); i++){ + if(this.items.get(i).getSku().equals(sku)){ + this.items.remove(i); + return true; + } + } + return false; + } + + public float getTotalCost(){ + float acc = 0.0f; + for(Item curr : this.items){ + acc = acc + curr.getPrice(); + } + return acc; + } + + private Map countItems(){ + Map itemCount = new HashMap<>(); + for(Item item : this.items){ + String sku = item.getSku(); + + itemCount.putIfAbsent(sku, 0); + itemCount.put(sku, itemCount.get(sku)+1); + + if(item instanceof Bagel){ + Bagel bagel = (Bagel) item; + for(Filling filling : bagel.getFillings()){ + sku = filling.getSku(); + itemCount.putIfAbsent(sku, 0); + itemCount.put(sku, itemCount.get(sku)+1); + } + } + } + return itemCount; + } + + public void updatePriceInfo(){ + //this.priceInfo.clear(); + + Map itemCount = countItems(); + + } + + public List getPriceInfo(){ + Map inputMap = this.countItems(); + List infos = new ArrayList<>(); + + Set> entries = inputMap.entrySet(); + + for(Map.Entry entry : entries) { + + Item item = stock.getItem(entry.getKey()); + String name = item.getVariant() + " " + item.getName(); + + PriceInfo currInfo = new PriceInfo(name, 0, 0, 0); + + + String key = entry.getKey(); + Integer val = entry.getValue(); + while (key.toLowerCase().contains("bgl") && val >= 12) { + currInfo.incCost(3.99f); + currInfo.incCount(12); + currInfo.incDiscount(stock.getPrice(key) * 12 - 3.99f); + val -= 12; + entry.setValue(val); + } + + while (key.toLowerCase().contains("bgl") && val >= 6) { + currInfo.incCost(2.49f); + currInfo.incCount(6); + currInfo.incDiscount(stock.getPrice(key) * 6 - 2.49f); + val -= 6; + entry.setValue(val); + } + + while (val > 0) { + if (key.toLowerCase().contains("bgl")) { + Map.Entry coffee = findCoffee(entries); + if (coffee != null) { + coffee.setValue(coffee.getValue() - 1); + val -= 1; + entry.setValue(val); + + PriceInfo coffeeCombo = getPriceInfoByName(infos, "Coffee Combo"); + if(coffeeCombo == null){ + coffeeCombo = new PriceInfo("Coffee Combo", 0, 0, 0); + infos.add(coffeeCombo); + } + + coffeeCombo.incCost(1.25f); + coffeeCombo.incCount(1); + float discount = stock.getPrice(coffee.getKey()) + stock.getPrice(key) - 1.25f; + coffeeCombo.incDiscount(discount); + continue; //Used now for simplicity, no good though + } + } + currInfo.incCost(stock.getPrice(key)); + currInfo.incCount(1); + val -= 1; + entry.setValue(val); + } + if(currInfo.getCount() > 0) { + infos.add(currInfo); + } + } + return infos; + } + + public PriceInfo getPriceInfoByName(List entries,String name){ + for(PriceInfo entry : entries){ + if(entry.getName().equals(name)){ + return entry; + } + } + return null; + } + + public float priceWithDiscount(){ + + List priceInfos = getPriceInfo(); + float acc = 0; + for(PriceInfo info : priceInfos){ + acc += info.getCost(); + } + return acc; + } + + private Map.Entry findCoffee(Set> itemCount){ + for(Map.Entry entry : itemCount){ + if(entry.getKey().toLowerCase().contains("cof") && entry.getValue() > 0){ + return entry; + } + } + return null; + } + + + + public String getReciept(){ + return ""; + } +} \ No newline at end of file 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..25418e52f --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Coffee.java @@ -0,0 +1,7 @@ +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..de6142195 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Filling.java @@ -0,0 +1,10 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; + +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..7be4d47b5 --- /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 Item(String sku, float price, String name, String variant){ + this.sku = sku; + this.price = price; + this.name = name; + this.variant = variant; + } + + public String getSku(){ + return this.sku; + } + + public float getPrice(){ + return this.price; + } + + public String getName(){ + return this.name; + } + + public String getVariant(){ + return this.variant; + } +} \ No newline at end of file diff --git a/src/main/java/com/booleanuk/extension/PriceInfo.java b/src/main/java/com/booleanuk/extension/PriceInfo.java new file mode 100644 index 000000000..abdfebe3f --- /dev/null +++ b/src/main/java/com/booleanuk/extension/PriceInfo.java @@ -0,0 +1,60 @@ +package com.booleanuk.extension; + +public class PriceInfo { + private String name; + private int count; + private float discount; + private float cost; + + public PriceInfo(String name, int count, float cost, float discount){ + this.name = name; + this.count = count; + this.cost = cost; + this.discount = discount; + } + + public float getDiscount() { + return discount; + } + + public int getCount() { + return count; + } + + public String getName() { + return name; + } + + public float getCost() { + return cost; + } + + public void setName(String name) { + this.name = name; + } + + public void setDiscount(float discount) { + this.discount = discount; + } + + public void setCount(int count) { + this.count = count; + } + + public void setCost(float cost) { + this.cost = cost; + } + + public void incDiscount(float add){ + this.setDiscount(this.getDiscount() + add); + } + + public void incCount(int add){ + this.setCount(this.getCount() + add); + } + + public void incCost(float add){ + this.setCost(this.getCost() + add); + } + +} diff --git a/src/main/java/com/booleanuk/extension/Reciept.java b/src/main/java/com/booleanuk/extension/Reciept.java new file mode 100644 index 000000000..0c421f286 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Reciept.java @@ -0,0 +1,83 @@ +package com.booleanuk.extension; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class Reciept { + private Basket basket; + + public Reciept(Basket basket){ + this.basket = basket; + } + + public String getReciept(){ + return buildReciept(false); + } + public String getDiscountedReciept(){ + return buildReciept(true); + } + + public String buildReciept(boolean discounted){ + StringBuilder sb = new StringBuilder(); + + sb.append(" ~~~ Bob's Bagels ~~~\n"); + sb.append("\n"); + LocalDateTime now = LocalDateTime.now(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + String formattedDateTime = now.format(formatter); + + sb.append(formattedDateTime); + sb.append("\n"); + sb.append("\n"); + sb.append("----------------------------"); + sb.append("\n"); + int countStart = 19; + int priceStart = 4; + List priceInfo = basket.getPriceInfo(); + float acc = 0; + float discountedAcc = 0; + for(PriceInfo curr : priceInfo){ + sb.append(curr.getName()); + sb.append(blankSpace(curr.getName(), countStart)); + sb.append(curr.getCount()); + sb.append(blankSpace(String.valueOf(curr.getCount()), priceStart)); + sb.append("£"); + sb.append(curr.getCost()); + sb.append("\n"); + acc += curr.getCost(); + if(discounted && curr.getDiscount() > 0){ + sb.append(blankSpace("", countStart+priceStart-2)); + sb.append("(-£"); + sb.append(String.format("%.2f",curr.getDiscount())); + sb.append(")\n"); + discountedAcc += curr.getDiscount(); + } + } + sb.append("\n"); + sb.append("----------------------------\n"); + sb.append("Total"); + sb.append(blankSpace("Total", countStart+priceStart)); + sb.append(String.format( "£%.2f", acc)); + if(discounted && discountedAcc > 0){ + sb.append("\n"); + sb.append("You saved a total of £"); + sb.append(String.format("%.2f\n", discountedAcc)); + sb.append(" on this shop"); + } + + return sb.toString(); + } + + public String blankSpace(String nameVar, int startIndex){ + int offset = startIndex-nameVar.length(); + + StringBuilder sb = new StringBuilder(); + sb.repeat(" ", offset); + return sb.toString(); + } + +} 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..eb020d39f --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Stock.java @@ -0,0 +1,69 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.List; + +public class Stock { + private List stock; + + public Stock(){ + stock = new ArrayList<>(); + + stock.addAll(List.of(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 float getPrice(String sku){ + for(Item item : this.stock){ + if(item.getSku().equals(sku)){ + return item.getPrice(); + } + } + return -1f; + } + + public List getFillings(){ + List retList = new ArrayList<>(); + + for(Item item : this.stock){ + if(item.getName().equals("Filling")){ + retList.add(item); + } + } + return retList; + } + + public Item getItem(String sku){ + for(Item item : this.stock){ + if(item.getSku().equals(sku)){ + return copyItem(item); + } + } + return null; + } + + private Item copyItem(Item item){ + if(item instanceof Filling) { + return new Filling(item.getSku(), item.getPrice(), item.getVariant()); + } + 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()); + } + return null; + } +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/core/BagelTest.java b/src/test/java/com/booleanuk/core/BagelTest.java new file mode 100644 index 000000000..444a3dca2 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BagelTest.java @@ -0,0 +1,24 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BagelTest { + + @Test + public void testAddAndRemoveFilling(){ + Bagel bagel = new Bagel("BGLO", 0.49f, "Onion"); + Filling filling = new Filling("FILB", 0.12f, "Bacon"); + + Assertions.assertTrue(bagel.getFillings().isEmpty()); + + bagel.addFilling(filling); + + Assertions.assertEquals(1, bagel.getFillings().size()); + + bagel.removeFilling("Bacon"); + + Assertions.assertTrue(bagel.getFillings().isEmpty()); + } + +} 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..d2b61cf02 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,66 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + @Test + public void testAddCorrectItemBasketNotFull(){ + Basket basket = new Basket(3); + Assertions.assertTrue(basket.getItems().isEmpty()); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertFalse(basket.getItems().isEmpty()); + } + + @Test + public void testAddIncorrectItemBasketNotFull() { + Basket basket = new Basket(3); + Assertions.assertTrue(basket.getItems().isEmpty()); + Assertions.assertEquals(-1, basket.add("test")); + Assertions.assertTrue(basket.getItems().isEmpty()); + } + + @Test + public void testAddCorrectItemBasketFull(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + basket.add("BGLO"); + basket.add("BGLO"); + + Assertions.assertEquals(0, basket.add("BGLO")); + } + + @Test + public void testRemovePresentItem(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + + Assertions.assertTrue(basket.remove("BGLO")); + Assertions.assertTrue(basket.getItems().isEmpty()); + } + + @Test + public void testRemoveNonPresentItem(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + + Assertions.assertFalse(basket.remove("test")); + Assertions.assertEquals(1, basket.getItems().size()); + } + + @Test + public void testGetTotalCostItemsPresent(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + Assertions.assertEquals(0.49, basket.getTotalCost(), 0.0001f); + + basket.add("BGLO"); + Assertions.assertEquals(0.98, basket.getTotalCost(), 0.0001f); + } + + @Test + public void testGetTotalCostEmpty(){ + Basket basket = new Basket(3); + Assertions.assertEquals(0, basket.getTotalCost()); + } +} \ No newline at end of file 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..4f880496e --- /dev/null +++ b/src/test/java/com/booleanuk/core/ItemTest.java @@ -0,0 +1,8 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemTest { + +} \ No newline at end of file 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..eca3676ea --- /dev/null +++ b/src/test/java/com/booleanuk/core/StockTest.java @@ -0,0 +1,51 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class StockTest { + + @Test + public void testGetPriceValidCode(){ + Stock stock = new Stock(); + + Assertions.assertEquals(0.49f, stock.getPrice("BGLO")); + Assertions.assertEquals(1.29f, stock.getPrice("COFL")); + } + + @Test + public void testGetPriceInvalidCode(){ + Stock stock = new Stock(); + + Assertions.assertEquals(-1, stock.getPrice("erqwer")); + Assertions.assertEquals(-1, stock.getPrice("\n")); + } + + @Test + public void testGetFillings(){ + Stock stock = new Stock(); + + List fillings = stock.getFillings(); + Assertions.assertEquals(6, fillings.size()); + + for(Item filling : fillings){ + Assertions.assertEquals("Filling", filling.getName()); + } + } + + @Test + public void testGetItemExists(){ + Stock stock = new Stock(); + + Assertions.assertEquals("BGLO", stock.getItem("BGLO").getSku()); + } + + @Test + public void testGetItemCodeDoesNotExist(){ + Stock stock = new Stock(); + + Assertions.assertNull(stock.getItem("test")); + } +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/extension/BagelTest.java b/src/test/java/com/booleanuk/extension/BagelTest.java new file mode 100644 index 000000000..0c1d06c6b --- /dev/null +++ b/src/test/java/com/booleanuk/extension/BagelTest.java @@ -0,0 +1,24 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BagelTest { + + @Test + public void testAddAndRemoveFilling(){ + Bagel bagel = new Bagel("BGLO", 0.49f, "Onion"); + Filling filling = new Filling("FILB", 0.12f, "Bacon"); + + Assertions.assertTrue(bagel.getFillings().isEmpty()); + + bagel.addFilling(filling); + + Assertions.assertEquals(1, bagel.getFillings().size()); + + bagel.removeFilling("Bacon"); + + Assertions.assertTrue(bagel.getFillings().isEmpty()); + } + +} diff --git a/src/test/java/com/booleanuk/extension/BasketTest.java b/src/test/java/com/booleanuk/extension/BasketTest.java new file mode 100644 index 000000000..175f112ce --- /dev/null +++ b/src/test/java/com/booleanuk/extension/BasketTest.java @@ -0,0 +1,93 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + @Test + public void testAddCorrectItemBasketNotFull(){ + Basket basket = new Basket(3); + Assertions.assertTrue(basket.getItems().isEmpty()); + Assertions.assertEquals(1, basket.add("BGLO")); + Assertions.assertFalse(basket.getItems().isEmpty()); + } + + @Test + public void testAddIncorrectItemBasketNotFull() { + Basket basket = new Basket(3); + Assertions.assertTrue(basket.getItems().isEmpty()); + Assertions.assertEquals(-1, basket.add("test")); + Assertions.assertTrue(basket.getItems().isEmpty()); + } + + @Test + public void testAddCorrectItemBasketFull(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + basket.add("BGLO"); + basket.add("BGLO"); + + Assertions.assertEquals(0, basket.add("BGLO")); + } + + @Test + public void testRemovePresentItem(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + + Assertions.assertTrue(basket.remove("BGLO")); + Assertions.assertTrue(basket.getItems().isEmpty()); + } + + @Test + public void testRemoveNonPresentItem(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + + Assertions.assertFalse(basket.remove("test")); + Assertions.assertEquals(1, basket.getItems().size()); + } + + @Test + public void testGetTotalCostItemsPresent(){ + Basket basket = new Basket(3); + basket.add("BGLO"); + Assertions.assertEquals(0.49, basket.getTotalCost(), 0.0001f); + + basket.add("BGLO"); + Assertions.assertEquals(0.98, basket.getTotalCost(), 0.0001f); + } + + @Test + public void testGetTotalCostEmpty(){ + Basket basket = new Basket(3); + Assertions.assertEquals(0, basket.getTotalCost()); + } + + public boolean addMult(Basket basket, String sku, int times){ + if(times == 0){ + return true; + } + + return basket.add(sku) == 1 && addMult(basket, sku, times-1); + } + + @Test + public void testPriceWithDiscountNonEmpty(){ + Basket basket = new Basket(50); + Assertions.assertTrue(addMult(basket, "BGLO", 2)); + Assertions.assertTrue(addMult(basket, "BGLP", 12)); + Assertions.assertTrue(addMult(basket, "BGLE", 6)); + Assertions.assertTrue(addMult(basket, "COFB", 3)); + + float delta = 0.001f; + + Assertions.assertEquals(9.97, basket.priceWithDiscount(), delta); + + basket = new Basket(50); + + Assertions.assertTrue(addMult(basket, "BGLP", 16)); + Assertions.assertEquals(5.55, basket.priceWithDiscount(), delta); + + } +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/extension/ItemTest.java b/src/test/java/com/booleanuk/extension/ItemTest.java new file mode 100644 index 000000000..37ac49b51 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/ItemTest.java @@ -0,0 +1,8 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemTest { + +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/extension/RecieptTest.java b/src/test/java/com/booleanuk/extension/RecieptTest.java new file mode 100644 index 000000000..022437d3a --- /dev/null +++ b/src/test/java/com/booleanuk/extension/RecieptTest.java @@ -0,0 +1,56 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +public class RecieptTest { + + private boolean addMult(Basket basket, String sku, int times){ + boolean ret = true; + for(int i = 0; i < times; i++){ + ret = ret && basket.add(sku) == 1; + } + + return ret; + } + + @Test + public void testPrintRecieptNormal(){ + + Basket basket = new Basket(50); + addMult(basket,"BGLO", 2); + addMult(basket,"BGLP", 12); + addMult(basket,"BGLE", 6); + addMult(basket,"COFB", 3); + + Reciept reciept = new Reciept(basket); + System.out.println(reciept.getReciept()); + Assertions.assertTrue(reciept.getReciept().contains("Plain Bagel 12 £3.99")); + Assertions.assertTrue(reciept.getReciept().contains("Everything Bagel 6 £2.49")); + Assertions.assertTrue(reciept.getReciept().contains("Black Coffee 1 £0.99")); + Assertions.assertTrue(reciept.getReciept().contains("Coffee Combo 2 £2.5")); + Assertions.assertTrue(reciept.getReciept().contains("Total £9.97")); + } + + @Test + public void testPrintDiscountedRecieptNormal(){ + + Basket basket = new Basket(50); + addMult(basket,"BGLO", 2); + addMult(basket,"BGLP", 12); + addMult(basket,"BGLE", 6); + addMult(basket,"COFB", 3); + + Reciept reciept = new Reciept(basket); + System.out.println(reciept.getDiscountedReciept()); + Assertions.assertTrue(reciept.getDiscountedReciept().contains("Plain Bagel 12 £3.99\n" + + " (-£0.69)")); + Assertions.assertTrue(reciept.getDiscountedReciept().contains("Everything Bagel 6 £2.49\n" + + " (-£0.45)")); + Assertions.assertTrue(reciept.getDiscountedReciept().contains("Black Coffee 1 £0.99")); + Assertions.assertTrue(reciept.getDiscountedReciept().contains("Coffee Combo 2 £2.5")); + Assertions.assertTrue(reciept.getDiscountedReciept().contains("Total £9.97")); + } +} diff --git a/src/test/java/com/booleanuk/extension/StockTest.java b/src/test/java/com/booleanuk/extension/StockTest.java new file mode 100644 index 000000000..3d7e5a9f2 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/StockTest.java @@ -0,0 +1,51 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; + +public class StockTest { + + @Test + public void testGetPriceValidCode(){ + Stock stock = new Stock(); + + Assertions.assertEquals(0.49f, stock.getPrice("BGLO")); + Assertions.assertEquals(1.29f, stock.getPrice("COFL")); + } + + @Test + public void testGetPriceInvalidCode(){ + Stock stock = new Stock(); + + Assertions.assertEquals(-1, stock.getPrice("erqwer")); + Assertions.assertEquals(-1, stock.getPrice("\n")); + } + + @Test + public void testGetFillings(){ + Stock stock = new Stock(); + + List fillings = stock.getFillings(); + Assertions.assertEquals(6, fillings.size()); + + for(Item filling : fillings){ + Assertions.assertEquals("Filling", filling.getName()); + } + } + + @Test + public void testGetItemExists(){ + Stock stock = new Stock(); + + Assertions.assertEquals("BGLO", stock.getItem("BGLO").getSku()); + } + + @Test + public void testGetItemCodeDoesNotExist(){ + Stock stock = new Stock(); + + Assertions.assertNull(stock.getItem("test")); + } +} \ No newline at end of file