diff --git a/README.md b/README.md index e2aa7ee51..e4bbf3bf6 100644 --- a/README.md +++ b/README.md @@ -97,9 +97,9 @@ I want customers to only be able to order things that we stock in our inventory. | SKU | Price | Name | Variant | |------|-------|---------|---------------| -| BGLO | 0.49 | Bagel | Onion | | BGLP | 0.39 | Bagel | Plain | | BGLE | 0.49 | Bagel | Everything | +| BGLO | 0.49 | Bagel | Onion | | BGLS | 0.49 | Bagel | Sesame | | COFB | 0.99 | Coffee | Black | | COFW | 1.19 | Coffee | White | 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..240d8b049 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Bagel.java @@ -0,0 +1,26 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class Bagel extends Item{ + private ArrayList fillings = new ArrayList<>(); + private int maxFillings = 3; + + public Bagel(String id, double price, String description) { + super(id, price, description); + } + + // Allows up to only 3 fillings (let's not get silly here) to be attached to bagels + public boolean attachFilling(Filling filling) { + System.out.println(this.fillings.size()); + if (this.fillings.size() <= this.maxFillings - 1) { + this.fillings.add(filling); + return true; + } + return false; + } + + public ArrayList getAllFillings() { + return this.fillings; + } +} diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java new file mode 100644 index 000000000..f486ac75d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,52 @@ +package com.booleanuk.core; + +import java.util.ArrayList; + +public class Basket { + private final ArrayList basket = new ArrayList<>(); + private int maxCapacity = 5; + private double totalCost = 0.0; + + public boolean addItem(Item item) { + if (this.basket.size() <= this.maxCapacity - 1) { + this.basket.add(item); + this.totalCost += item.getPrice(); + return true; + } + System.out.println("Basket full!"); + return false; + + } + + public boolean removeItem(Item item) { + if (this.basket.contains(item)) { + this.basket.remove(item); + this.totalCost -= item.getPrice(); + return true; + } + System.out.println("No such item in the basket!"); + return false; + } + + public boolean changeCapacity(int newCapacity) { + if (newCapacity > 0) { + this.maxCapacity = newCapacity; + return true; + } + return false; + } + + public ArrayList getBasket(){ + return this.basket; + } + + public double getTotalCost() { + return this.totalCost; + } + + public int getMaxCapacity() { + return this.maxCapacity; + } + + +} diff --git a/src/main/java/com/booleanuk/core/ClassDiagramBobsBagels.png b/src/main/java/com/booleanuk/core/ClassDiagramBobsBagels.png new file mode 100644 index 000000000..634756e45 Binary files /dev/null and b/src/main/java/com/booleanuk/core/ClassDiagramBobsBagels.png differ 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..347c4fd44 --- /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 id, double price, String description) { + super(id, price, description); + } +} 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..cb6771b05 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Filling.java @@ -0,0 +1,7 @@ +package com.booleanuk.core; + +public class Filling extends Item{ + public Filling(String id, double price, String description) { + super(id, price, description); + } +} 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..b058cebdb --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item.java @@ -0,0 +1,25 @@ +package com.booleanuk.core; + +public class Item { + private final String id; + private final double price; + private final String description; + + public Item(String id, double price, String description) { + this.id = id; + this.price = price; + this.description = description; + } + + public String getId() { + return id; + } + + public double getPrice() { + return price; + } + + public String getDescription() { + return description; + } +} diff --git a/src/main/java/com/booleanuk/core/ItemFactory.java b/src/main/java/com/booleanuk/core/ItemFactory.java new file mode 100644 index 000000000..734382b78 --- /dev/null +++ b/src/main/java/com/booleanuk/core/ItemFactory.java @@ -0,0 +1,53 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; + +public class ItemFactory { + private String id; + private final HashMap> inventory = new HashMap<>() {{ + put("BGLP", new ArrayList<>(Arrays.asList(0.39, "Bagel", "Plain"))); + put("BGLE", new ArrayList<>(Arrays.asList(0.49, "Bagel", "Everything"))); + put("BGLO", new ArrayList<>(Arrays.asList(0.49, "Bagel", "Onion"))); + put("BGLS", new ArrayList<>(Arrays.asList(0.49, "Bagel", "Sesame"))); + put("COFB", new ArrayList<>(Arrays.asList(0.99, "Coffee", "Black"))); + put("COFW", new ArrayList<>(Arrays.asList(1.19, "Coffee", "White"))); + put("COFC", new ArrayList<>(Arrays.asList(1.29, "Coffee","Cappuccino"))); + put("COFL", new ArrayList<>(Arrays.asList(1.29, "Coffee", "Latte"))); + put("FILB", new ArrayList<>(Arrays.asList(0.12, "Filling", "Bacon"))); + put("FILE", new ArrayList<>(Arrays.asList(0.12, "Filling", "Egg"))); + put("FILC", new ArrayList<>(Arrays.asList(0.12, "Filling", "Cheese"))); + put("FILX", new ArrayList<>(Arrays.asList(0.12, "Filling", "Cream Cheese"))); + put("FILS", new ArrayList<>(Arrays.asList(0.12, "Filling", "Smoked Salmon"))); + put("FILH", new ArrayList<>(Arrays.asList(0.12, "Filling", "Ham"))); + }}; + + + //private internal setters + private double setPrice() { + return (double) inventory.get(this.id).getFirst(); + } + + private String setDescription() { + return (String) inventory.get(this.id).get(2); + } + + // create and return new instance of bagel, coffee or filling based on what the provided SKU (id) was + public Item createItem(String id) { + this.id = id; + if (this.inventory.containsKey(this.id)) { + if (this.id.startsWith("B")) { + return new Bagel(this.id, setPrice(), setDescription()); + + } else if (this.id.startsWith("C")) { + return new Coffee(this.id, setPrice(), setDescription()); + + } else if (this.id.startsWith("F")) { + return new Filling(this.id, setPrice(), setDescription()); + } + } + return null; + } + +} 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..a70d3eab1 --- /dev/null +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -0,0 +1,63 @@ +(Get/set methods don't have a scenario because they return null/do nothing if the input for the setter is invalid or the getter tries to get something it can't.) + +# Item class + +| Members | Methods | Output | +|--------------------|-------------------------|------------------------| +| String id | String getId() | SKU of the item | +| double price | double getPrice() | Price of the item | +| String description | String getDescription() | Description of the item | + +# Bagel class + +| Inheritance | Members | Methods | Scenario | Result/Output | +|---------------------|------------------------------|----------------------------------------|-------------------------------------|------------------------------------------------| +| Child of Item class | String id | String getId() | | SKU of the bagel | +| | double price | double getPrice() | | Price of the bagel | +| | String description | String getDescription() | | Description of the bagel | +| | ArrayList\ fillings | boolean attachFilling(Filling filling) | 3 or more fillings already attached | Return false, don't add the filling | +| | | | Less than 3 fillings attached | Return true, add filling to fillings ArrayList | +| | | ArrayList\ getAllFillings() | | All fillings attached to the bagel | + +# Filling class + +| Inheritance | Members | Methods | Output | +|---------------------|--------------------|-------------------------|-----------------------------| +| Child of Item class | String id | String getId() | SKU of the filling | +| | double price | double getPrice() | Price of the filling | +| | String description | String getDescription() | Description of the filling | + +# Coffee class + + +| Inheritance | Members | Methods | Output | +|---------------------|--------------------|-------------------------|---------------------------| +| Child of Item class | String id | String getId() | SKU of the coffee | +| | double price | double getPrice() | Price of the coffee | +| | String description | String getDescription() | Description of the coffee | + +# ItemFactory class + + +| Members | Methods | Scenario | Output/Result | +|-------------------------------------------------|----------------------------|----------------------|-----------------------------------------------------------------------| +| HashMap\, ArrayList\> inventory | void setPrice() | | Sets the price of the item internally based on the provided SKU | +| String id | void setDescription | | Sets the description of the item internally based on the provided SKU | +| | Item createItem(String id) | Invalid id | return null | +| | | id starts with 'B' | create and return Bagel object based on id | +| | | id starts with 'C' | create and return Coffee object based on id | +| | | id starts with 'F' | create and return Filling object based on id | + + +# Basket class + + +| Members | Methods | Scenario | Output/Result | +|-------------------------|-----------------------------------------|-------------------------------------|--------------------------------------------------------------------------| +| ArrayList\ basket | boolean addToBasket(Item item) | Basket at max capacity | Don't add, return false | +| int maxCapacity; | | Basket not at max capacity | Add item to basket, increase totalPrice by price of item, return true | +| double totalPrice; | boolean removeFromBasket(Item item) | Item does not exist in basket | Don't remove, return false | +| | | Item in basket | Remove the item, decrease total totalPrice by price of item, return true | +| | boolean changeCapacity(int newCapacity) | newCapacity is less than 1 | Don't change capacity, return false | +| | | newCapacity is larger or equal to 1 | Update to new capacity, return true | +| | ArrayList\ getBasket() | | Returns contents of basket | 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..3000b7ef0 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Bagel.java @@ -0,0 +1,26 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; + +public class Bagel extends Item { + private ArrayList fillings = new ArrayList<>(); + private int maxFillings = 3; + + public Bagel(String id, double price, String description) { + super(id, price, description); + } + + // Allows up to only 3 fillings (let's not get silly here) to be attached to bagels + public boolean attachFilling(Filling filling) { + System.out.println(this.fillings.size()); + if (this.fillings.size() <= this.maxFillings - 1) { + this.fillings.add(filling); + return true; + } + return false; + } + + public ArrayList getAllFillings() { + return this.fillings; + } +} 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..5c9fef651 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Basket.java @@ -0,0 +1,111 @@ +package com.booleanuk.extension; + +import jdk.jfr.Label; + +import java.security.cert.Extension; +import java.sql.SQLOutput; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; + +public class Basket { + private final ArrayList basket = new ArrayList<>(); + private int maxCapacity = 25; + private double totalCost = 0.0; + + public boolean addItem(Item item) { + if (this.basket.size() <= this.maxCapacity - 1) { + this.basket.add(item); + this.totalCost += item.getPrice(); + return true; + } + System.out.println("Basket full!"); + return false; + + } + + public boolean removeItem(Item item) { + if (this.basket.contains(item)) { + this.basket.remove(item); + this.totalCost -= item.getPrice(); + return true; + } + System.out.println("No such item in the basket!"); + return false; + } + + public boolean changeCapacity(int newCapacity) { + if (newCapacity > 0) { + this.maxCapacity = newCapacity; + return true; + } + return false; + } + + public ArrayList getBasket(){ + return this.basket; + } + + public double getTotalCost() { + return this.totalCost; + } + + public int getMaxCapacity() { + return this.maxCapacity; + } + + + //This methods will allow multiple discounts to be applied and likely got this ugly because of it + //I.e. if there are 18 bagels of the same type, first the 12 bagel discount is given and then the 6 bagel discount for the remaining bagles + //The coffee discount needs "stand-alone" bagels, so not bagles that are already part of another discount calculation + public void checkDiscounts() { + HashMap> counterMap = new HashMap<>(); + double counter = 0.0; + + for (Item item : this.basket) { + counterMap.computeIfAbsent(item.getId(), k -> new ArrayList<>()).add(item); + } + + for (String id : counterMap.keySet()) { + int remainder12; + int finalRemainder; + if (id.startsWith("BGL")) { + //Check for 12 bagel discount + int quotient12 = counterMap.get(id).size() / 12; + counter += 3.99 * quotient12; + if (quotient12 != 0) { + remainder12 = counterMap.get(id).size() % (12 * quotient12); + } else { + remainder12 = counterMap.get(id).size(); + } + + //Check for 6 bagel discount + int quotient6 = remainder12 / 6; + counter += 2.49 * quotient6; + if (quotient6 != 0) { + finalRemainder = remainder12 % (6 * quotient6); + } else { + finalRemainder = remainder12; + } + + //Check for bagel and coffee discounts + if (counterMap.containsKey("COFB")) { + while (!counterMap.get("COFB").isEmpty() && finalRemainder > 0) { + counter += 1.25; + counterMap.get("COFB").removeFirst(); + finalRemainder--; + } + + } + //Good chance that the coffee check has removed all elements in the list so need to check if null + //before the remaining (non-discounted) items are calculated + if (counterMap.get(id).getFirst() != null) { + counter += finalRemainder * counterMap.get(id).getFirst().getPrice(); + } + } + } + this.totalCost = counter; + } + + +} 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..44e8269c4 --- /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 id, double price, String description) { + super(id, price, description); + } +} 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..c4c48adcb --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Filling.java @@ -0,0 +1,7 @@ +package com.booleanuk.extension; + +public class Filling extends Item { + public Filling(String id, double price, String description) { + super(id, price, description); + } +} 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..b0fe83152 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Item.java @@ -0,0 +1,25 @@ +package com.booleanuk.extension; + +public class Item { + private final String id; + private final double price; + private final String description; + + public Item(String id, double price, String description) { + this.id = id; + this.price = price; + this.description = description; + } + + public String getId() { + return id; + } + + public double getPrice() { + return price; + } + + public String getDescription() { + return description; + } +} diff --git a/src/main/java/com/booleanuk/extension/ItemFactory.java b/src/main/java/com/booleanuk/extension/ItemFactory.java new file mode 100644 index 000000000..a011d8b63 --- /dev/null +++ b/src/main/java/com/booleanuk/extension/ItemFactory.java @@ -0,0 +1,53 @@ +package com.booleanuk.extension; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; + +public class ItemFactory { + private String id; + private final HashMap> inventory = new HashMap<>() {{ + put("BGLP", new ArrayList<>(Arrays.asList(0.39, "Bagel", "Plain"))); + put("BGLE", new ArrayList<>(Arrays.asList(0.49, "Bagel", "Everything"))); + put("BGLO", new ArrayList<>(Arrays.asList(0.49, "Bagel", "Onion"))); + put("BGLS", new ArrayList<>(Arrays.asList(0.49, "Bagel", "Sesame"))); + put("COFB", new ArrayList<>(Arrays.asList(0.99, "Coffee", "Black"))); + put("COFW", new ArrayList<>(Arrays.asList(1.19, "Coffee", "White"))); + put("COFC", new ArrayList<>(Arrays.asList(1.29, "Coffee","Cappuccino"))); + put("COFL", new ArrayList<>(Arrays.asList(1.29, "Coffee", "Latte"))); + put("FILB", new ArrayList<>(Arrays.asList(0.12, "Filling", "Bacon"))); + put("FILE", new ArrayList<>(Arrays.asList(0.12, "Filling", "Egg"))); + put("FILC", new ArrayList<>(Arrays.asList(0.12, "Filling", "Cheese"))); + put("FILX", new ArrayList<>(Arrays.asList(0.12, "Filling", "Cream Cheese"))); + put("FILS", new ArrayList<>(Arrays.asList(0.12, "Filling", "Smoked Salmon"))); + put("FILH", new ArrayList<>(Arrays.asList(0.12, "Filling", "Ham"))); + }}; + + + //private internal setters + private double setPrice() { + return (double) inventory.get(this.id).getFirst(); + } + + private String setDescription() { + return (String) inventory.get(this.id).get(2); + } + + // create and return new instance of bagel, coffee or filling based on what the provided SKU (id) was + public Item createItem(String id) { + this.id = id; + if (this.inventory.containsKey(this.id)) { + if (this.id.startsWith("B")) { + return new Bagel(this.id, setPrice(), setDescription()); + + } else if (this.id.startsWith("C")) { + return new Coffee(this.id, setPrice(), setDescription()); + + } else if (this.id.startsWith("F")) { + return new Filling(this.id, setPrice(), setDescription()); + } + } + return null; + } + +} 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..c496a4e6e --- /dev/null +++ b/src/main/java/com/booleanuk/extension/Receipt.java @@ -0,0 +1,107 @@ +package com.booleanuk.extension; + +import java.lang.reflect.Array; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; + + +public class Receipt { + private ArrayList itemsInBasket; + private HashMap> itemCount = new HashMap<>(); + private ArrayList itemTypes = new ArrayList<>(); + + public ArrayList getItemsInBasket() { + return itemsInBasket; + } + + public Receipt(Basket basket) { + this.itemsInBasket = basket.getBasket(); + + } + + public void checkItemTypes() { + for (Item item : itemsInBasket) { + if (!itemTypes.contains(item.getId())) { + itemTypes.add(item.getId()); + } + } + } + + public void dataToMap() { + for (Item item : this.itemsInBasket) { + itemCount.computeIfAbsent(item.getId(), k -> new ArrayList<>()).add(item); + } + System.out.println(itemCount); + } + + + public String constructDate() { + StringBuilder stringBuilder = new StringBuilder(); + String date = """ + + ~~~ Bob's Bagels ~~~ + + %s + + --------------------------- + """; + String dateNow = new SimpleDateFormat("dd-MM-yyyy").format(Calendar.getInstance().getTime()); + date = String.format(date, dateNow); + return date; + + } + + public String constructBody() { + checkItemTypes(); + String body = """ + + %s + --------------------------- + """; + StringBuilder stringbuilder = new StringBuilder(); + + for (String id : itemTypes) { + stringbuilder.append( + itemCount.get(id).getFirst().getDescription() + " " + + itemCount.get(id).getFirst().getClass().toString().replace("class com.booleanuk.extension.", "") + "\t\t" + + itemCount.get(id).size() + "\t\t" + + Math.floor(itemCount.get(id).getFirst().getPrice() * (double) itemCount.get(id).size() * 100) /100 + "\n" + ); + } + return String.format(body, stringbuilder); + } + + public String constructTotal() { + double totalPrice = 0.0; + for (Item item : this.itemsInBasket) { + totalPrice += item.getPrice(); + } + totalPrice = Math.floor(totalPrice * 100)/100; + + String total = """ + Total %s + + Thank you for your order! + """; + total = String.format(total, totalPrice); + return total; + } + + public String conStructReceipt(String header, String body, String total) { + StringBuilder stringbuilder = new StringBuilder(); + stringbuilder.append(header); + stringbuilder.append(body); + stringbuilder.append(total); + return stringbuilder.toString(); + } + + + + + + + +} 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..dedcdd88c --- /dev/null +++ b/src/main/java/com/booleanuk/extension/domain-model.md @@ -0,0 +1,68 @@ +(Get/set methods don't have a scenario because they return null/do nothing if the input for the setter is invalid or the getter tries to get something it can't.) + +# Item class + +| Members | Methods | Output | +|--------------------|-------------------------|------------------------| +| String id | String getId() | SKU of the item | +| double price | double getPrice() | Price of the item | +| String description | String getDescription() | Description of the item | + +# Bagel class + +| Inheritance | Members | Methods | Scenario | Result/Output | +|---------------------|------------------------------|----------------------------------------|-------------------------------------|------------------------------------------------| +| Child of Item class | String id | String getId() | | SKU of the bagel | +| | double price | double getPrice() | | Price of the bagel | +| | String description | String getDescription() | | Description of the bagel | +| | ArrayList\ fillings | boolean attachFilling(Filling filling) | 3 or more fillings already attached | Return false, don't add the filling | +| | | | Less than 3 fillings attached | Return true, add filling to fillings ArrayList | +| | | ArrayList\ getAllFillings() | | All fillings attached to the bagel | + +# Filling class + +| Inheritance | Members | Methods | Output | +|---------------------|--------------------|-------------------------|-----------------------------| +| Child of Item class | String id | String getId() | SKU of the filling | +| | double price | double getPrice() | Price of the filling | +| | String description | String getDescription() | Description of the filling | + +# Coffee class + + +| Inheritance | Members | Methods | Output | +|---------------------|--------------------|-------------------------|---------------------------| +| Child of Item class | String id | String getId() | SKU of the coffee | +| | double price | double getPrice() | Price of the coffee | +| | String description | String getDescription() | Description of the coffee | + +# ItemFactory class + + +| Members | Methods | Scenario | Output/Result | +|-------------------------------------------------|----------------------------|----------------------|-----------------------------------------------------------------------| +| HashMap\, ArrayList\> inventory | void setPrice() | | Sets the price of the item internally based on the provided SKU | +| String id | void setDescription | | Sets the description of the item internally based on the provided SKU | +| | Item createItem(String id) | Invalid id | return null | +| | | id starts with 'B' | create and return Bagel object based on id | +| | | id starts with 'C' | create and return Coffee object based on id | +| | | id starts with 'F' | create and return Filling object based on id | + + +# Basket class + + +| Members | Methods | Scenario | Output/Result | +|-------------------------|-----------------------------------------|---------------------------------------|--------------------------------------------------------------------------| +| ArrayList\ basket | boolean addToBasket(Item item) | Basket at max capacity | Don't add, return false | +| int maxCapacity; | | Basket not at max capacity | Add item to basket, increase totalPrice by price of item, return true | +| double totalPrice; | boolean removeFromBasket(Item item) | Item does not exist in basket | Don't remove, return false | +| | | Item in basket | Remove the item, decrease total totalPrice by price of item, return true | +| | boolean changeCapacity(int newCapacity) | newCapacity is less than 1 | Don't change capacity, return false | +| | | newCapacity is larger or equal to 1 | Update to new capacity, return true | +| | ArrayList\ getBasket() | | Returns contents of basket | +| | double checkDiscounts() | No discount items in basket | No discount given | +| | | 6 or more onion bagels in basket | Apply discount per 6 onion bagels (total: 2.49) | +| | | 12 or more plain bagels in basket | Apply discount per 12 plain bagels (total: 3.99) | +| | | 6 or more everything bagels in basket | Apply discount per 6 everything bagels (total: 2.49) | +| | | 1 black coffee + any bagel | Apply discount per set of coffee and bagel (total: 1.25) | 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..552cf1ece --- /dev/null +++ b/src/test/java/com/booleanuk/core/BagelTest.java @@ -0,0 +1,27 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BagelTest { + + @Test + public void testBagelCreation() { + Bagel bagel = new Bagel("BGLP", 0.39, "Plain"); + Assertions.assertEquals("BGLP",bagel.getId()); + Assertions.assertEquals(0.39, bagel.getPrice()); + Assertions.assertEquals("Plain", bagel.getDescription()); + Assertions.assertInstanceOf(Bagel.class, bagel); + } + + @Test + public void testFillingAttachment() { + Bagel bagel = new Bagel("BGLP", 0.39, "Plain"); + Filling filling1 = new Filling("FILB", 0.12, "Bacon"); + bagel.attachFilling(filling1); //add first filling + Assertions.assertEquals(filling1, bagel.getAllFillings().getFirst()); + bagel.attachFilling(filling1); // add second filling + bagel.attachFilling(filling1); // add third filling + Assertions.assertFalse(bagel.attachFilling(filling1)); //adding the fourth filling should fail + } +} 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..1ec649104 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,79 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + + @Test + public void testAddItem() { + //Don't need to test for invalid items because they cannot be created according to the item factory tests + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + boolean successfullyAdded = basket.addItem(item); // add item + Assertions.assertTrue(successfullyAdded); + Assertions.assertEquals(item, basket.getBasket().getFirst()); //Check that the added item is in the basket + Assertions.assertEquals(item.getPrice(), basket.getTotalCost()); //Check if the total increased by the item price + } + + @Test + public void testAddingTooManyItems() { + //Max capacity is 5, so adding should fail after 5 elements are present in the basket list + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + basket.addItem(item); + basket.addItem(item); + basket.addItem(item); + basket.addItem(item); + boolean successfullyAdded5 = basket.addItem(item); + boolean successfullyAdded6 = basket.addItem(item); + Assertions.assertTrue(successfullyAdded5); + Assertions.assertFalse(successfullyAdded6); + //check if 6 elements were added despite the max capacity being 5 + Assertions.assertThrows(IndexOutOfBoundsException.class , () -> basket.getBasket().get(5)); + } + + @Test + public void testRemoveItem() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + basket.addItem(item); + boolean successfullyRemoved = basket.removeItem(item); + Assertions.assertTrue(successfullyRemoved); + Assertions.assertFalse(basket.getBasket().contains(item)); //check if the item is still in the basket + Assertions.assertEquals(0, basket.getTotalCost()); //Check if the price went down back to 0 after removing + } + + @Test + public void testRemoveNonExistingItem() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item1 = factory.createItem("BGLO"); + basket.addItem(item1); + Item item2 = factory.createItem("BGLP"); //Item2 never gets added to basket + boolean successfullyRemoved = basket.removeItem(item2); + Assertions.assertFalse(successfullyRemoved); + Assertions.assertEquals(1, basket.getBasket().size()); //Check if the basket still holds the items it should + + } + + @Test + public void testChangeCapacity() { + Basket basket = new Basket(); //basket has capacity of 5 by default + basket.changeCapacity(7); + Assertions.assertEquals(7, basket.getMaxCapacity()); //Check that the capacity was changed + } + + @Test + public void testInvalidCapacity() { + Basket basket = new Basket(); + boolean successfulChange = basket.changeCapacity(-1); + Assertions.assertFalse(successfulChange); + Assertions.assertEquals(5, basket.getMaxCapacity()); // -1 is invalid so the capacity should not change + } + +} diff --git a/src/test/java/com/booleanuk/core/CoffeeTest.java b/src/test/java/com/booleanuk/core/CoffeeTest.java new file mode 100644 index 000000000..855e667b5 --- /dev/null +++ b/src/test/java/com/booleanuk/core/CoffeeTest.java @@ -0,0 +1,16 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class CoffeeTest { + + @Test + public void testCoffeeCreation() { + Coffee coffee = new Coffee("COFB", 0.99, "Black"); + Assertions.assertEquals("COFB",coffee.getId()); + Assertions.assertEquals(0.99, coffee.getPrice()); + Assertions.assertEquals("Black", coffee.getDescription()); + Assertions.assertInstanceOf(Coffee.class, coffee); + } +} diff --git a/src/test/java/com/booleanuk/core/FillingTest.java b/src/test/java/com/booleanuk/core/FillingTest.java new file mode 100644 index 000000000..d227e6c7b --- /dev/null +++ b/src/test/java/com/booleanuk/core/FillingTest.java @@ -0,0 +1,16 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class FillingTest { + + @Test + public void testFillingCreation() { + Filling filling = new Filling("FILB", 0.12, "Bacon"); + Assertions.assertEquals("FILB",filling.getId()); + Assertions.assertEquals(0.12, filling.getPrice()); + Assertions.assertEquals("Bacon", filling.getDescription()); + Assertions.assertInstanceOf(Filling.class, filling); + } +} diff --git a/src/test/java/com/booleanuk/core/ItemFactoryTest.java b/src/test/java/com/booleanuk/core/ItemFactoryTest.java new file mode 100644 index 000000000..faab044ae --- /dev/null +++ b/src/test/java/com/booleanuk/core/ItemFactoryTest.java @@ -0,0 +1,36 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemFactoryTest { + + @Test + public void testBagelCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + Assertions.assertInstanceOf(Bagel.class, item); //Item should be a bagel because "BGLP" is a bagel SKU + + } + @Test + public void testFillingCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("FILB"); + Assertions.assertInstanceOf(Filling.class, item); //Item should be a filling because "FILB" is a filling SKU + } + + @Test + public void testCoffeeCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("COFB"); + Assertions.assertInstanceOf(Coffee.class, item); //Item should be a coffee because "COFB" is a coffee SKU + } + + @Test + public void testNullCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("XXXX"); + Assertions.assertNull(item); //Item should be a null because "XXXX" is not a valid SKU + } + +} 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..bb28ed640 --- /dev/null +++ b/src/test/java/com/booleanuk/core/ItemTest.java @@ -0,0 +1,17 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemTest { + + @Test + public void testItemCreation() { + Item item = new Item("BGLP", 0.39, "Plain"); + Assertions.assertEquals("BGLP",item.getId()); + Assertions.assertEquals(0.39, item.getPrice()); + Assertions.assertEquals("Plain", item.getDescription()); + Assertions.assertInstanceOf(Item.class, item); + } + +} 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..24cfd3065 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/BagelTest.java @@ -0,0 +1,27 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BagelTest { + + @Test + public void testBagelCreation() { + Bagel bagel = new Bagel("BGLP", 0.39, "Plain"); + Assertions.assertEquals("BGLP",bagel.getId()); + Assertions.assertEquals(0.39, bagel.getPrice()); + Assertions.assertEquals("Plain", bagel.getDescription()); + Assertions.assertInstanceOf(Bagel.class, bagel); + } + + @Test + public void testFillingAttachment() { + Bagel bagel = new Bagel("BGLP", 0.39, "Plain"); + Filling filling1 = new Filling("FILB", 0.12, "Bacon"); + bagel.attachFilling(filling1); //add first filling + Assertions.assertEquals(filling1, bagel.getAllFillings().getFirst()); + bagel.attachFilling(filling1); // add second filling + bagel.attachFilling(filling1); // add third filling + Assertions.assertFalse(bagel.attachFilling(filling1)); //adding the fourth filling should fail + } +} 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..2bd221db4 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/BasketTest.java @@ -0,0 +1,270 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + + @Test + public void testAddItem() { + //Don't need to test for invalid items because they cannot be created according to the item factory tests + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + boolean successfullyAdded = basket.addItem(item); // add item + Assertions.assertTrue(successfullyAdded); + Assertions.assertEquals(item, basket.getBasket().getFirst()); //Check that the added item is in the basket + Assertions.assertEquals(item.getPrice(), basket.getTotalCost()); //Check if the total increased by the item price + } + +/* I would need to add 26 items for this test to pass. I increased the max capacity to 25 to be able to test the discount problem properly + @Test + public void testAddingTooManyItems() { + //Max capacity is 5, so adding should fail after 5 elements are present in the basket list + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + basket.addItem(item); + basket.addItem(item); + basket.addItem(item); + basket.addItem(item); + boolean successfullyAdded5 = basket.addItem(item); + boolean successfullyAdded6 = basket.addItem(item); + Assertions.assertTrue(successfullyAdded5); + Assertions.assertFalse(successfullyAdded6); + //check if 6 elements were added despite the max capacity being 5 + Assertions.assertThrows(IndexOutOfBoundsException.class , () -> basket.getBasket().get(5)); + } +*/ + @Test + public void testRemoveItem() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + basket.addItem(item); + boolean successfullyRemoved = basket.removeItem(item); + Assertions.assertTrue(successfullyRemoved); + Assertions.assertFalse(basket.getBasket().contains(item)); //check if the item is still in the basket + Assertions.assertEquals(0, basket.getTotalCost()); //Check if the price went down back to 0 after removing + } + + @Test + public void testRemoveNonExistingItem() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item item1 = factory.createItem("BGLO"); + basket.addItem(item1); + Item item2 = factory.createItem("BGLP"); //Item2 never gets added to basket + boolean successfullyRemoved = basket.removeItem(item2); + Assertions.assertFalse(successfullyRemoved); + Assertions.assertEquals(1, basket.getBasket().size()); //Check if the basket still holds the items it should + + } + + @Test + public void testChangeCapacity() { + Basket basket = new Basket(); //basket has capacity of 25 by default + basket.changeCapacity(7); + Assertions.assertEquals(7, basket.getMaxCapacity()); //Check that the capacity was changed + } + + @Test + public void testInvalidCapacity() { + Basket basket = new Basket(); + boolean successfulChange = basket.changeCapacity(-1); + Assertions.assertFalse(successfulChange); + Assertions.assertEquals(25, basket.getMaxCapacity()); // -1 is invalid so the capacity should not change + } + + //Discount testing starts here: + //Test the behaviour if only the discount items are in the basket + + @Test + public void testOnlySixOnionBagelsDiscount() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item onionBagel = factory.createItem("BGLO"); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); //6th bagel added + basket.checkDiscounts(); + Assertions.assertEquals(2.49, basket.getTotalCost()); + + } + + @Test + public void testOnlyTwelvePlainBagelsDiscount() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item plainBagel = factory.createItem("BGLP"); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); //12th bagel added + basket.checkDiscounts(); + Assertions.assertEquals(3.99, basket.getTotalCost()); + } + + @Test + public void testOnlyBlackCoffeeAndBagelDiscount() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item everythingBagel = factory.createItem("BGLE"); + Item blackCoffee = factory.createItem("COFB"); + basket.addItem(everythingBagel); + basket.addItem(blackCoffee); + basket.checkDiscounts(); + Assertions.assertEquals(1.25, basket.getTotalCost()); + } + + //test discount items with other items from here + + @Test + public void testSixOnionBagelsDiscountWithOtherItems() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item onionBagel = factory.createItem("BGLO"); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); //6th bagel added + basket.addItem(onionBagel); + basket.checkDiscounts(); + Assertions.assertEquals(2.49 + 0.49, basket.getTotalCost()); + + } + + @Test + public void testTwelvePlainBagelsDiscountWithOtherItems() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item plainBagel = factory.createItem("BGLP"); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); //12th bagel added + basket.addItem(plainBagel); + basket.checkDiscounts(); + Assertions.assertEquals(3.99 + 0.39, basket.getTotalCost()); + } + + @Test + public void testBlackCoffeeAndBagelDiscountWithOtherItems() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item everythingBagel = factory.createItem("BGLE"); + Item blackCoffee = factory.createItem("COFB"); + basket.addItem(everythingBagel); + basket.addItem(blackCoffee); //discount applies from here + basket.addItem(everythingBagel); + basket.checkDiscounts(); + Assertions.assertEquals(1.25 + 0.49, basket.getTotalCost()); + } + + //Testing of multiple instances of the same discount being applied (so 24 onion bagels = 2 * the discount price for 12) + + + @Test + public void testTwoPlainBagelsDiscounts() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item plainBagel = factory.createItem("BGLP"); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); //12th bagel added + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); //24th bagel added + basket.checkDiscounts(); + Assertions.assertEquals(3.99 * 2, basket.getTotalCost()); + } + + @Test + public void testTwoBlackCoffeeAndBagelDiscounts() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item everythingBagel = factory.createItem("BGLE"); + Item blackCoffee = factory.createItem("COFB"); + basket.addItem(everythingBagel); + basket.addItem(blackCoffee); //discount applies from here + basket.addItem(everythingBagel); + basket.addItem(blackCoffee); //discount applies from here again + basket.checkDiscounts(); + Assertions.assertEquals(1.25 * 2, basket.getTotalCost()); + } + + @Test + public void multipleDifferentDiscounts() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item onionBagel = factory.createItem("BGLO"); + Item everythingBagel = factory.createItem("BGLE"); + Item blackCoffee = factory.createItem("COFB"); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); //6th bagel added + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); //12th bagel added, should be 3.99 in total at this point + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); //18th bagel added, should be 3.99 + 2.49 at this point + //basket.addItem(blackCoffee); + //basket.addItem(everythingBagel); // coffee discount applies from here + basket.checkDiscounts(); + Assertions.assertEquals(3.99 + 2.49, basket.getTotalCost()); + } + + + +} diff --git a/src/test/java/com/booleanuk/extension/CoffeeTest.java b/src/test/java/com/booleanuk/extension/CoffeeTest.java new file mode 100644 index 000000000..9e7df7690 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/CoffeeTest.java @@ -0,0 +1,17 @@ +package com.booleanuk.extension; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class CoffeeTest { + + @Test + public void testCoffeeCreation() { + Coffee coffee = new Coffee("COFB", 0.99, "Black"); + Assertions.assertEquals("COFB",coffee.getId()); + Assertions.assertEquals(0.99, coffee.getPrice()); + Assertions.assertEquals("Black", coffee.getDescription()); + Assertions.assertInstanceOf(Coffee.class, coffee); + } +} diff --git a/src/test/java/com/booleanuk/extension/FillingTest.java b/src/test/java/com/booleanuk/extension/FillingTest.java new file mode 100644 index 000000000..2bc6fefd0 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/FillingTest.java @@ -0,0 +1,16 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class FillingTest { + + @Test + public void testFillingCreation() { + Filling filling = new Filling("FILB", 0.12, "Bacon"); + Assertions.assertEquals("FILB",filling.getId()); + Assertions.assertEquals(0.12, filling.getPrice()); + Assertions.assertEquals("Bacon", filling.getDescription()); + Assertions.assertInstanceOf(Filling.class, filling); + } +} diff --git a/src/test/java/com/booleanuk/extension/ItemFactoryTest.java b/src/test/java/com/booleanuk/extension/ItemFactoryTest.java new file mode 100644 index 000000000..cb5d1608b --- /dev/null +++ b/src/test/java/com/booleanuk/extension/ItemFactoryTest.java @@ -0,0 +1,36 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemFactoryTest { + + @Test + public void testBagelCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("BGLP"); + Assertions.assertInstanceOf(Bagel.class, item); //Item should be a bagel because "BGLP" is a bagel SKU + + } + @Test + public void testFillingCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("FILB"); + Assertions.assertInstanceOf(Filling.class, item); //Item should be a filling because "FILB" is a filling SKU + } + + @Test + public void testCoffeeCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("COFB"); + Assertions.assertInstanceOf(Coffee.class, item); //Item should be a coffee because "COFB" is a coffee SKU + } + + @Test + public void testNullCreation() { + ItemFactory factory = new ItemFactory(); + Item item = factory.createItem("XXXX"); + Assertions.assertNull(item); //Item should be a null because "XXXX" is not a valid SKU + } + +} 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..46bd9d12d --- /dev/null +++ b/src/test/java/com/booleanuk/extension/ItemTest.java @@ -0,0 +1,18 @@ +package com.booleanuk.extension; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemTest { + + @Test + public void testItemCreation() { + Item item = new Item("BGLP", 0.39, "Plain"); + Assertions.assertEquals("BGLP",item.getId()); + Assertions.assertEquals(0.39, item.getPrice()); + Assertions.assertEquals("Plain", item.getDescription()); + Assertions.assertInstanceOf(Item.class, item); + } + +} diff --git a/src/test/java/com/booleanuk/extension/ReceiptTest.java b/src/test/java/com/booleanuk/extension/ReceiptTest.java new file mode 100644 index 000000000..ab7b7c4e7 --- /dev/null +++ b/src/test/java/com/booleanuk/extension/ReceiptTest.java @@ -0,0 +1,143 @@ +package com.booleanuk.extension; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.text.SimpleDateFormat; +import java.util.Calendar; + +public class ReceiptTest { + + @Test + public void testDateHeader() { + Basket basket = new Basket(); + Receipt receipt = new Receipt(basket); + String date = """ + + ~~~ Bob's Bagels ~~~ + + %s + + --------------------------- + """; + String dateNow = new SimpleDateFormat("dd-MM-yyyy").format(Calendar.getInstance().getTime()); + date = String.format(date, dateNow); + Assertions.assertEquals(date, receipt.constructDate()); + // Sort of doing the same thing as in the method tested, but date changed every day so hard to write a super different test + } + + @Test + public void testBodyConstruction() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item plainBagel = factory.createItem("BGLP"); + Item onionBagel = factory.createItem("BGLO"); + Item blackCoffee = factory.createItem("COFB"); + basket.addItem(plainBagel); + basket.addItem(onionBagel); + basket.addItem(blackCoffee); + Receipt receipt = new Receipt(basket); + receipt.dataToMap(); + StringBuilder testString1 = new StringBuilder(); + StringBuilder testString2 = new StringBuilder(); + + String body = """ + + Plain Bagel\t\t1\t\t0.39 + Onion Bagel\t\t1\t\t0.49 + Black Coffee\t\t1\t\t0.99 + + --------------------------- + """; + testString1.append(body); + testString2.append(receipt.constructBody()); + System.out.println(testString1); + System.out.println(testString2); + Assertions.assertEquals(0, testString1.compareTo(testString2)); + } + + @Test + public void testTotalConstruction() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item plainBagel = factory.createItem("BGLP"); + Item onionBagel = factory.createItem("BGLO"); + Item blackCoffee = factory.createItem("COFB"); + basket.addItem(plainBagel); + basket.addItem(onionBagel); + basket.addItem(blackCoffee); + Receipt receipt = new Receipt(basket); + receipt.dataToMap(); + StringBuilder testString1 = new StringBuilder(); + StringBuilder testString2 = new StringBuilder(); + + String testTotal = """ + Total 1.87 + + Thank you for your order! + """; + testString1.append(testTotal); + testString2.append(receipt.constructTotal()); + Assertions.assertEquals(0,testString1.compareTo(testString2)); + + } + + + @Test + public void testCompleteReceipt() { + Basket basket = new Basket(); + ItemFactory factory = new ItemFactory(); + Item plainBagel = factory.createItem("BGLP"); + Item onionBagel = factory.createItem("BGLO"); + Item blackCoffee = factory.createItem("COFB"); + Item cheeseFilling = factory.createItem("FILC"); + Item eggFilling = factory.createItem("FILE"); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); + basket.addItem(plainBagel); // 5 plain + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); + basket.addItem(onionBagel); // 5 onion + basket.addItem(blackCoffee); + basket.addItem(cheeseFilling); + basket.addItem(eggFilling); + Receipt receipt= new Receipt(basket); + receipt.dataToMap(); + + String testReceipt = """ + + ~~~ Bob's Bagels ~~~ + + 14-01-2025 + + --------------------------- + + Plain Bagel\t\t5\t\t1.95 + Onion Bagel\t\t5\t\t2.45 + Black Coffee\t\t1\t\t0.99 + Cheese Filling\t\t1\t\t0.12 + Egg Filling\t\t1\t\t0.12 + + --------------------------- + Total 5.63 + + Thank you for your order! + """; + + StringBuilder stringBuilder1 = new StringBuilder(); + StringBuilder stringBuilder2 = new StringBuilder(); + stringBuilder1.append(testReceipt); + stringBuilder2.append(receipt.conStructReceipt(receipt.constructDate(), receipt.constructBody(), receipt.constructTotal())); + System.out.println(testReceipt); + System.out.println(receipt.conStructReceipt(receipt.constructDate(), receipt.constructBody(), receipt.constructTotal())); + Assertions.assertEquals(0, stringBuilder1.compareTo(stringBuilder2)); + + + } + + +}