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..e580c5d25 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,104 @@ +package com.booleanuk.core; +import com.booleanuk.core.Item.Item; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class Basket { + private final int capacity; + private final List items; + private final Inventory inventory; + + public Basket(int capacity, Inventory inventory){ + this.capacity = capacity; + this.items = new ArrayList<>(); + this.inventory = inventory; + } + + public boolean add(Item item){ + if (!inventory.exists(item)){ + System.out.println("The item is out of stock!"); + return false; + } else if (isFull()){ + System.out.println("The basket is full!"); + return false; + } else { + items.add(item); + inventory.reduceCount(item); + return true; + } + } + + public boolean remove(Item item){ + if (items.contains(item)){ + items.remove(item); + inventory.increaseCount(item); + return true; + } else { + System.out.println("This item does not exist in your basket!"); + return false; + } + } + + public boolean isFull() { + return items.size() >= capacity; + } + + public double total(){ + return items.stream().map(Item::getPrice).reduce(0.0, Double::sum) - findDiscount(); + } + + public double findDiscount(){ + double totalDiscount = 0.0; + + int bagelsPlainCount = Math.toIntExact(items.stream().filter(item -> item.getSku().equals("BGLP")).count()); + int bagelsNotPlainCount = Math.toIntExact(items.stream().filter(item -> !item.getSku().equals("BGLP")).count()); + int coffeeBlackCoffeeCount = Math.toIntExact(items.stream().filter(item -> item.getSku().equals("COFB")).count()); + if (bagelsPlainCount >= 12) { + // adding the difference between normal price of 6 plain bagels vs discounted price + totalDiscount += 0.69; + } + if (bagelsNotPlainCount >= 6){ + totalDiscount += 0.45; + } + if (bagelsPlainCount > 0 && coffeeBlackCoffeeCount > 0){ + totalDiscount += 0.13; + } else if (bagelsNotPlainCount > 0 && coffeeBlackCoffeeCount > 0){ + totalDiscount += 0.23; + } + return totalDiscount; + } + + public void printReceipt(){ + // A little bit messy, but I didn't want to rewrite the other parts of the code + // to be able to simplify this one, but it works. + // There is definitely a better way to structure the print-statements, + // but I figured that weren't the main objective of the task anyway. + + HashMap res = new HashMap<>(); + for (Item item : items){ + res.put(item.getType(), res.getOrDefault(item.getType(), 0) + 1); + } + LocalDateTime currentDateTime = LocalDateTime.now(); + + String firstPart = + "\n\n ~~~ Bob's Bagels ~~~\n\n " + currentDateTime + "\n\n" + + "-------------------------------------" + "\n"; + + System.out.println(firstPart); + res.forEach((key,val) -> { + double price = items.stream().filter(itm -> itm.getType().equals(key)).toList().getFirst().getPrice()*val; + System.out.println(key + " " + " " + val + " £" + price); + }); + String lastPart = + "-------------------------------------" + "\n" + + "Sum £" + String.format("%.2f", total()+findDiscount()) + "\n" + + "Discount (-£" + String.format("%.2f", findDiscount()) + ")\n" + + "Total £" + String.format("%.2f", total()) + + "\n\nYou saved a total of: £" + String.format("%.2f", findDiscount()) + + "\n\n Thank you \n for your order!\n\n"; + + System.out.println(lastPart); + } +} diff --git a/src/main/java/com/booleanuk/core/BasketCapacity.java b/src/main/java/com/booleanuk/core/BasketCapacity.java new file mode 100644 index 000000000..3b1fe1653 --- /dev/null +++ b/src/main/java/com/booleanuk/core/BasketCapacity.java @@ -0,0 +1,27 @@ +package com.booleanuk.core; + +// Basically the class I used as a "global" variable to be used as the capacity +// values of baskets once they're created. The logic is that every basket that is already +// in use by the time this value changes, will remain at the previous capacity-value, +// while new baskets will receive the updated capacity-value, which made sense to me. + +public class BasketCapacity { + public int basketCapacity; + + public BasketCapacity(int capacity){ + this.basketCapacity = capacity; + } + + public int getCapacity(){ + return this.basketCapacity; + } + + public boolean setCapacity(int capacity){ + if (capacity != this.basketCapacity){ + this.basketCapacity = capacity; + return true; + } else { + return false; + } + } +} diff --git a/src/main/java/com/booleanuk/core/ClassDiagram.png b/src/main/java/com/booleanuk/core/ClassDiagram.png new file mode 100644 index 000000000..1f009a157 Binary files /dev/null and b/src/main/java/com/booleanuk/core/ClassDiagram.png differ 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..fc7827aea --- /dev/null +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -0,0 +1,33 @@ +package com.booleanuk.core; +import com.booleanuk.core.Item.Item; +import java.util.HashMap; + +public class Inventory { + HashMap mapOfItems; + + public Inventory(HashMap mapOfItems){ + this.mapOfItems = mapOfItems; + } + + public void increaseCount(Item item){ + mapOfItems.put(item.getType(), mapOfItems.getOrDefault(item.getType(), 0) + 1); + } + + public boolean reduceCount(Item item){ + if (!mapOfItems.containsKey(item.getType())){ + return false; + } else { + if (mapOfItems.get(item.getType()) < 1) { + return false; + } + else { + mapOfItems.put(item.getType(), mapOfItems.getOrDefault(item.getType(), 0) - 1); + return true; + } + } + } + + public boolean exists(Item item){ + return mapOfItems.containsKey(item.getType()) && mapOfItems.get(item.getType()) > 0; + } +} diff --git a/src/main/java/com/booleanuk/core/Item/Bagel/Bagel.java b/src/main/java/com/booleanuk/core/Item/Bagel/Bagel.java new file mode 100644 index 000000000..3b57ec8bc --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item/Bagel/Bagel.java @@ -0,0 +1,44 @@ +package com.booleanuk.core.Item.Bagel; +import com.booleanuk.core.Item.Filling.Filling; +import com.booleanuk.core.Item.Item; +import java.util.ArrayList; +import java.util.List; + +public class Bagel implements Item { + private final List filling; + private final double price; + private final String type; + private final String sku; + + public Bagel(BagelType type){ + this.price = type.equals(BagelType.PLAIN)? 0.39 : 0.49; + filling = new ArrayList<>(); + this.type = type.toString(); + this.sku = switch(type){ + case PLAIN -> "BGLP"; + case ONION -> "BGLO"; + case EVERYTHING -> "BGLE"; + case SESAME -> "BGLS"; + }; + } + + public String getSku(){ + return this.sku; + } + + public double getPrice(){ + return this.price; + } + + public String getType(){ + return this.type; + } + + public List getFilling(){ + return this.filling; + } + + public void addFilling(Filling filling){ + this.filling.add(filling); + } +} diff --git a/src/main/java/com/booleanuk/core/Item/Bagel/BagelType.java b/src/main/java/com/booleanuk/core/Item/Bagel/BagelType.java new file mode 100644 index 000000000..5fd4f3d48 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item/Bagel/BagelType.java @@ -0,0 +1,8 @@ +package com.booleanuk.core.Item.Bagel; + +public enum BagelType{ + ONION, + PLAIN, + EVERYTHING, + SESAME +} diff --git a/src/main/java/com/booleanuk/core/Item/Coffee/Coffee.java b/src/main/java/com/booleanuk/core/Item/Coffee/Coffee.java new file mode 100644 index 000000000..b6035ac79 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item/Coffee/Coffee.java @@ -0,0 +1,36 @@ +package com.booleanuk.core.Item.Coffee; +import com.booleanuk.core.Item.Item; + +public class Coffee implements Item { + private final double price; + private final String type; + private final String sku; + + public Coffee(CoffeeType type){ + this.price = switch(type){ + case BLACK -> 0.99; + case WHITE -> 1.19; + default -> 1.29; + }; + this.type = type.toString(); + this.sku = switch(type){ + case BLACK -> "COFB"; + case WHITE -> "COFW"; + case CAPPUCCINO -> "COFC"; + case LATTE -> "COFL"; + }; + } + + public String getSku(){ + return this.sku; + } + + public double getPrice(){ + return this.price; + } + + @Override + public String getType(){ + return this.type; + } +} diff --git a/src/main/java/com/booleanuk/core/Item/Coffee/CoffeeType.java b/src/main/java/com/booleanuk/core/Item/Coffee/CoffeeType.java new file mode 100644 index 000000000..fb3e89967 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item/Coffee/CoffeeType.java @@ -0,0 +1,8 @@ +package com.booleanuk.core.Item.Coffee; + +public enum CoffeeType{ + BLACK, + WHITE, + CAPPUCCINO, + LATTE +} diff --git a/src/main/java/com/booleanuk/core/Item/Filling/Filling.java b/src/main/java/com/booleanuk/core/Item/Filling/Filling.java new file mode 100644 index 000000000..93393039d --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item/Filling/Filling.java @@ -0,0 +1,34 @@ +package com.booleanuk.core.Item.Filling; +import com.booleanuk.core.Item.Item; + +public class Filling implements Item { + private final double price; + private final String type; + private final String sku; + + public Filling(FillingType type){ + this.price = 0.12; + this.type = type.toString(); + this.sku = switch(type){ + case BACON -> "FILB"; + case EGG -> "FILE"; + case CHEESE -> "FILC"; + case CREAM_CHEESE -> "FILX"; + case SMOKED_SALMON -> "FILS"; + case HAM -> "FILH"; + }; + } + + public String getSku(){ + return this.sku; + } + + public double getPrice(){ + return this.price; + } + + @Override + public String getType(){ + return this.type; + } +} diff --git a/src/main/java/com/booleanuk/core/Item/Filling/FillingType.java b/src/main/java/com/booleanuk/core/Item/Filling/FillingType.java new file mode 100644 index 000000000..37230d8ed --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item/Filling/FillingType.java @@ -0,0 +1,10 @@ +package com.booleanuk.core.Item.Filling; + +public enum FillingType{ + BACON, + EGG, + CHEESE, + CREAM_CHEESE, + SMOKED_SALMON, + HAM +} diff --git a/src/main/java/com/booleanuk/core/Item/Item.java b/src/main/java/com/booleanuk/core/Item/Item.java new file mode 100644 index 000000000..144e879ee --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item/Item.java @@ -0,0 +1,7 @@ +package com.booleanuk.core.Item; + +public interface Item { + double getPrice(); + String getType(); + String getSku(); +} diff --git a/src/main/java/com/booleanuk/core/Person/Customer.java b/src/main/java/com/booleanuk/core/Person/Customer.java new file mode 100644 index 000000000..b93443dc4 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Person/Customer.java @@ -0,0 +1,15 @@ +package com.booleanuk.core.Person; +import com.booleanuk.core.Basket; + +public class Customer extends Member{ + private final int customerId; + + public Customer(String name, int id, Basket basket, int customerId) { + super(name, id, basket); + this.customerId = customerId; + } + + public int getCustomerId(){ + return this.customerId; + } +} diff --git a/src/main/java/com/booleanuk/core/Person/Manager.java b/src/main/java/com/booleanuk/core/Person/Manager.java new file mode 100644 index 000000000..5cf53ee35 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Person/Manager.java @@ -0,0 +1,13 @@ +package com.booleanuk.core.Person; +import com.booleanuk.core.BasketCapacity; + +public class Manager extends Person{ + + public Manager(String name) { + super(name); + } + + public boolean changeBasketCapacity(BasketCapacity basketCapacity, int capacity){ + return basketCapacity.setCapacity(capacity); + } +} diff --git a/src/main/java/com/booleanuk/core/Person/Member.java b/src/main/java/com/booleanuk/core/Person/Member.java new file mode 100644 index 000000000..d21efaa63 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Person/Member.java @@ -0,0 +1,44 @@ +package com.booleanuk.core.Person; +import com.booleanuk.core.Basket; +import com.booleanuk.core.Item.Bagel.Bagel; +import com.booleanuk.core.Item.Filling.Filling; +import com.booleanuk.core.Item.Item; + +public class Member extends Person{ + private final int id; + private final Basket basket; + + public Member(String name, int id, Basket basket) { + super(name); + this.id = id; + this.basket = basket; + } + + public int getMemberId(){ + return this.id; + } + + public boolean addItemToBasket(Item item){ + return basket.add(item); + } + + public boolean removeItemFromBasket(Item item){ + return basket.remove(item); + } + + public double totalCost(){ + return basket.total(); + } + + public double getPriceOfItem(Item item){ + return item.getPrice(); + } + + public void chooseFillingForBagel(Bagel bagel, Filling filling){ + bagel.addFilling(filling); + } + + public void printReceiptFromBasket(){ + basket.printReceipt(); + } +} diff --git a/src/main/java/com/booleanuk/core/Person/Person.java b/src/main/java/com/booleanuk/core/Person/Person.java new file mode 100644 index 000000000..c26ff2ee2 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Person/Person.java @@ -0,0 +1,13 @@ +package com.booleanuk.core.Person; + +public class Person { + private final String name; + + public Person(String name){ + this.name = name; + } + + public String getName(){ + return this.name; + } +} 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..d93971d0b --- /dev/null +++ b/src/main/java/com/booleanuk/core/domain-model.md @@ -0,0 +1,43 @@ +| Class | Method | Scenario | Output | +|-----------|--------------------------------------------|----------------------------------|----------------| +| Member | boolean addItemFromBasket(Item item) | If basket is full | false | +| Basket | boolean add(Item item) | If space in basket | true | +| | | | | +| Member | boolean removeItemFromBasket(Item item) | If item doesn't exist | false | +| Basket | boolean remove(Item item) | If item exists | true | +| | | | | +| Basket | boolean isFull() | If basket is full | true | +| | | If basket is not full | false | +| | | | | +| Manager | boolean changeBasketCapacity(int capacity) | If capacity is the same | false | +| | | If capacity is changed | true | +| | | | | +| Customer | double totalCostInBasket() | If no items in basket | 0 | +| Basket | double totalCost() | If items in basket | sum | +| | | | | +| Customer | double getPriceOfBagel(Bagel bagel) | If bagel doesn't exist | null | +| Bagel | double getPrice() | If bagel exists | price | +| | | | | +| Customer | boolean chooseFilling(Filling filling) | If filling doesn't exist | false | +| Bagel | boolean addFilling(Filling filling) | If filling exists | true | +| | | | | +| Customer | double getPriceOfFilling(Filling filling) | If filling doesn't exist | null | +| Filling | double getPrice() | If filling exists | price | +| | | | | +| Inventory | boolean doesItemExist(Item item) | If item doesn't exist | false | +| | | If item exists | true | +| | | | | +| Basket | //updated// double totalCost() | If no discounts are available | total | +| | | If discounts are available | total-discount | +| | | | | +| Basket | double findDiscount() | If discount is found | discount | +| | | If no discount is found | 0.0 | +| | | | | +| Basket | void printReceipt() | no scenario affects return value | void | +| | | | | +| | | | | +| | | | | + + +Some methods and classes in the code are not included here, as they weren't needed based on the +user stories. \ No newline at end of file diff --git a/src/test/java/com/booleanuk/core/BobsBagelTests.java b/src/test/java/com/booleanuk/core/BobsBagelTests.java new file mode 100644 index 000000000..ba1db53e4 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BobsBagelTests.java @@ -0,0 +1,285 @@ +package com.booleanuk.core; + +import com.booleanuk.core.Item.Bagel.*; +import com.booleanuk.core.Item.Filling.*; +import com.booleanuk.core.Item.Coffee.*; +import com.booleanuk.core.Person.Customer; +import com.booleanuk.core.Person.Manager; +import com.booleanuk.core.Person.Member; +import com.booleanuk.core.Person.Person; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; + +import static com.booleanuk.core.Item.Bagel.BagelType.*; +import static com.booleanuk.core.Item.Filling.FillingType.*; +import static com.booleanuk.core.Item.Coffee.CoffeeType.*; + +public class BobsBagelTests { + + @Test + public void testBagel(){ + Bagel bagelOnion = new Bagel(ONION); + Bagel bagelPlain = new Bagel(PLAIN); + Bagel bagelEverything = new Bagel(EVERYTHING); + Bagel bagelSesame = new Bagel(SESAME); + + Filling hamFilling = new Filling(HAM); + + // test getPrice method and addFilling + + double onionPrice = bagelOnion.getPrice(); + double plainPrice = bagelPlain.getPrice(); + double everythingPrice = bagelEverything.getPrice(); + double sesamePrice = bagelSesame.getPrice(); + + bagelOnion.addFilling(hamFilling); + + Assertions.assertEquals(0.49, onionPrice); + Assertions.assertEquals(0.39, plainPrice); + Assertions.assertEquals(0.49, everythingPrice); + Assertions.assertEquals(0.49, sesamePrice); + + + Assertions.assertEquals(hamFilling, bagelOnion.getFilling().getFirst()); + } + + @Test + public void testFilling(){ + + Filling fillingBacon = new Filling(BACON); + Filling fillingEgg = new Filling(EGG); + Filling fillingCheese = new Filling(CHEESE); + Filling fillingCreamCheese = new Filling(CREAM_CHEESE); + Filling fillingSmokedSalmon = new Filling(SMOKED_SALMON); + Filling fillingHam = new Filling(HAM); + + // test getPrice method + + double baconPrice = fillingBacon.getPrice(); + double eggPrice = fillingEgg.getPrice(); + double cheesePrice = fillingCheese.getPrice(); + double creamCheesePrice = fillingCreamCheese.getPrice(); + double smokedSalmonPrice = fillingSmokedSalmon.getPrice(); + double hamPrice = fillingHam.getPrice(); + + + Assertions.assertEquals(0.12, baconPrice); + Assertions.assertEquals(0.12, eggPrice); + Assertions.assertEquals(0.12, cheesePrice); + Assertions.assertEquals(0.12, creamCheesePrice); + Assertions.assertEquals(0.12, smokedSalmonPrice); + Assertions.assertEquals(0.12, hamPrice); + } + + @Test + public void testCoffee(){ + + Coffee coffeeBlack = new Coffee(BLACK); + Coffee coffeeWhite = new Coffee(WHITE); + Coffee coffeeCappuccino = new Coffee(CAPPUCCINO); + Coffee coffeeLatte = new Coffee(LATTE); + + // test getPrice method + + double blackPrice = coffeeBlack.getPrice(); + double whitePrice = coffeeWhite.getPrice(); + double cappuccinoPrice = coffeeCappuccino.getPrice(); + double lattePrice = coffeeLatte.getPrice(); + + + Assertions.assertEquals(0.99, blackPrice); + Assertions.assertEquals(1.19, whitePrice); + Assertions.assertEquals(1.29, cappuccinoPrice); + Assertions.assertEquals(1.29, lattePrice); + + } + + @Test + public void testItem(){ + + // Testing the Item interface + + Bagel itemBagel = new Bagel(ONION); + Filling itemFilling = new Filling(HAM); + + itemBagel.addFilling(itemFilling); + + Assertions.assertEquals(0.49, itemBagel.getPrice()); + Assertions.assertEquals(itemFilling, itemBagel.getFilling().getFirst()); + + } + + @Test + public void testInventory(){ + + HashMap mapOfItems = new HashMap<>(){{ + put(PLAIN.toString(), 5); + put(ONION.toString(), 5); + put(EVERYTHING.toString(), 4); + put(BACON.toString(), 7); + put(CHEESE.toString(), 1); + put(WHITE.toString(), 3); + put(LATTE.toString(), 2); + put(HAM.toString(), 4); + }}; + + Bagel bagel1 = new Bagel(SESAME); + + Inventory inventory = new Inventory(mapOfItems); + + Bagel bagel2 = new Bagel(PLAIN); + Filling filling = new Filling(HAM); + Coffee coffee = new Coffee(WHITE); + + Assertions.assertTrue(inventory.exists(bagel2)); + Assertions.assertFalse(inventory.reduceCount(bagel1)); + Assertions.assertEquals(5, inventory.mapOfItems.get(PLAIN.toString())); + Assertions.assertTrue(inventory.exists(filling)); + Assertions.assertTrue(inventory.exists(coffee)); + } + + @Test + public void testBasket(){ + + HashMap mapOfItems = new HashMap<>(){{ + put(PLAIN.toString(), 5); + put(ONION.toString(), 5); + put(EVERYTHING.toString(), 4); + put(BACON.toString(), 7); + put(HAM.toString(), 3); + put(CHEESE.toString(), 1); + put(WHITE.toString(), 3); + put(LATTE.toString(), 2); + }}; + + Inventory inventory = new Inventory(mapOfItems); + + BasketCapacity basketCapacity = new BasketCapacity(10); + + Basket basket = new Basket(basketCapacity.getCapacity(), inventory); + + Bagel bagelOnion = new Bagel(ONION); + Bagel bagelPlain = new Bagel(PLAIN); + Filling fillingCheese = new Filling(CHEESE); + Filling fillingHam = new Filling(HAM); + + basket.add(bagelPlain); + basket.add(bagelOnion); + basket.add(fillingCheese); + basket.add(fillingHam); + + double expectedSum = bagelPlain.getPrice() + bagelOnion.getPrice() + fillingCheese.getPrice() + fillingHam.getPrice(); + + Assertions.assertFalse(basket.isFull()); + Assertions.assertEquals(expectedSum, basket.total()); + Assertions.assertTrue(basket.add(bagelOnion)); + Assertions.assertEquals(4, inventory.mapOfItems.get(PLAIN.toString())); + + basket.remove(bagelPlain); + + Assertions.assertEquals(5, inventory.mapOfItems.get(PLAIN.toString())); + Assertions.assertFalse(basket.remove(bagelPlain)); + + } + + @Test + public void testPerson(){ + + Person person1 = new Person("Odd"); + Person person2 = new Person("Jimmy"); + + Assertions.assertEquals("Odd", person1.getName()); + Assertions.assertNotEquals("jim", person2.getName()); + } + + @Test + public void testMember(){ + + HashMap mapOfItems = new HashMap<>(){{ + put(PLAIN.toString(), 5); + put(ONION.toString(), 5); + put(EVERYTHING.toString(), 4); + put(BACON.toString(), 7); + put(HAM.toString(), 3); + put(CHEESE.toString(), 1); + put(WHITE.toString(), 3); + put(LATTE.toString(), 2); + }}; + + Inventory inventory = new Inventory(mapOfItems); + + BasketCapacity basketCapacity = new BasketCapacity(1); + + Basket basket = new Basket(basketCapacity.getCapacity(), inventory); + Basket basket2 = new Basket(basketCapacity.getCapacity(), inventory); + + Member member1 = new Member("Cody", 5, basket); + Member member2 = new Member("Bob", 6, basket2); + + Bagel bagel = new Bagel(ONION); + + Assertions.assertTrue(member1.addItemToBasket(bagel)); + Assertions.assertFalse(member1.addItemToBasket(bagel)); + Assertions.assertTrue(member2.addItemToBasket(bagel)); + Assertions.assertTrue(member2.removeItemFromBasket(bagel)); + Assertions.assertEquals("Cody", member1.getName()); + Assertions.assertEquals(5, member1.getMemberId()); + } + + @Test + public void testCustomer(){ + + HashMap mapOfItems = new HashMap<>(){{ + put(PLAIN.toString(), 5); + put(ONION.toString(), 5); + put(EVERYTHING.toString(), 4); + put(BACON.toString(), 7); + put(HAM.toString(), 3); + put(CHEESE.toString(), 1); + put(WHITE.toString(), 3); + put(LATTE.toString(), 2); + }}; + + Inventory inventory = new Inventory(mapOfItems); + + BasketCapacity basketCapacity = new BasketCapacity(1); + + Basket basket = new Basket(basketCapacity.getCapacity(), inventory); + Basket basket2 = new Basket(basketCapacity.getCapacity(), inventory); + + Customer customer1 = new Customer("Ray", 10, basket, 19); + Customer customer2 = new Customer("Bob", 5, basket2, 21); + + Bagel bagel = new Bagel(ONION); + Filling filling = new Filling(HAM); + + customer1.chooseFillingForBagel(bagel, filling); + + Assertions.assertEquals(1, bagel.getFilling().size()); + + Assertions.assertTrue(customer1.addItemToBasket(bagel)); + Assertions.assertEquals(0.49, customer1.getPriceOfItem(bagel)); + Assertions.assertEquals(0.49, customer1.totalCost()); + Assertions.assertFalse(customer1.addItemToBasket(bagel)); + Assertions.assertTrue(customer2.addItemToBasket(bagel)); + Assertions.assertTrue(customer2.removeItemFromBasket(bagel)); + Assertions.assertEquals("Ray", customer1.getName()); + Assertions.assertEquals(10, customer1.getMemberId()); + Assertions.assertEquals(21, customer2.getCustomerId()); + } + + @Test + public void testManager(){ + + Manager manager = new Manager("Johnny"); + BasketCapacity basketCapacity = new BasketCapacity(10); + + manager.changeBasketCapacity(basketCapacity, 12); + + Assertions.assertEquals(12, basketCapacity.basketCapacity); + Assertions.assertFalse(manager.changeBasketCapacity(basketCapacity, 12)); + + } +} diff --git a/src/test/java/com/booleanuk/extension/BobsBagelExtensionTest.java b/src/test/java/com/booleanuk/extension/BobsBagelExtensionTest.java new file mode 100644 index 000000000..81f933aef --- /dev/null +++ b/src/test/java/com/booleanuk/extension/BobsBagelExtensionTest.java @@ -0,0 +1,128 @@ +package com.booleanuk.extension; + +import com.booleanuk.core.Basket; +import com.booleanuk.core.BasketCapacity; +import com.booleanuk.core.Inventory; +import com.booleanuk.core.Item.Bagel.Bagel; +import com.booleanuk.core.Item.Coffee.Coffee; +import com.booleanuk.core.Item.Filling.Filling; +import com.booleanuk.core.Person.Customer; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; + +import static com.booleanuk.core.Item.Bagel.BagelType.EVERYTHING; +import static com.booleanuk.core.Item.Bagel.BagelType.ONION; +import static com.booleanuk.core.Item.Bagel.BagelType.PLAIN; +import static com.booleanuk.core.Item.Coffee.CoffeeType.*; +import static com.booleanuk.core.Item.Coffee.CoffeeType.WHITE; +import static com.booleanuk.core.Item.Filling.FillingType.*; + +public class BobsBagelExtensionTest { + @Test + public void testDiscount(){ + + HashMap mapOfItems = new HashMap<>(){{ + put(PLAIN.toString(), 15); + put(ONION.toString(), 20); + put(EVERYTHING.toString(), 20); + put(BACON.toString(), 20); + put(HAM.toString(), 20); + put(CHEESE.toString(), 20); + put(BLACK.toString(), 20); + put(WHITE.toString(), 20); + put(LATTE.toString(), 20); + }}; + + Inventory inventory = new Inventory(mapOfItems); + + BasketCapacity basketCapacity = new BasketCapacity(15); + Basket basket = new Basket(basketCapacity.getCapacity(), inventory); + Customer customer = new Customer("Ray", 10, basket, 19); + + Bagel bagelPlain = new Bagel(PLAIN); + Coffee coffeeBlack = new Coffee(BLACK); + Filling filling = new Filling(HAM); + + for (int i=0; i < 12; i++){ + customer.addItemToBasket(bagelPlain); + } + + double totalWithoutDiscount = bagelPlain.getPrice()*12; + double totalWithDiscount = customer.totalCost(); + + Basket basket2 = new Basket(5, inventory); + + Customer customer2 = new Customer("Bella", 11, basket2, 4); + + customer2.addItemToBasket(bagelPlain); + customer2.addItemToBasket(filling); + customer2.addItemToBasket(coffeeBlack); + + double totalWithoutDiscount2 = bagelPlain.getPrice() + filling.getPrice() + coffeeBlack.getPrice(); + double totalWithDiscount2 = customer2.totalCost(); + + Basket basket3 = new Basket(9, inventory); + Customer customer3 = new Customer("Sara", 24, basket3, 91); + + Bagel bagelOnion = new Bagel(ONION); + + for (int i=0;i<6;i++){ + customer3.addItemToBasket(bagelOnion); + } + customer3.addItemToBasket(bagelOnion); + + double totalWithoutDiscount3 = bagelOnion.getPrice()*7; + double totalWithDiscount3 = customer3.totalCost(); + + Assertions.assertNotEquals(totalWithDiscount, totalWithoutDiscount, 0.0); + Assertions.assertEquals(0.39*12, totalWithoutDiscount); + Assertions.assertEquals(Math.round(3.99), Math.round(totalWithDiscount)); + + Assertions.assertNotEquals(totalWithDiscount2, totalWithoutDiscount2); + Assertions.assertEquals(0.12+1.25, totalWithDiscount2); + + Assertions.assertNotEquals(totalWithDiscount3, totalWithoutDiscount3); + Assertions.assertEquals(2.49+bagelOnion.getPrice(), totalWithDiscount3); + } + + @Test + public void testReceipt(){ + HashMap mapOfItems = new HashMap<>(){{ + put(PLAIN.toString(), 15); + put(ONION.toString(), 20); + put(EVERYTHING.toString(), 20); + put(BACON.toString(), 20); + put(HAM.toString(), 20); + put(CHEESE.toString(), 20); + put(BLACK.toString(), 20); + put(WHITE.toString(), 20); + put(LATTE.toString(), 20); + }}; + + Inventory inventory = new Inventory(mapOfItems); + + BasketCapacity basketCapacity = new BasketCapacity(15); + Basket basket = new Basket(basketCapacity.getCapacity(), inventory); + Customer customer = new Customer("Ray", 10, basket, 19); + + Bagel bagel1 = new Bagel(ONION); + Bagel bagel2 = new Bagel(PLAIN); + Filling filling1 = new Filling(HAM); + Filling filling2 = new Filling(CHEESE); + Coffee coffee1 = new Coffee(BLACK); + Coffee coffee2 = new Coffee(WHITE); + + customer.addItemToBasket(bagel1); + customer.addItemToBasket(bagel2); + customer.addItemToBasket(filling1); + customer.addItemToBasket(filling2); + customer.addItemToBasket(coffee1); + customer.addItemToBasket(coffee2); + customer.addItemToBasket(coffee2); + customer.addItemToBasket(bagel1); + + customer.printReceiptFromBasket(); + } +}