From 233a2bdb14b16edc324da5fd730946ce4872c481 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Thu, 20 Aug 2026 14:25:49 +0200 Subject: [PATCH 01/15] first draft of structure --- model.md | 87 +++++++++++++++++++ src/main/java/com/booleanuk/core/Basket.java | 5 ++ .../java/com/booleanuk/core/Customer.java | 5 ++ src/main/java/com/booleanuk/core/Item.java | 5 ++ src/main/java/com/booleanuk/core/Manger.java | 5 ++ src/main/java/com/booleanuk/core/Shop.java | 5 ++ .../com/booleanuk/core/item_types/Bagel.java | 7 ++ .../com/booleanuk/core/item_types/Coffee.java | 7 ++ .../booleanuk/core/item_types/Filling.java | 7 ++ .../java/com/booleanuk/core/BasketTest.java | 5 ++ .../java/com/booleanuk/core/CustomerTest.java | 5 ++ .../java/com/booleanuk/core/ItemTest.java | 5 ++ .../java/com/booleanuk/core/MangerTest.java | 5 ++ src/test/java/com/booleanuk/core/Shop.java | 5 ++ .../java/com/booleanuk/core/ShopTest.java | 5 ++ 15 files changed, 163 insertions(+) create mode 100644 model.md create mode 100644 src/main/java/com/booleanuk/core/Basket.java create mode 100644 src/main/java/com/booleanuk/core/Customer.java create mode 100644 src/main/java/com/booleanuk/core/Item.java create mode 100644 src/main/java/com/booleanuk/core/Manger.java create mode 100644 src/main/java/com/booleanuk/core/Shop.java create mode 100644 src/main/java/com/booleanuk/core/item_types/Bagel.java create mode 100644 src/main/java/com/booleanuk/core/item_types/Coffee.java create mode 100644 src/main/java/com/booleanuk/core/item_types/Filling.java create mode 100644 src/test/java/com/booleanuk/core/BasketTest.java create mode 100644 src/test/java/com/booleanuk/core/CustomerTest.java create mode 100644 src/test/java/com/booleanuk/core/ItemTest.java create mode 100644 src/test/java/com/booleanuk/core/MangerTest.java create mode 100644 src/test/java/com/booleanuk/core/Shop.java create mode 100644 src/test/java/com/booleanuk/core/ShopTest.java diff --git a/model.md b/model.md new file mode 100644 index 000000000..0ecd35e88 --- /dev/null +++ b/model.md @@ -0,0 +1,87 @@ +```mermaid +classDiagram + class Shop { + -Stock map + + +isInStock() boolean + +sold(sku) void + + } + + class Basket { + -int size + -Array items + + +add(item.type.type) boolean + +getCost(item.type.type) float + +delete() boolean + +totalCost() + +protected changeSize() ? + + } + + class Manager { + -name + + +changeBasketCap() void + } + + class Item { + -sku + -price + -varient + + +get() + +set() + + } + + class Bagel{ + -type + + +get() + +set() + } + + class Coffee{ + -type + + + +get() + +set() + } + + class Filling{ + -type + + + +get() + +set() + } + + class Customer{ + -name + -email + -password? + + +add() + +seePrice() + +getReciept() + + } + + Shop o-- Basket : has a + + Customer o-- Basket : has a + + Basket o-- Item : has + + Manager o-- Shop : change values + + Bagel o-- Item: extends + Coffee o-- Item: extends + Filling o-- Item: extends + + Bagel o-- Filling: has + +``` 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..90cc2387e --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class Basket { + +} diff --git a/src/main/java/com/booleanuk/core/Customer.java b/src/main/java/com/booleanuk/core/Customer.java new file mode 100644 index 000000000..dd79454e1 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Customer.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class Customer { + +} 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..fe84b890b --- /dev/null +++ b/src/main/java/com/booleanuk/core/Item.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class Item { + +} diff --git a/src/main/java/com/booleanuk/core/Manger.java b/src/main/java/com/booleanuk/core/Manger.java new file mode 100644 index 000000000..2e8aa0262 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Manger.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class Manger { + +} diff --git a/src/main/java/com/booleanuk/core/Shop.java b/src/main/java/com/booleanuk/core/Shop.java new file mode 100644 index 000000000..06404d9dc --- /dev/null +++ b/src/main/java/com/booleanuk/core/Shop.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class Shop { + +} diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java new file mode 100644 index 000000000..fa299150d --- /dev/null +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -0,0 +1,7 @@ +package com.booleanuk.core.item_types; + +import com.booleanuk.core.Item; + +public class Bagel extends Item{ + +} diff --git a/src/main/java/com/booleanuk/core/item_types/Coffee.java b/src/main/java/com/booleanuk/core/item_types/Coffee.java new file mode 100644 index 000000000..5a104b663 --- /dev/null +++ b/src/main/java/com/booleanuk/core/item_types/Coffee.java @@ -0,0 +1,7 @@ +package com.booleanuk.core.item_types; + +import com.booleanuk.core.Item; + +public class Coffee extends Item { + +} diff --git a/src/main/java/com/booleanuk/core/item_types/Filling.java b/src/main/java/com/booleanuk/core/item_types/Filling.java new file mode 100644 index 000000000..13cefbe23 --- /dev/null +++ b/src/main/java/com/booleanuk/core/item_types/Filling.java @@ -0,0 +1,7 @@ +package com.booleanuk.core.item_types; + +import com.booleanuk.core.Item; + +public class Filling extends Item { + +} 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..0f8146214 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class BasketTest { + +} diff --git a/src/test/java/com/booleanuk/core/CustomerTest.java b/src/test/java/com/booleanuk/core/CustomerTest.java new file mode 100644 index 000000000..603478f6f --- /dev/null +++ b/src/test/java/com/booleanuk/core/CustomerTest.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class CustomerTest { + +} 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..d2687d311 --- /dev/null +++ b/src/test/java/com/booleanuk/core/ItemTest.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class ItemTest { + +} diff --git a/src/test/java/com/booleanuk/core/MangerTest.java b/src/test/java/com/booleanuk/core/MangerTest.java new file mode 100644 index 000000000..f1930f404 --- /dev/null +++ b/src/test/java/com/booleanuk/core/MangerTest.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class MangerTest { + +} diff --git a/src/test/java/com/booleanuk/core/Shop.java b/src/test/java/com/booleanuk/core/Shop.java new file mode 100644 index 000000000..06404d9dc --- /dev/null +++ b/src/test/java/com/booleanuk/core/Shop.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class Shop { + +} diff --git a/src/test/java/com/booleanuk/core/ShopTest.java b/src/test/java/com/booleanuk/core/ShopTest.java new file mode 100644 index 000000000..bf210fdfa --- /dev/null +++ b/src/test/java/com/booleanuk/core/ShopTest.java @@ -0,0 +1,5 @@ +package com.booleanuk.core; + +public class ShopTest { + +} From d1525a0b808cf7f3d5e44ba8ac43fc3e4c097075 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Thu, 20 Aug 2026 19:12:42 +0200 Subject: [PATCH 02/15] done some boilerplate and logic... --- model.md | 12 +--- src/main/java/com/booleanuk/core/Basket.java | 47 +++++++++++++++- src/main/java/com/booleanuk/core/Item.java | 56 ++++++++++++++++++- src/main/java/com/booleanuk/core/Shop.java | 24 +++++++- .../com/booleanuk/core/item_types/Bagel.java | 22 +++++++- .../com/booleanuk/core/item_types/Coffee.java | 14 ++++- .../booleanuk/core/item_types/Filling.java | 15 ++++- .../java/com/booleanuk/core/BasketTest.java | 35 ++++++++++++ src/test/java/com/booleanuk/core/Shop.java | 5 -- 9 files changed, 209 insertions(+), 21 deletions(-) delete mode 100644 src/test/java/com/booleanuk/core/Shop.java diff --git a/model.md b/model.md index 0ecd35e88..bdf6c6720 100644 --- a/model.md +++ b/model.md @@ -12,11 +12,12 @@ classDiagram -int size -Array items - +add(item.type.type) boolean + +add(item.type.type) boolean make seperate adds? +getCost(item.type.type) float +delete() boolean +totalCost() +protected changeSize() ? + +getBasket() String (what the basket has) } @@ -39,24 +40,16 @@ classDiagram class Bagel{ -type - +get() - +set() } class Coffee{ -type - - +get() - +set() } class Filling{ -type - - +get() - +set() } class Customer{ @@ -82,6 +75,5 @@ classDiagram Coffee o-- Item: extends Filling o-- Item: extends - Bagel o-- Filling: has ``` diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 90cc2387e..62f00473f 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,50 @@ package com.booleanuk.core; public class Basket { - + private int size; + private Object[] arr; + + public Basket(){ + this.size = 10; + this.arr = new Object[this.size]; + } + + public void addBagel(String sku){ + + } + + public void addCoffee(String sku){ + + } + + public void addFilling(String sku){ + + } + + public boolean removeBagel(String sku){ + return false; + } + + public boolean removeCoffee(String sku){ + return false; + } + + public boolean removeFilling(String sku){ + return false; + } + + public float totalCost(){ + return 0f; + } + + protected void changeSize(){ + + } + + @Override + public String toString(){ + String tmp = ""; + return tmp; + } + } diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index fe84b890b..eace1a9e0 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -1,5 +1,59 @@ package com.booleanuk.core; +import java.util.HashMap; +import java.util.Map; + public class Item { - + protected float price; + protected String sku; + protected String varient; + protected String name; + protected Map inventory; + + public Item(String sku) { + this.inventory = new HashMap(); + this.inventory.put("BGLO", new Object[]{0.49, "Bagel", "Onion"}); + this.inventory.put("BGLP", new Object[]{0.39, "Bagel", "Plain"}); + this.inventory.put("BGLE", new Object[]{0.49, "Bagel", "Everything"}); + this.inventory.put("BGLS", new Object[]{0.49, "Bagel", "Sesame"}); + this.inventory.put("COFB", new Object[]{0.99, "Coffee", "Black"}); + this.inventory.put("COFW", new Object[]{1.19, "Coffee", "White"}); + this.inventory.put("COFC", new Object[]{1.29, "Coffee", "Capuccino"}); + this.inventory.put("COFL", new Object[]{1.29, "Coffee", "Latte"}); + this.inventory.put("FILB", new Object[]{0.12, "Filling", "Bacon"}); + this.inventory.put("FILE", new Object[]{0.12, "Filling", "Egg"}); + this.inventory.put("FILC", new Object[]{0.12, "Filling", "Cheese"}); + this.inventory.put("FILX", new Object[]{0.12, "Filling", "Cream Cheese"}); + this.inventory.put("FILS", new Object[]{0.12, "Filling", "Smoked Salmon"}); + this.inventory.put("FILH", new Object[]{0.12, "Filling", "Ham"}); + try{ + this.sku = sku; + this.price = (float) this.inventory.get(sku)[0]; + this.varient = (String) this.inventory.get(sku)[1]; + this.name = (String) this.inventory.get(sku)[2]; + } + catch (Exception e){ + System.out.println("Not a valid SKU"); + throw e; + } + + } + + public float getPrice(){ + return this.price; + } + + public String getSKU(){ + return this.sku; + } + + public String getVarient(){ + return this.varient; + } + + public String getName(){ + return this.name; + } + + } diff --git a/src/main/java/com/booleanuk/core/Shop.java b/src/main/java/com/booleanuk/core/Shop.java index 06404d9dc..4e2465172 100644 --- a/src/main/java/com/booleanuk/core/Shop.java +++ b/src/main/java/com/booleanuk/core/Shop.java @@ -1,5 +1,27 @@ package com.booleanuk.core; +import java.util.Map; +import java.util.HashMap; + public class Shop { - + private Map stock; + + public Shop(){ + this.stock = new HashMap(); + this.stock.put("BGLO", 20); + this.stock.put("BGLP", 21); + this.stock.put("BGLE", 22); + this.stock.put("BGLS", 23); + this.stock.put("COFB", 24); + this.stock.put("COFW", 25); + this.stock.put("COFC", 26); + this.stock.put("COFL", 27); + this.stock.put("FILB", 28); + this.stock.put("FILE", 29); + this.stock.put("FILC", 30); + this.stock.put("FILX", 31); + this.stock.put("FILS", 32); + this.stock.put("FILH", 33); + } } + diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index fa299150d..5bae4aab3 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -1,7 +1,27 @@ package com.booleanuk.core.item_types; + import com.booleanuk.core.Item; +import java.util.Arrays; +import java.lang.Exception; +import java.util.List; + public class Bagel extends Item{ - + private static final String[] bagelSKU = {"BGLO", "BGLP", "BGLE", "BGLS"}; + private List filling; + + public Bagel(String sku) throws Exception{ + super(sku); + boolean contains = Arrays.stream(this.bagelSKU).anyMatch(sku::equals); + if(!contains){ + throw new Exception("Must be a valid bagel SKU!"); + } + + } + + public void addFilling(Filling filling){ + this.filling.add(filling); + } // remove this? is does this even work? + } diff --git a/src/main/java/com/booleanuk/core/item_types/Coffee.java b/src/main/java/com/booleanuk/core/item_types/Coffee.java index 5a104b663..26b90137c 100644 --- a/src/main/java/com/booleanuk/core/item_types/Coffee.java +++ b/src/main/java/com/booleanuk/core/item_types/Coffee.java @@ -2,6 +2,18 @@ import com.booleanuk.core.Item; +import java.util.Arrays; +import java.lang.Exception; + public class Coffee extends Item { - + private static final String[] coffeeSKU = {"COFB", "COFW", "COFC", "COFL"}; + + public Coffee(String sku) throws Exception{ + super(sku); + boolean contains = Arrays.stream(this.coffeeSKU).anyMatch(sku::equals); + if(!contains){ + throw new Exception("Must be a valid coffee SKU!"); + } + + } } diff --git a/src/main/java/com/booleanuk/core/item_types/Filling.java b/src/main/java/com/booleanuk/core/item_types/Filling.java index 13cefbe23..de7e8b857 100644 --- a/src/main/java/com/booleanuk/core/item_types/Filling.java +++ b/src/main/java/com/booleanuk/core/item_types/Filling.java @@ -2,6 +2,19 @@ import com.booleanuk.core.Item; +import java.util.Arrays; +import java.lang.Exception; + public class Filling extends Item { - + + private static final String[] fillingSKU = {"FILB", "FILE", "FILC", "FILX", "FILS", "FILH"}; + + public Filling(String sku) throws Exception{ + super(sku); + boolean contains = Arrays.stream(this.fillingSKU).anyMatch(sku::equals); + if(!contains){ + throw new Exception("Must be a valid filling SKU!"); + } + + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 0f8146214..39ce9d104 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -2,4 +2,39 @@ public class BasketTest { + public Basket b; + + public BasketTest() { + this.b = new Basket(); + } + + @Test + public void addBagelTest(){ + Bagel bag = new Bagel("BGLO"); + b.addBagel(bag); + Assertions.assertEquals( "BGLO Onion Bagel", b.toString()); + } + + @Test + public void addToppingTest(){ + Topping top = new Topping("FILB"); + b.addTopping(top); + Assertions.assertEquals("FILB Bacon Filling", b.toString()); + } + + @Test + public void addCoffeeTest(){ + Coffee top = new Coffee("COFB"); + b.addCoffee(top); + Assertions.assertEquals("COFB Black Coffee", b.toString()); + } + + @Test + public void getCostTest(){ + Bagel bag = new Bagel("BGLP"); // Bagel plain 0.39 + bag.addTopping("FILS"); // Smoked Salmon 0.12 + b.addBagel(bag); + Assertions.assertEquals(0.39 + 0.12, b.getCost(bag)); + } + } diff --git a/src/test/java/com/booleanuk/core/Shop.java b/src/test/java/com/booleanuk/core/Shop.java deleted file mode 100644 index 06404d9dc..000000000 --- a/src/test/java/com/booleanuk/core/Shop.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.booleanuk.core; - -public class Shop { - -} From c8c0a3300861fe759b4a8a0ca66683cd8f227104 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Thu, 20 Aug 2026 21:52:13 +0200 Subject: [PATCH 03/15] need to test! --- model.md | 17 +++--- src/main/java/com/booleanuk/core/Basket.java | 59 ++++++++++++------- .../java/com/booleanuk/core/Customer.java | 10 +++- src/main/java/com/booleanuk/core/Manger.java | 6 +- src/main/java/com/booleanuk/core/Shop.java | 27 --------- .../com/booleanuk/core/item_types/Bagel.java | 2 +- .../com/booleanuk/core/item_types/Coffee.java | 2 +- .../booleanuk/core/item_types/Filling.java | 2 +- .../java/com/booleanuk/core/BasketTest.java | 23 ++++---- .../java/com/booleanuk/core/ShopTest.java | 5 -- 10 files changed, 72 insertions(+), 81 deletions(-) delete mode 100644 src/main/java/com/booleanuk/core/Shop.java delete mode 100644 src/test/java/com/booleanuk/core/ShopTest.java diff --git a/model.md b/model.md index bdf6c6720..3e446c169 100644 --- a/model.md +++ b/model.md @@ -1,6 +1,6 @@ ```mermaid classDiagram - class Shop { + class Shop might be added later { -Stock map +isInStock() boolean @@ -16,7 +16,6 @@ classDiagram +getCost(item.type.type) float +delete() boolean +totalCost() - +protected changeSize() ? +getBasket() String (what the basket has) } @@ -24,7 +23,7 @@ classDiagram class Manager { -name - +changeBasketCap() void + +changeBasketSize() void } class Item { @@ -39,6 +38,7 @@ classDiagram class Bagel{ -type + -could have filling, but this can lead to amount of items being over amount in capacity unless you dont count fillings as items... } @@ -53,23 +53,20 @@ classDiagram } class Customer{ - -name - -email + -name? + -email? -password? - +add() - +seePrice() - +getReciept() + +getBasket()? } - Shop o-- Basket : has a Customer o-- Basket : has a Basket o-- Item : has - Manager o-- Shop : change values + Manager o-- Basket : change values Bagel o-- Item: extends Coffee o-- Item: extends diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 62f00473f..6d3acdd91 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,44 +1,59 @@ package com.booleanuk.core; +import com.booleanuk.core.item_types.*; + public class Basket { - private int size; - private Object[] arr; + protected static int size = 10; + private int curSize; + private Item[] arr; public Basket(){ - this.size = 10; - this.arr = new Object[this.size]; + this.arr = new Item[Basket.size]; + this.curSize = 0; } - public void addBagel(String sku){ - + public void addBagel(String sku) throws Exception{ + if(curSize >= size) + throw new Exception("Exceeds maximum basket size!"); + this.arr[this.curSize] = new Bagel(sku); + this.curSize++; } - public void addCoffee(String sku){ - + public void addCoffee(String sku) throws Exception{ + if(curSize >= size) + throw new Exception("Exceeds maximum basket size!"); + this.arr[this.curSize] = new Coffee(sku); + this.curSize++; } - public void addFilling(String sku){ - + public void addFilling(String sku) throws Exception{ + if(curSize >= size) + throw new Exception("Exceeds maximum basket size!"); + this.arr[this.curSize] = new Filling(sku); + this.curSize++; } - public boolean removeBagel(String sku){ + public boolean removeItem(String sku){ + for (int i = 0; i < this.curSize; i++){ + if(this.arr[i].getSKU() == sku){ + this.arr[i] = null; + this.curSize--; + return true; + } + } return false; } - public boolean removeCoffee(String sku){ - return false; - } - - public boolean removeFilling(String sku){ - return false; + public float costOf(String sku){ + return new Item(sku).getPrice(); } public float totalCost(){ - return 0f; - } - - protected void changeSize(){ - + float sum = 0f; + for (int i = 0; i < this.curSize; i++){ + sum += this.arr[i].getPrice(); + } + return sum; } @Override diff --git a/src/main/java/com/booleanuk/core/Customer.java b/src/main/java/com/booleanuk/core/Customer.java index dd79454e1..ff9f88848 100644 --- a/src/main/java/com/booleanuk/core/Customer.java +++ b/src/main/java/com/booleanuk/core/Customer.java @@ -1,5 +1,13 @@ package com.booleanuk.core; public class Customer { - + private Basket basket; + + public Customer(Basket basket){ + this.basket = basket; + } + + public Basket getBasket(){ + return this.basket; + } } diff --git a/src/main/java/com/booleanuk/core/Manger.java b/src/main/java/com/booleanuk/core/Manger.java index 2e8aa0262..d9dbf8e94 100644 --- a/src/main/java/com/booleanuk/core/Manger.java +++ b/src/main/java/com/booleanuk/core/Manger.java @@ -1,5 +1,7 @@ package com.booleanuk.core; -public class Manger { - +public class Manger extends Basket{ + public static void changeBasketSize(int size){ + Basket.size = size; + } } diff --git a/src/main/java/com/booleanuk/core/Shop.java b/src/main/java/com/booleanuk/core/Shop.java deleted file mode 100644 index 4e2465172..000000000 --- a/src/main/java/com/booleanuk/core/Shop.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.booleanuk.core; - -import java.util.Map; -import java.util.HashMap; - -public class Shop { - private Map stock; - - public Shop(){ - this.stock = new HashMap(); - this.stock.put("BGLO", 20); - this.stock.put("BGLP", 21); - this.stock.put("BGLE", 22); - this.stock.put("BGLS", 23); - this.stock.put("COFB", 24); - this.stock.put("COFW", 25); - this.stock.put("COFC", 26); - this.stock.put("COFL", 27); - this.stock.put("FILB", 28); - this.stock.put("FILE", 29); - this.stock.put("FILC", 30); - this.stock.put("FILX", 31); - this.stock.put("FILS", 32); - this.stock.put("FILH", 33); - } -} - diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index 5bae4aab3..8d9ad1655 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -13,7 +13,7 @@ public class Bagel extends Item{ public Bagel(String sku) throws Exception{ super(sku); - boolean contains = Arrays.stream(this.bagelSKU).anyMatch(sku::equals); + boolean contains = Arrays.stream(Bagel.bagelSKU).anyMatch(sku::equals); if(!contains){ throw new Exception("Must be a valid bagel SKU!"); } diff --git a/src/main/java/com/booleanuk/core/item_types/Coffee.java b/src/main/java/com/booleanuk/core/item_types/Coffee.java index 26b90137c..f558ad614 100644 --- a/src/main/java/com/booleanuk/core/item_types/Coffee.java +++ b/src/main/java/com/booleanuk/core/item_types/Coffee.java @@ -10,7 +10,7 @@ public class Coffee extends Item { public Coffee(String sku) throws Exception{ super(sku); - boolean contains = Arrays.stream(this.coffeeSKU).anyMatch(sku::equals); + boolean contains = Arrays.stream(Coffee.coffeeSKU).anyMatch(sku::equals); if(!contains){ throw new Exception("Must be a valid coffee SKU!"); } diff --git a/src/main/java/com/booleanuk/core/item_types/Filling.java b/src/main/java/com/booleanuk/core/item_types/Filling.java index de7e8b857..335dff49e 100644 --- a/src/main/java/com/booleanuk/core/item_types/Filling.java +++ b/src/main/java/com/booleanuk/core/item_types/Filling.java @@ -11,7 +11,7 @@ public class Filling extends Item { public Filling(String sku) throws Exception{ super(sku); - boolean contains = Arrays.stream(this.fillingSKU).anyMatch(sku::equals); + boolean contains = Arrays.stream(Filling.fillingSKU).anyMatch(sku::equals); if(!contains){ throw new Exception("Must be a valid filling SKU!"); } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 39ce9d104..cf938004f 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -1,5 +1,11 @@ package com.booleanuk.core; +import org.junit.jupiter.api.Assertions; + +import com.booleanuk.core.item_types.Bagel; +import com.booleanuk.core.item_types.Coffee; + + public class BasketTest { public Basket b; @@ -10,31 +16,26 @@ public BasketTest() { @Test public void addBagelTest(){ - Bagel bag = new Bagel("BGLO"); - b.addBagel(bag); + b.addBagel("BGLO"); Assertions.assertEquals( "BGLO Onion Bagel", b.toString()); } @Test - public void addToppingTest(){ - Topping top = new Topping("FILB"); - b.addTopping(top); + public void addFillingTest(){ + b.addFilling("FILB"); Assertions.assertEquals("FILB Bacon Filling", b.toString()); } @Test public void addCoffeeTest(){ - Coffee top = new Coffee("COFB"); - b.addCoffee(top); + b.addCoffee("COFB"); Assertions.assertEquals("COFB Black Coffee", b.toString()); } @Test public void getCostTest(){ - Bagel bag = new Bagel("BGLP"); // Bagel plain 0.39 - bag.addTopping("FILS"); // Smoked Salmon 0.12 - b.addBagel(bag); - Assertions.assertEquals(0.39 + 0.12, b.getCost(bag)); + //"BGLP" Bagel plain 0.39 + Assertions.assertEquals(0.39, b.costOf("BGLP")); } } diff --git a/src/test/java/com/booleanuk/core/ShopTest.java b/src/test/java/com/booleanuk/core/ShopTest.java deleted file mode 100644 index bf210fdfa..000000000 --- a/src/test/java/com/booleanuk/core/ShopTest.java +++ /dev/null @@ -1,5 +0,0 @@ -package com.booleanuk.core; - -public class ShopTest { - -} From b29af944c502bf4e4ebd4226e208944259540f83 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Sat, 22 Aug 2026 11:06:22 +0200 Subject: [PATCH 04/15] done with core --- src/main/java/com/booleanuk/core/Basket.java | 11 ++++- .../java/com/booleanuk/core/Customer.java | 4 +- src/main/java/com/booleanuk/core/Item.java | 28 ++++++------ .../com/booleanuk/core/item_types/Bagel.java | 21 ++++++--- .../java/com/booleanuk/core/BasketTest.java | 43 ++++++++++++++++--- .../java/com/booleanuk/core/CustomerTest.java | 13 ++++++ .../java/com/booleanuk/core/ItemTest.java | 34 +++++++++++++++ .../java/com/booleanuk/core/MangerTest.java | 15 ++++++- .../booleanuk/core/item_types/BagelTest.java | 16 +++++++ .../booleanuk/core/item_types/CoffeeTest.java | 15 +++++++ .../core/item_types/FillingTest.java | 14 ++++++ 11 files changed, 183 insertions(+), 31 deletions(-) create mode 100644 src/test/java/com/booleanuk/core/item_types/BagelTest.java create mode 100644 src/test/java/com/booleanuk/core/item_types/CoffeeTest.java create mode 100644 src/test/java/com/booleanuk/core/item_types/FillingTest.java diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 6d3acdd91..e46b3ac2c 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -48,7 +48,11 @@ public float costOf(String sku){ return new Item(sku).getPrice(); } - public float totalCost(){ + public int getMaxSize(){ + return Basket.size; + } + + public float getTotalCost(){ float sum = 0f; for (int i = 0; i < this.curSize; i++){ sum += this.arr[i].getPrice(); @@ -59,7 +63,10 @@ public float totalCost(){ @Override public String toString(){ String tmp = ""; - return tmp; + for (int i = 0; i < this.curSize; i++){ + tmp += this.arr[i].getSKU() + " " + this.arr[i].getName() + " " + this.arr[i].getVarient() + "\n"; + } + return tmp.trim(); } } diff --git a/src/main/java/com/booleanuk/core/Customer.java b/src/main/java/com/booleanuk/core/Customer.java index ff9f88848..380ecd08d 100644 --- a/src/main/java/com/booleanuk/core/Customer.java +++ b/src/main/java/com/booleanuk/core/Customer.java @@ -3,8 +3,8 @@ public class Customer { private Basket basket; - public Customer(Basket basket){ - this.basket = basket; + public Customer(){ + this.basket = new Basket(); } public Basket getBasket(){ diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index eace1a9e0..cdb15c041 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -12,20 +12,20 @@ public class Item { public Item(String sku) { this.inventory = new HashMap(); - this.inventory.put("BGLO", new Object[]{0.49, "Bagel", "Onion"}); - this.inventory.put("BGLP", new Object[]{0.39, "Bagel", "Plain"}); - this.inventory.put("BGLE", new Object[]{0.49, "Bagel", "Everything"}); - this.inventory.put("BGLS", new Object[]{0.49, "Bagel", "Sesame"}); - this.inventory.put("COFB", new Object[]{0.99, "Coffee", "Black"}); - this.inventory.put("COFW", new Object[]{1.19, "Coffee", "White"}); - this.inventory.put("COFC", new Object[]{1.29, "Coffee", "Capuccino"}); - this.inventory.put("COFL", new Object[]{1.29, "Coffee", "Latte"}); - this.inventory.put("FILB", new Object[]{0.12, "Filling", "Bacon"}); - this.inventory.put("FILE", new Object[]{0.12, "Filling", "Egg"}); - this.inventory.put("FILC", new Object[]{0.12, "Filling", "Cheese"}); - this.inventory.put("FILX", new Object[]{0.12, "Filling", "Cream Cheese"}); - this.inventory.put("FILS", new Object[]{0.12, "Filling", "Smoked Salmon"}); - this.inventory.put("FILH", new Object[]{0.12, "Filling", "Ham"}); + this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion"}); + this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain"}); + this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything"}); + this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame"}); + this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black"}); + this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White"}); + this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino"}); + this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte"}); + this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon"}); + this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg"}); + this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese"}); + this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese"}); + this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon"}); + this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham"}); try{ this.sku = sku; this.price = (float) this.inventory.get(sku)[0]; diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index 8d9ad1655..13abea5b6 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -5,11 +5,11 @@ import java.util.Arrays; import java.lang.Exception; -import java.util.List; +// import java.util.List; public class Bagel extends Item{ private static final String[] bagelSKU = {"BGLO", "BGLP", "BGLE", "BGLS"}; - private List filling; + // private List filling; public Bagel(String sku) throws Exception{ super(sku); @@ -20,8 +20,19 @@ public Bagel(String sku) throws Exception{ } - public void addFilling(Filling filling){ - this.filling.add(filling); - } // remove this? is does this even work? + // public int getSize(){ + // return this.filling.size(); + // } + + // public void addFilling(Filling filling){ + // this.filling.add(filling); + // } + + // public void removeFIlling(Filling filling){ + // for(int i = 0; i < this.filling.size(); i++){ + // if(this.filling.get(i) == filling) + // this.filling.remove(i); + // } + // } might add latur? } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index cf938004f..67f9f0520 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -1,10 +1,11 @@ package com.booleanuk.core; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; -import com.booleanuk.core.item_types.Bagel; -import com.booleanuk.core.item_types.Coffee; - +import java.lang.Exception; public class BasketTest { @@ -15,19 +16,19 @@ public BasketTest() { } @Test - public void addBagelTest(){ + public void addBagelTest() throws Exception{ b.addBagel("BGLO"); Assertions.assertEquals( "BGLO Onion Bagel", b.toString()); } @Test - public void addFillingTest(){ + public void addFillingTest() throws Exception{ b.addFilling("FILB"); Assertions.assertEquals("FILB Bacon Filling", b.toString()); } @Test - public void addCoffeeTest(){ + public void addCoffeeTest() throws Exception{ b.addCoffee("COFB"); Assertions.assertEquals("COFB Black Coffee", b.toString()); } @@ -35,7 +36,35 @@ public void addCoffeeTest(){ @Test public void getCostTest(){ //"BGLP" Bagel plain 0.39 - Assertions.assertEquals(0.39, b.costOf("BGLP")); + Assertions.assertEquals(0.39, b.costOf("BGLP"), 0.0001); + } + + @Test + public void removeItemTest() throws Exception{ + b.addCoffee("COFB"); + Assertions.assertTrue(b.toString().contains("Black")); + Assertions.assertTrue(b.removeItem("COFB")); + Assertions.assertEquals("", b.toString()); + Assertions.assertFalse(b.removeItem("COFB")); + } + + @Test + public void getTotalCostTest() throws Exception{ + b.addBagel("BGLP"); // 0.39 + b.addCoffee("COFB"); // 0.99 + b.addFilling("FILB"); // 0.12 + Assertions.assertEquals(0.39 + 0.99 + 0.12, b.getTotalCost(), 0.0001); } + @Test + void throwsWhenBasketIsFull() throws Exception { + for (int i = 0; i < b.getMaxSize(); i++){ + b.addBagel("BGLO"); + } + Exception exception = assertThrows(Exception.class, () -> b.addBagel("BGLO")); + assertEquals("Exceeds maximum basket size!", exception.getMessage()); + } + + + } diff --git a/src/test/java/com/booleanuk/core/CustomerTest.java b/src/test/java/com/booleanuk/core/CustomerTest.java index 603478f6f..fe3b0e373 100644 --- a/src/test/java/com/booleanuk/core/CustomerTest.java +++ b/src/test/java/com/booleanuk/core/CustomerTest.java @@ -1,5 +1,18 @@ package com.booleanuk.core; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + public class CustomerTest { + private Customer c; + + public CustomerTest(){ + this.c = new Customer(); + } + + @Test + public void getBasketTest(){ + Assertions.assertInstanceOf(Basket.class, c.getBasket()); + } } diff --git a/src/test/java/com/booleanuk/core/ItemTest.java b/src/test/java/com/booleanuk/core/ItemTest.java index d2687d311..61480e2a8 100644 --- a/src/test/java/com/booleanuk/core/ItemTest.java +++ b/src/test/java/com/booleanuk/core/ItemTest.java @@ -1,5 +1,39 @@ package com.booleanuk.core; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Assertions; + +import org.junit.jupiter.api.Test; + public class ItemTest { + @Test + void throwsWhenSkuDoesNotExistAtAll() { + assertThrows(NullPointerException.class, () -> new Item("ZZZZ")); + } + + @Test + public void getPriceTest(){ + Item i = new Item("COFB"); + Assertions.assertEquals(0.99, i.getPrice(), 0.0001); + } + + @Test + public void getSKUTest(){ + Item i = new Item("COFB"); + Assertions.assertEquals("COFB", i.getSKU()); + } + + @Test + public void getVarientTest(){ + Item i = new Item("COFB"); + Assertions.assertEquals("Coffee", i.getVarient()); + } + + @Test + public void getNameTest(){ + Item i = new Item("COFB"); + Assertions.assertEquals("Black", i.getName()); + } } diff --git a/src/test/java/com/booleanuk/core/MangerTest.java b/src/test/java/com/booleanuk/core/MangerTest.java index f1930f404..89f1986a5 100644 --- a/src/test/java/com/booleanuk/core/MangerTest.java +++ b/src/test/java/com/booleanuk/core/MangerTest.java @@ -1,5 +1,18 @@ package com.booleanuk.core; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + public class MangerTest { - + private Manger m; + + public MangerTest(){ + this.m = new Manger(); + } + + @Test + public void changeBasketSizeTest(){ + Manger.changeBasketSize(15); + Assertions.assertEquals(15, m.getMaxSize()); + } } diff --git a/src/test/java/com/booleanuk/core/item_types/BagelTest.java b/src/test/java/com/booleanuk/core/item_types/BagelTest.java new file mode 100644 index 000000000..fb3701ad7 --- /dev/null +++ b/src/test/java/com/booleanuk/core/item_types/BagelTest.java @@ -0,0 +1,16 @@ +package com.booleanuk.core.item_types; + + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +public class BagelTest{ + + @Test + void throwsWhenSkuIsValidButNotABagel() { + Exception exception = assertThrows(Exception.class, () -> new Bagel("COFB")); + assertEquals("Must be a valid bagel SKU!", exception.getMessage()); + } + +} diff --git a/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java b/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java new file mode 100644 index 000000000..4a3826ec2 --- /dev/null +++ b/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java @@ -0,0 +1,15 @@ +package com.booleanuk.core.item_types; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +class CoffeeTest { + + @Test + void throwsWhenSkuIsValidButNotACoffee() { + Exception exception = assertThrows(Exception.class, () -> new Coffee("BGLO")); + assertEquals("Must be a valid coffee SKU!", exception.getMessage()); + } + +} \ No newline at end of file diff --git a/src/test/java/com/booleanuk/core/item_types/FillingTest.java b/src/test/java/com/booleanuk/core/item_types/FillingTest.java new file mode 100644 index 000000000..2f6396cd0 --- /dev/null +++ b/src/test/java/com/booleanuk/core/item_types/FillingTest.java @@ -0,0 +1,14 @@ +package com.booleanuk.core.item_types; + +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + +public class FillingTest { + + @Test + void throwsWhenSkuIsValidButNotAFilling() { + Exception exception = assertThrows(Exception.class, () -> new Filling("BGLO")); + assertEquals("Must be a valid filling SKU!", exception.getMessage()); + } +} From 2e4d5873bd76a9433cabe841e030911df067fa2c Mon Sep 17 00:00:00 2001 From: David Bylund Date: Sat, 22 Aug 2026 12:55:21 +0200 Subject: [PATCH 05/15] discount added --- src/main/java/com/booleanuk/core/Basket.java | 33 +++++++++++++- src/main/java/com/booleanuk/core/Item.java | 44 +++++++++++++------ .../java/com/booleanuk/core/BasketTest.java | 31 ++++++++++--- .../java/com/booleanuk/core/MangerTest.java | 4 +- 4 files changed, 87 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index e46b3ac2c..d2b27ce4a 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,15 +1,19 @@ package com.booleanuk.core; +import java.util.HashMap; +import java.util.Map; + import com.booleanuk.core.item_types.*; public class Basket { - protected static int size = 10; + protected static int size = 30; private int curSize; private Item[] arr; public Basket(){ this.arr = new Item[Basket.size]; this.curSize = 0; + } public void addBagel(String sku) throws Exception{ @@ -35,8 +39,16 @@ public void addFilling(String sku) throws Exception{ public boolean removeItem(String sku){ for (int i = 0; i < this.curSize; i++){ + if(this.arr[i].getSKU() == sku){ - this.arr[i] = null; + for (int j = i; j < this.curSize; j++){ + if (j+1 >= this.curSize){ + this.arr[j] = null; + break; + } + this.arr[j] = this.arr[j+1]; + } + this.arr[i] = this.arr[i+1]; this.curSize--; return true; } @@ -54,7 +66,24 @@ public int getMaxSize(){ public float getTotalCost(){ float sum = 0f; + Map skuDiscounts = new HashMap(); + for (int i = 0; i < this.curSize; i++){ + String sku = this.arr[i].getSKU(); + int discountCondition = this.arr[i].getDiscountCondition(); + boolean containsSKU = skuDiscounts.containsKey(sku); + System.err.println(discountCondition); + + if(discountCondition != 0 && !containsSKU){ + skuDiscounts.put(sku, 1); + } + if(containsSKU){ + skuDiscounts.replace(sku, skuDiscounts.get(sku) + 1); + } + if(containsSKU && skuDiscounts.get(sku) >= discountCondition){ + skuDiscounts.replace(sku, 0); + sum += this.arr[i].getDiscount(); + } sum += this.arr[i].getPrice(); } return sum; diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index cdb15c041..321c3531b 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -8,29 +8,38 @@ public class Item { protected String sku; protected String varient; protected String name; + protected int discountCondition; + protected float discount; protected Map inventory; public Item(String sku) { this.inventory = new HashMap(); - this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion"}); - this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain"}); - this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything"}); - this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame"}); - this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black"}); - this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White"}); - this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino"}); - this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte"}); - this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon"}); - this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg"}); - this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese"}); - this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese"}); - this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon"}); - this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham"}); + this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion", 6, -0.45f}); + this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain", 12, -0.69f}); + this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything", 6, -0.45f}); + this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame", null}); + this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black", null}); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White", null}); + this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino", null}); + this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte", null}); + this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon", null}); + this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg", null}); + this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese", null}); + this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese", null}); + this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon", null}); + this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham", null}); try{ this.sku = sku; this.price = (float) this.inventory.get(sku)[0]; this.varient = (String) this.inventory.get(sku)[1]; this.name = (String) this.inventory.get(sku)[2]; + if(this.inventory.get(sku)[3] != null){ + this.discountCondition = (int) this.inventory.get(sku)[3]; + this.discount = (float) this.inventory.get(sku)[4]; + }else{ + this.discountCondition = 0; + this.discount = 0f; + } } catch (Exception e){ System.out.println("Not a valid SKU"); @@ -55,5 +64,12 @@ public String getName(){ return this.name; } + public int getDiscountCondition(){ + return this.discountCondition; + } + + public float getDiscount(){ + return this.discount; + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 67f9f0520..ce287d81b 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -48,13 +48,13 @@ public void removeItemTest() throws Exception{ Assertions.assertFalse(b.removeItem("COFB")); } - @Test - public void getTotalCostTest() throws Exception{ - b.addBagel("BGLP"); // 0.39 - b.addCoffee("COFB"); // 0.99 - b.addFilling("FILB"); // 0.12 - Assertions.assertEquals(0.39 + 0.99 + 0.12, b.getTotalCost(), 0.0001); - } + // @Test + // public void getTotalCostTest() throws Exception{ + // b.addBagel("BGLP"); // 0.39 + // b.addCoffee("COFB"); // 0.99 + // b.addFilling("FILB"); // 0.12 + // Assertions.assertEquals(0.39 + 0.99 + 0.12, b.getTotalCost(), 0.0001); + // } @Test void throwsWhenBasketIsFull() throws Exception { @@ -65,6 +65,23 @@ void throwsWhenBasketIsFull() throws Exception { assertEquals("Exceeds maximum basket size!", exception.getMessage()); } + @Test + public void discountAppliedTest() throws Exception{ + for(int i = 0; i < 16; i++){ + b.addBagel("BGLP"); + } + Assertions.assertEquals(5.55, b.getTotalCost(), 0.0001); + for(int i = 0; i < 6; i++){ + if(i < 2) + b.addBagel("BGLO"); + if(i < 3) + b.addCoffee("COFB"); + if(i < 4) + b.removeItem("BGLP"); + b.addBagel("BGLE"); + } + Assertions.assertEquals(10.43, b.getTotalCost(), 0.0001); + } } diff --git a/src/test/java/com/booleanuk/core/MangerTest.java b/src/test/java/com/booleanuk/core/MangerTest.java index 89f1986a5..36fe5642a 100644 --- a/src/test/java/com/booleanuk/core/MangerTest.java +++ b/src/test/java/com/booleanuk/core/MangerTest.java @@ -12,7 +12,7 @@ public MangerTest(){ @Test public void changeBasketSizeTest(){ - Manger.changeBasketSize(15); - Assertions.assertEquals(15, m.getMaxSize()); + Manger.changeBasketSize(35); + Assertions.assertEquals(35, m.getMaxSize()); } } From 2cbecd38fa01ac3b3b42705b8461efd7a308caf6 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Sat, 22 Aug 2026 15:57:06 +0200 Subject: [PATCH 06/15] made inventory its own thing and did the extentions 1-3 --- src/main/java/com/booleanuk/core/Basket.java | 74 ++++++++++++++++--- .../java/com/booleanuk/core/Inventory.java | 56 ++++++++++++++ src/main/java/com/booleanuk/core/Item.java | 36 ++------- .../java/com/booleanuk/core/BasketTest.java | 36 ++++++++- 4 files changed, 160 insertions(+), 42 deletions(-) create mode 100644 src/main/java/com/booleanuk/core/Inventory.java diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index d2b27ce4a..daf7c83b6 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,5 +1,7 @@ package com.booleanuk.core; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; import java.util.HashMap; import java.util.Map; @@ -9,10 +11,12 @@ public class Basket { protected static int size = 30; private int curSize; private Item[] arr; + private Inventory inventory; public Basket(){ this.arr = new Item[Basket.size]; this.curSize = 0; + this.inventory = new Inventory(); } @@ -57,35 +61,81 @@ public boolean removeItem(String sku){ } public float costOf(String sku){ - return new Item(sku).getPrice(); + return inventory.getPrice(sku); } public int getMaxSize(){ return Basket.size; } - public float getTotalCost(){ + private record Pair(int count, float discount){} + + private String padding(int max, String str){ + return " ".repeat((max/2) - (str.length()/2)) + str; + } + + private String paddTwo(int max, String str1, String str2){ + int padd = max- str1.length() - str2.length(); + return str1 +" ".repeat(padd) + str2; + } + + private String buildPrint(Map skuDiscounts, float sum){ + String print =""; + String bobString = "~~~ Bob's Bagels ~~~"; + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"); + String dateTime = LocalDateTime.now().format(formatter); + int maxStringSize = 28; + String div = "-".repeat(maxStringSize) + "\n"; + + print += padding(maxStringSize, bobString) + "\n" + padding(maxStringSize, dateTime) + "\n" + div; + String tmp = ""; + float totDisc = 0f; + for (Map.Entry entry : skuDiscounts.entrySet()) { + String sku = entry.getKey(); + Pair pair = entry.getValue(); + String nameVarient = this.inventory.getName(sku) + " " + this.inventory.getVarient(sku); + String price = "£" + String.format("%.2f", this.inventory.getPrice(sku)*pair.count); + String amountPrice = paddTwo(10, ""+ pair.count, price); + tmp += paddTwo(maxStringSize, nameVarient, amountPrice)+ "\n"; + if(pair.discount != 0){ + tmp += paddTwo(maxStringSize, "", "(-£" + Math.abs(pair.discount) + ")") + "\n"; + totDisc += pair.discount; + } + + } + print += tmp; + print += div + paddTwo(maxStringSize, "Total", "£"+ String.format("%.2f", sum))+ "\n"; + print += "\n" + padding(maxStringSize, "You saved a total of " + "£"+ String.format("%.2f", Math.abs(totDisc))) + "\n"; + print += "\n" + padding(maxStringSize, "Thank you for buying!"); + return print; + } + + public float getTotalCost(){ float sum = 0f; - Map skuDiscounts = new HashMap(); + Map skuDiscounts = new HashMap(); for (int i = 0; i < this.curSize; i++){ String sku = this.arr[i].getSKU(); int discountCondition = this.arr[i].getDiscountCondition(); boolean containsSKU = skuDiscounts.containsKey(sku); - System.err.println(discountCondition); + // System.err.println(discountCondition); - if(discountCondition != 0 && !containsSKU){ - skuDiscounts.put(sku, 1); - } - if(containsSKU){ - skuDiscounts.replace(sku, skuDiscounts.get(sku) + 1); + if(!containsSKU){ + skuDiscounts.put(sku, new Pair(1, 0f)); + }else{ + skuDiscounts.computeIfPresent(sku, (s, p) -> new Pair(p.count() + 1, p.discount())); + // System.err.println(skuDiscounts.get(sku).count); } - if(containsSKU && skuDiscounts.get(sku) >= discountCondition){ - skuDiscounts.replace(sku, 0); - sum += this.arr[i].getDiscount(); + if(containsSKU && discountCondition != 0 && skuDiscounts.get(sku).count % discountCondition == 0){ + float discount = this.arr[i].getDiscount(); + skuDiscounts.computeIfPresent(sku, (s, p) -> new Pair(p.count(), p.discount() + discount)); + sum += discount; } sum += this.arr[i].getPrice(); } + + System.out.println(this.buildPrint(skuDiscounts, sum)); + // System.err.println(this.buildPrint(skuDiscounts, sum)); return sum; } 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..adb5549ae --- /dev/null +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -0,0 +1,56 @@ +package com.booleanuk.core; + +import java.util.HashMap; +import java.util.Map; + +public class Inventory { + private Map inventory; + + public Inventory(){ + this.inventory = new HashMap(); + this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion", 6, -0.45f}); + this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain", 12, -0.69f}); + this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything", 6, -0.45f}); + this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame", null}); + this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black", null}); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White", null}); + this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino", null}); + this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte", null}); + this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon", null}); + this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg", null}); + this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese", null}); + this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese", null}); + this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon", null}); + this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham", null}); + } + + public float getPrice(String sku){ + return (float) this.inventory.get(sku)[0]; + } + + public String getVarient(String sku){ + return (String) this.inventory.get(sku)[1]; + } + + public String getName(String sku){ + return (String) this.inventory.get(sku)[2]; + } + + public int getDiscountCondition(String sku){ + Object dsc = this.inventory.get(sku)[3]; + if(dsc != null){ + return (int) dsc; + }else{ + return 0; + } + } + + public float getDiscount(String sku){ + Object dsc = this.inventory.get(sku)[3]; + if(dsc != null){ + return (float) this.inventory.get(sku)[4]; + }else{ + return 0f; + } + } +} diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index 321c3531b..468b811a0 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -1,8 +1,5 @@ package com.booleanuk.core; -import java.util.HashMap; -import java.util.Map; - public class Item { protected float price; protected String sku; @@ -10,36 +7,17 @@ public class Item { protected String name; protected int discountCondition; protected float discount; - protected Map inventory; + protected Inventory inventory; public Item(String sku) { - this.inventory = new HashMap(); - this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion", 6, -0.45f}); - this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain", 12, -0.69f}); - this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything", 6, -0.45f}); - this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame", null}); - this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black", null}); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... - this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White", null}); - this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino", null}); - this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte", null}); - this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon", null}); - this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg", null}); - this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese", null}); - this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese", null}); - this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon", null}); - this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham", null}); + this.inventory = new Inventory(); try{ this.sku = sku; - this.price = (float) this.inventory.get(sku)[0]; - this.varient = (String) this.inventory.get(sku)[1]; - this.name = (String) this.inventory.get(sku)[2]; - if(this.inventory.get(sku)[3] != null){ - this.discountCondition = (int) this.inventory.get(sku)[3]; - this.discount = (float) this.inventory.get(sku)[4]; - }else{ - this.discountCondition = 0; - this.discount = 0f; - } + this.price = this.inventory.getPrice(sku); + this.varient = this.inventory.getVarient(sku); + this.name = this.inventory.getName(sku); + this.discountCondition = this.inventory.getDiscountCondition(sku); + this.discount = this.inventory.getDiscount(sku); } catch (Exception e){ System.out.println("Not a valid SKU"); diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index ce287d81b..df05ad534 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -2,14 +2,21 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import java.lang.Exception; public class BasketTest { - public Basket b; + private Basket b; + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; public BasketTest() { this.b = new Basket(); @@ -83,5 +90,32 @@ public void discountAppliedTest() throws Exception{ Assertions.assertEquals(10.43, b.getTotalCost(), 0.0001); } + @BeforeEach + void setUp() { + System.setOut(new PrintStream(outContent)); + } + @AfterEach + void tearDown() { + System.setOut(originalOut); + } + + @Test + void printsExpectedReceipt() throws Exception { + for(int i = 0; i < 16; i++){ + b.addBagel("BGLP"); + } + b.getTotalCost(); + String printed = outContent.toString(); + Assertions.assertTrue(printed.contains("~~~ Bob's Bagels ~~~")); + Assertions.assertTrue(printed.contains("----------------------------")); + Assertions.assertTrue(printed.contains("Plain Bagel")); + Assertions.assertTrue(printed.contains("16")); + Assertions.assertTrue(printed.contains("£6.24")); + Assertions.assertTrue(printed.contains("(-£0.69)")); + Assertions.assertTrue(printed.contains("Total")); + Assertions.assertTrue(printed.contains("£5.55")); + Assertions.assertTrue(printed.contains("You saved a total of £0.69")); + Assertions.assertTrue(printed.contains("Thank you for buying!")); + } } From 226e5d4112047e366a44f5000722cd93587f2fd1 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Mon, 24 Aug 2026 09:48:17 +0200 Subject: [PATCH 07/15] changed bagel to allow fillings and made sure addBagel now handles that --- src/main/java/com/booleanuk/core/Basket.java | 8 +-- .../com/booleanuk/core/item_types/Bagel.java | 30 +++++----- .../java/com/booleanuk/core/BasketTest.java | 57 +++++++++++++------ .../booleanuk/core/item_types/BagelTest.java | 22 +++++++ 4 files changed, 82 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index daf7c83b6..248fa2c2c 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -20,11 +20,11 @@ public Basket(){ } - public void addBagel(String sku) throws Exception{ - if(curSize >= size) + public void addBagel(Bagel bag) throws Exception{ + if(curSize + bag.getSize() -1 >= size) throw new Exception("Exceeds maximum basket size!"); - this.arr[this.curSize] = new Bagel(sku); - this.curSize++; + this.arr[this.curSize] = bag; + this.curSize += bag.getSize(); } public void addCoffee(String sku) throws Exception{ diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index 13abea5b6..dbefe7a48 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -3,13 +3,14 @@ import com.booleanuk.core.Item; +import java.util.ArrayList; import java.util.Arrays; import java.lang.Exception; -// import java.util.List; +import java.util.List; public class Bagel extends Item{ private static final String[] bagelSKU = {"BGLO", "BGLP", "BGLE", "BGLS"}; - // private List filling; + private List filling; public Bagel(String sku) throws Exception{ super(sku); @@ -17,22 +18,23 @@ public Bagel(String sku) throws Exception{ if(!contains){ throw new Exception("Must be a valid bagel SKU!"); } + this.filling = new ArrayList(); } - // public int getSize(){ - // return this.filling.size(); - // } + public int getSize(){ + return this.filling.size() + 1; //+1 for the bagel + } - // public void addFilling(Filling filling){ - // this.filling.add(filling); - // } + public void addFilling(Filling filling){ + this.filling.add(filling); + } - // public void removeFIlling(Filling filling){ - // for(int i = 0; i < this.filling.size(); i++){ - // if(this.filling.get(i) == filling) - // this.filling.remove(i); - // } - // } might add latur? + public void removeFIlling(String sku){ + for(int i = 0; i < this.filling.size(); i++){ + if(this.filling.get(i).getSKU() == sku) + this.filling.remove(i); + } + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index df05ad534..13611cbfc 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -8,6 +8,9 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import com.booleanuk.core.item_types.Bagel; +import com.booleanuk.core.item_types.Filling; + import java.io.ByteArrayOutputStream; import java.io.PrintStream; import java.lang.Exception; @@ -24,7 +27,8 @@ public BasketTest() { @Test public void addBagelTest() throws Exception{ - b.addBagel("BGLO"); + Bagel bag = new Bagel("BGLO"); + b.addBagel(bag); Assertions.assertEquals( "BGLO Onion Bagel", b.toString()); } @@ -55,37 +59,55 @@ public void removeItemTest() throws Exception{ Assertions.assertFalse(b.removeItem("COFB")); } - // @Test - // public void getTotalCostTest() throws Exception{ - // b.addBagel("BGLP"); // 0.39 - // b.addCoffee("COFB"); // 0.99 - // b.addFilling("FILB"); // 0.12 - // Assertions.assertEquals(0.39 + 0.99 + 0.12, b.getTotalCost(), 0.0001); - // } + @Test + public void getTotalCostTest() throws Exception{ + Bagel bag = new Bagel("BGLP"); + b.addBagel(bag); // 0.39 + b.addCoffee("COFB"); // 0.99 + b.addFilling("FILB"); // 0.12 + Assertions.assertEquals(0.39 + 0.99 + 0.12, b.getTotalCost(), 0.0001); + } @Test - void throwsWhenBasketIsFull() throws Exception { + void throwsWhenBasketIsFull() throws Exception { + Bagel bag = new Bagel("BGLO"); + for (int i = 0; i < b.getMaxSize(); i++){ - b.addBagel("BGLO"); + b.addBagel(bag); } - Exception exception = assertThrows(Exception.class, () -> b.addBagel("BGLO")); - assertEquals("Exceeds maximum basket size!", exception.getMessage()); - } + Exception exception = assertThrows(Exception.class, () -> b.addBagel(bag)); + assertEquals("Exceeds maximum basket size!", exception.getMessage()); + } + + @Test + void throwsWhenBasketIsFull_GigaBagel() throws Exception{ + Bagel bag = new Bagel("BGLO"); + for (int i = 0; i < b.getMaxSize(); i++){ + bag.addFilling(new Filling("FILB")); + } + Exception exception = assertThrows(Exception.class, () -> b.addBagel(bag)); + assertEquals("Exceeds maximum basket size!", exception.getMessage()); + } @Test public void discountAppliedTest() throws Exception{ + Bagel bagO = new Bagel("BGLO"); + Bagel bagP = new Bagel("BGLP"); + Bagel bagE = new Bagel("BGLE"); + + for(int i = 0; i < 16; i++){ - b.addBagel("BGLP"); + b.addBagel(bagP); } Assertions.assertEquals(5.55, b.getTotalCost(), 0.0001); for(int i = 0; i < 6; i++){ if(i < 2) - b.addBagel("BGLO"); + b.addBagel(bagO); if(i < 3) b.addCoffee("COFB"); if(i < 4) b.removeItem("BGLP"); - b.addBagel("BGLE"); + b.addBagel(bagE); } Assertions.assertEquals(10.43, b.getTotalCost(), 0.0001); } @@ -102,8 +124,9 @@ void tearDown() { @Test void printsExpectedReceipt() throws Exception { + Bagel bagP = new Bagel("BGLP"); for(int i = 0; i < 16; i++){ - b.addBagel("BGLP"); + b.addBagel(bagP); } b.getTotalCost(); String printed = outContent.toString(); diff --git a/src/test/java/com/booleanuk/core/item_types/BagelTest.java b/src/test/java/com/booleanuk/core/item_types/BagelTest.java index fb3701ad7..2c071c8c3 100644 --- a/src/test/java/com/booleanuk/core/item_types/BagelTest.java +++ b/src/test/java/com/booleanuk/core/item_types/BagelTest.java @@ -2,6 +2,9 @@ import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Assertions; + import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; @@ -12,5 +15,24 @@ void throwsWhenSkuIsValidButNotABagel() { Exception exception = assertThrows(Exception.class, () -> new Bagel("COFB")); assertEquals("Must be a valid bagel SKU!", exception.getMessage()); } + + @Test + public void getSize_addFillingTest() throws Exception{ + Bagel bag = new Bagel("BGLO"); + Assertions.assertEquals(1, bag.getSize()); + Filling fill = new Filling("FILB"); + bag.addFilling(fill); + Assertions.assertEquals(2, bag.getSize()); + } + + @Test + public void removeFillingTest() throws Exception{ + Bagel bag = new Bagel("BGLO"); + Filling fill = new Filling("FILB"); + bag.addFilling(fill); + Assertions.assertEquals(2, bag.getSize()); + bag.removeFIlling("FILB"); + Assertions.assertEquals(1, bag.getSize()); + } } From 3806f42caed4a446435d8aa417f0e713f769137c Mon Sep 17 00:00:00 2001 From: David Bylund Date: Mon, 24 Aug 2026 09:53:33 +0200 Subject: [PATCH 08/15] changed bagel again slightly --- src/main/java/com/booleanuk/core/Basket.java | 4 ++-- src/main/java/com/booleanuk/core/item_types/Bagel.java | 2 +- .../java/com/booleanuk/core/item_types/BagelTest.java | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 248fa2c2c..53a8439e5 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -21,10 +21,10 @@ public Basket(){ } public void addBagel(Bagel bag) throws Exception{ - if(curSize + bag.getSize() -1 >= size) + if(curSize + bag.getSize()>= size) throw new Exception("Exceeds maximum basket size!"); this.arr[this.curSize] = bag; - this.curSize += bag.getSize(); + this.curSize += bag.getSize() + 1; // + 1 for bagel } public void addCoffee(String sku) throws Exception{ diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index dbefe7a48..b5ff6d9fa 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -23,7 +23,7 @@ public Bagel(String sku) throws Exception{ } public int getSize(){ - return this.filling.size() + 1; //+1 for the bagel + return this.filling.size(); } public void addFilling(Filling filling){ diff --git a/src/test/java/com/booleanuk/core/item_types/BagelTest.java b/src/test/java/com/booleanuk/core/item_types/BagelTest.java index 2c071c8c3..3c1b757a5 100644 --- a/src/test/java/com/booleanuk/core/item_types/BagelTest.java +++ b/src/test/java/com/booleanuk/core/item_types/BagelTest.java @@ -19,10 +19,10 @@ void throwsWhenSkuIsValidButNotABagel() { @Test public void getSize_addFillingTest() throws Exception{ Bagel bag = new Bagel("BGLO"); - Assertions.assertEquals(1, bag.getSize()); + Assertions.assertEquals(0, bag.getSize()); Filling fill = new Filling("FILB"); bag.addFilling(fill); - Assertions.assertEquals(2, bag.getSize()); + Assertions.assertEquals(1, bag.getSize()); } @Test @@ -30,9 +30,9 @@ public void removeFillingTest() throws Exception{ Bagel bag = new Bagel("BGLO"); Filling fill = new Filling("FILB"); bag.addFilling(fill); - Assertions.assertEquals(2, bag.getSize()); - bag.removeFIlling("FILB"); Assertions.assertEquals(1, bag.getSize()); + bag.removeFIlling("FILB"); + Assertions.assertEquals(0, bag.getSize()); } } From 4654d9b472fb91d1ffe439a945d2d6538f490ac1 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Mon, 24 Aug 2026 10:07:10 +0200 Subject: [PATCH 09/15] furter refinement of bagel, TODO (if time) make sure the build print works with bagels with fillings! --- .../java/com/booleanuk/core/item_types/Bagel.java | 11 +++++++++-- src/test/java/com/booleanuk/core/BasketTest.java | 5 ++--- .../com/booleanuk/core/item_types/BagelTest.java | 13 +++++++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index b5ff6d9fa..9a619f620 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -1,6 +1,5 @@ package com.booleanuk.core.item_types; - import com.booleanuk.core.Item; import java.util.ArrayList; @@ -11,6 +10,7 @@ public class Bagel extends Item{ private static final String[] bagelSKU = {"BGLO", "BGLP", "BGLE", "BGLS"}; private List filling; + private float totalPrice; public Bagel(String sku) throws Exception{ super(sku); @@ -26,8 +26,10 @@ public int getSize(){ return this.filling.size(); } - public void addFilling(Filling filling){ + public void addFilling(String sku) throws Exception{ + Filling filling = new Filling(sku); this.filling.add(filling); + this.totalPrice += filling.getPrice(); } public void removeFIlling(String sku){ @@ -36,5 +38,10 @@ public void removeFIlling(String sku){ this.filling.remove(i); } } + + @Override + public float getPrice(){ + return this.price + this.totalPrice; + } } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 13611cbfc..9caa5457e 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -9,7 +9,6 @@ import org.junit.jupiter.api.Test; import com.booleanuk.core.item_types.Bagel; -import com.booleanuk.core.item_types.Filling; import java.io.ByteArrayOutputStream; import java.io.PrintStream; @@ -64,7 +63,7 @@ public void getTotalCostTest() throws Exception{ Bagel bag = new Bagel("BGLP"); b.addBagel(bag); // 0.39 b.addCoffee("COFB"); // 0.99 - b.addFilling("FILB"); // 0.12 + bag.addFilling("FILB"); // 0.12 Assertions.assertEquals(0.39 + 0.99 + 0.12, b.getTotalCost(), 0.0001); } @@ -83,7 +82,7 @@ void throwsWhenBasketIsFull() throws Exception { void throwsWhenBasketIsFull_GigaBagel() throws Exception{ Bagel bag = new Bagel("BGLO"); for (int i = 0; i < b.getMaxSize(); i++){ - bag.addFilling(new Filling("FILB")); + bag.addFilling("FILB"); } Exception exception = assertThrows(Exception.class, () -> b.addBagel(bag)); assertEquals("Exceeds maximum basket size!", exception.getMessage()); diff --git a/src/test/java/com/booleanuk/core/item_types/BagelTest.java b/src/test/java/com/booleanuk/core/item_types/BagelTest.java index 3c1b757a5..2f1c0c9f6 100644 --- a/src/test/java/com/booleanuk/core/item_types/BagelTest.java +++ b/src/test/java/com/booleanuk/core/item_types/BagelTest.java @@ -20,19 +20,24 @@ void throwsWhenSkuIsValidButNotABagel() { public void getSize_addFillingTest() throws Exception{ Bagel bag = new Bagel("BGLO"); Assertions.assertEquals(0, bag.getSize()); - Filling fill = new Filling("FILB"); - bag.addFilling(fill); + bag.addFilling("FILB"); Assertions.assertEquals(1, bag.getSize()); } @Test public void removeFillingTest() throws Exception{ Bagel bag = new Bagel("BGLO"); - Filling fill = new Filling("FILB"); - bag.addFilling(fill); + bag.addFilling("FILB"); Assertions.assertEquals(1, bag.getSize()); bag.removeFIlling("FILB"); Assertions.assertEquals(0, bag.getSize()); } + + @Test + public void getPriceTest() throws Exception{ + Bagel bag = new Bagel("BGLO"); //0.49 + bag.addFilling("FILB"); // 0.12 + Assertions.assertEquals(0.61, bag.getPrice(), 0.0001); + } } From c63ae8b10fddfb4b46a07105ae6b4e9f5ddb64d2 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Mon, 24 Aug 2026 13:25:42 +0200 Subject: [PATCH 10/15] day4 dependancy injection added to bobs bagels --- model.md | 61 ++++++++---- src/main/java/com/booleanuk/core/Basket.java | 24 +++-- .../java/com/booleanuk/core/Customer.java | 4 +- .../java/com/booleanuk/core/Inventory.java | 52 ++++++++--- src/main/java/com/booleanuk/core/Item.java | 16 ++-- src/main/java/com/booleanuk/core/Manger.java | 8 ++ src/main/java/com/booleanuk/core/Stock.java | 10 ++ .../com/booleanuk/core/item_types/Bagel.java | 11 +-- .../com/booleanuk/core/item_types/Coffee.java | 9 +- .../booleanuk/core/item_types/Filling.java | 9 +- .../java/com/booleanuk/core/BasketTest.java | 34 +++++-- .../java/com/booleanuk/core/CustomerTest.java | 19 +++- .../java/com/booleanuk/core/ItemTest.java | 31 ++++++- .../java/com/booleanuk/core/MangerTest.java | 18 +++- .../java/com/booleanuk/core/StockTest.java | 92 +++++++++++++++++++ .../booleanuk/core/item_types/BagelTest.java | 31 ++++++- .../booleanuk/core/item_types/CoffeeTest.java | 25 ++++- .../core/item_types/FillingTest.java | 25 ++++- 18 files changed, 391 insertions(+), 88 deletions(-) create mode 100644 src/main/java/com/booleanuk/core/Stock.java create mode 100644 src/test/java/com/booleanuk/core/StockTest.java diff --git a/model.md b/model.md index 3e446c169..fc2d5d595 100644 --- a/model.md +++ b/model.md @@ -1,20 +1,36 @@ ```mermaid classDiagram - class Shop might be added later { - -Stock map + class Inventory { + -Stock map - +isInStock() boolean - +sold(sku) void + +getPrice() + +getVarient() + +getName() + +getDiscountCondition() + +getDiscount() } + class Stock { + hasItem(String sku) + getPrice(String sku) + getVarient(String sku) + getName(String sku) + getDiscountCondition(String sku) + etDiscount(String sku) + } + class Basket { -int size -Array items - - +add(item.type.type) boolean make seperate adds? - +getCost(item.type.type) float - +delete() boolean + -Inventory inventory + + +Basket(Inventory) + +addBagel(Bagel) void + +addFilling(sku) void + +addCoffee(sku) void + +costOf(sku) float + +removeItem(sku) boolean +totalCost() +getBasket() String (what the basket has) @@ -27,18 +43,27 @@ classDiagram } class Item { - -sku -price + -sku -varient + -name + -discountCondition + -discount + -inventory - +get() - +set() + +getters() } class Bagel{ -type - -could have filling, but this can lead to amount of items being over amount in capacity unless you dont count fillings as items... + -fillings + -totalPrice + + +addFilling() void + +removeFilling() void + +getPrice() float + +getSize() int } @@ -53,11 +78,9 @@ classDiagram } class Customer{ - -name? - -email? - -password? + -basket - +getBasket()? + +getBasket() } @@ -71,6 +94,12 @@ classDiagram Bagel o-- Item: extends Coffee o-- Item: extends Filling o-- Item: extends + Bagel o-- Filling: has + + Item o-- Stock: uses + Basket o-- Stock: uses + + Inventory o-- Stock: implements ``` diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 53a8439e5..8d1f5aa96 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -11,17 +11,17 @@ public class Basket { protected static int size = 30; private int curSize; private Item[] arr; - private Inventory inventory; + private final Stock stock; - public Basket(){ + public Basket(Stock stock){ this.arr = new Item[Basket.size]; this.curSize = 0; - this.inventory = new Inventory(); + this.stock = stock; } public void addBagel(Bagel bag) throws Exception{ - if(curSize + bag.getSize()>= size) + if(curSize + bag.getSize() >= size) throw new Exception("Exceeds maximum basket size!"); this.arr[this.curSize] = bag; this.curSize += bag.getSize() + 1; // + 1 for bagel @@ -30,14 +30,20 @@ public void addBagel(Bagel bag) throws Exception{ public void addCoffee(String sku) throws Exception{ if(curSize >= size) throw new Exception("Exceeds maximum basket size!"); - this.arr[this.curSize] = new Coffee(sku); + if(!this.stock.hasItem(sku)){ + throw new Exception("Not a valid SKU"); + } + this.arr[this.curSize] = new Coffee(sku, this.stock); this.curSize++; } public void addFilling(String sku) throws Exception{ if(curSize >= size) throw new Exception("Exceeds maximum basket size!"); - this.arr[this.curSize] = new Filling(sku); + if(!this.stock.hasItem(sku)){ + throw new Exception("Not a valid SKU"); + } + this.arr[this.curSize] = new Filling(sku, this.stock); this.curSize++; } @@ -61,7 +67,7 @@ public boolean removeItem(String sku){ } public float costOf(String sku){ - return inventory.getPrice(sku); + return stock.getPrice(sku); } public int getMaxSize(){ @@ -93,8 +99,8 @@ private String buildPrint(Map skuDiscounts, float sum){ for (Map.Entry entry : skuDiscounts.entrySet()) { String sku = entry.getKey(); Pair pair = entry.getValue(); - String nameVarient = this.inventory.getName(sku) + " " + this.inventory.getVarient(sku); - String price = "£" + String.format("%.2f", this.inventory.getPrice(sku)*pair.count); + String nameVarient = this.stock.getName(sku) + " " + this.stock.getVarient(sku); + String price = "£" + String.format("%.2f", this.stock.getPrice(sku)*pair.count); String amountPrice = paddTwo(10, ""+ pair.count, price); tmp += paddTwo(maxStringSize, nameVarient, amountPrice)+ "\n"; if(pair.discount != 0){ diff --git a/src/main/java/com/booleanuk/core/Customer.java b/src/main/java/com/booleanuk/core/Customer.java index 380ecd08d..dc2a7d1a7 100644 --- a/src/main/java/com/booleanuk/core/Customer.java +++ b/src/main/java/com/booleanuk/core/Customer.java @@ -3,8 +3,8 @@ public class Customer { private Basket basket; - public Customer(){ - this.basket = new Basket(); + public Customer(Inventory inventory){ + this.basket = new Basket(inventory); } public Basket getBasket(){ diff --git a/src/main/java/com/booleanuk/core/Inventory.java b/src/main/java/com/booleanuk/core/Inventory.java index adb5549ae..553491153 100644 --- a/src/main/java/com/booleanuk/core/Inventory.java +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -3,39 +3,43 @@ import java.util.HashMap; import java.util.Map; -public class Inventory { +public class Inventory implements Stock{ private Map inventory; public Inventory(){ this.inventory = new HashMap(); - this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion", 6, -0.45f}); - this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain", 12, -0.69f}); - this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything", 6, -0.45f}); - this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame", null}); - this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black", null}); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... - this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White", null}); - this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino", null}); - this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte", null}); - this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon", null}); - this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg", null}); - this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese", null}); - this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese", null}); - this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon", null}); - this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham", null}); + // this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion", 6, -0.45f}); + // this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain", 12, -0.69f}); + // this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything", 6, -0.45f}); + // this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame", null}); + // this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black", null}); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + // this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White", null}); + // this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino", null}); + // this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte", null}); + // this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon", null}); + // this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg", null}); + // this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese", null}); + // this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese", null}); + // this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon", null}); + // this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham", null}); } + @Override public float getPrice(String sku){ return (float) this.inventory.get(sku)[0]; } + @Override public String getVarient(String sku){ return (String) this.inventory.get(sku)[1]; } + @Override public String getName(String sku){ return (String) this.inventory.get(sku)[2]; } + @Override public int getDiscountCondition(String sku){ Object dsc = this.inventory.get(sku)[3]; if(dsc != null){ @@ -45,6 +49,7 @@ public int getDiscountCondition(String sku){ } } + @Override public float getDiscount(String sku){ Object dsc = this.inventory.get(sku)[3]; if(dsc != null){ @@ -53,4 +58,21 @@ public float getDiscount(String sku){ return 0f; } } + + public void addEntry(String sku, float price, String varient, String name, int discountCondition, float discount){ + this.inventory.put(sku, new Object[]{price, varient, name, discountCondition, discount}); + } + + public void addEntry(String sku, float price, String varient, String name){ + this.inventory.put(sku, new Object[]{price, varient, name, null}); + } + + public void removeEntry(String sku){ + this.inventory.remove(sku); + } + + @Override + public boolean hasItem(String sku){ + return this.inventory.containsKey(sku); + } } diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index 468b811a0..3821342a9 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -7,17 +7,17 @@ public class Item { protected String name; protected int discountCondition; protected float discount; - protected Inventory inventory; + protected Stock stock; - public Item(String sku) { - this.inventory = new Inventory(); + public Item(String sku, Stock stock) { + this.stock = stock; try{ this.sku = sku; - this.price = this.inventory.getPrice(sku); - this.varient = this.inventory.getVarient(sku); - this.name = this.inventory.getName(sku); - this.discountCondition = this.inventory.getDiscountCondition(sku); - this.discount = this.inventory.getDiscount(sku); + this.price = this.stock.getPrice(sku); + this.varient = this.stock.getVarient(sku); + this.name = this.stock.getName(sku); + this.discountCondition = this.stock.getDiscountCondition(sku); + this.discount = this.stock.getDiscount(sku); } catch (Exception e){ System.out.println("Not a valid SKU"); diff --git a/src/main/java/com/booleanuk/core/Manger.java b/src/main/java/com/booleanuk/core/Manger.java index d9dbf8e94..ef4316ca0 100644 --- a/src/main/java/com/booleanuk/core/Manger.java +++ b/src/main/java/com/booleanuk/core/Manger.java @@ -1,7 +1,15 @@ package com.booleanuk.core; public class Manger extends Basket{ + + public Manger(Inventory inventory) { + super(inventory); + //TODO Auto-generated constructor stub + } + public static void changeBasketSize(int size){ Basket.size = size; } + + //public } diff --git a/src/main/java/com/booleanuk/core/Stock.java b/src/main/java/com/booleanuk/core/Stock.java new file mode 100644 index 000000000..14fb4ca46 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Stock.java @@ -0,0 +1,10 @@ +package com.booleanuk.core; + +public interface Stock { + boolean hasItem(String sku); + public float getPrice(String sku); + public String getVarient(String sku); + public String getName(String sku); + public int getDiscountCondition(String sku); + public float getDiscount(String sku); +} diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index 9a619f620..fefaf7b81 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -1,20 +1,19 @@ package com.booleanuk.core.item_types; +import com.booleanuk.core.Stock; import com.booleanuk.core.Item; import java.util.ArrayList; -import java.util.Arrays; import java.lang.Exception; import java.util.List; public class Bagel extends Item{ - private static final String[] bagelSKU = {"BGLO", "BGLP", "BGLE", "BGLS"}; private List filling; private float totalPrice; - public Bagel(String sku) throws Exception{ - super(sku); - boolean contains = Arrays.stream(Bagel.bagelSKU).anyMatch(sku::equals); + public Bagel(String sku, Stock stock) throws Exception{ + super(sku, stock); + boolean contains = stock.hasItem(sku) & sku.contains("BGL"); if(!contains){ throw new Exception("Must be a valid bagel SKU!"); } @@ -27,7 +26,7 @@ public int getSize(){ } public void addFilling(String sku) throws Exception{ - Filling filling = new Filling(sku); + Filling filling = new Filling(sku, this.stock); this.filling.add(filling); this.totalPrice += filling.getPrice(); } diff --git a/src/main/java/com/booleanuk/core/item_types/Coffee.java b/src/main/java/com/booleanuk/core/item_types/Coffee.java index f558ad614..0e134a85e 100644 --- a/src/main/java/com/booleanuk/core/item_types/Coffee.java +++ b/src/main/java/com/booleanuk/core/item_types/Coffee.java @@ -1,16 +1,15 @@ package com.booleanuk.core.item_types; +import com.booleanuk.core.Stock; import com.booleanuk.core.Item; -import java.util.Arrays; import java.lang.Exception; public class Coffee extends Item { - private static final String[] coffeeSKU = {"COFB", "COFW", "COFC", "COFL"}; - public Coffee(String sku) throws Exception{ - super(sku); - boolean contains = Arrays.stream(Coffee.coffeeSKU).anyMatch(sku::equals); + public Coffee(String sku, Stock stock) throws Exception{ + super(sku, stock); + boolean contains = stock.hasItem(sku) & sku.contains("COF"); if(!contains){ throw new Exception("Must be a valid coffee SKU!"); } diff --git a/src/main/java/com/booleanuk/core/item_types/Filling.java b/src/main/java/com/booleanuk/core/item_types/Filling.java index 335dff49e..92a0fccc0 100644 --- a/src/main/java/com/booleanuk/core/item_types/Filling.java +++ b/src/main/java/com/booleanuk/core/item_types/Filling.java @@ -1,17 +1,16 @@ package com.booleanuk.core.item_types; +import com.booleanuk.core.Stock; import com.booleanuk.core.Item; -import java.util.Arrays; import java.lang.Exception; public class Filling extends Item { - private static final String[] fillingSKU = {"FILB", "FILE", "FILC", "FILX", "FILS", "FILH"}; - public Filling(String sku) throws Exception{ - super(sku); - boolean contains = Arrays.stream(Filling.fillingSKU).anyMatch(sku::equals); + public Filling(String sku, Stock stock) throws Exception{ + super(sku, stock); + boolean contains = stock.hasItem(sku)& sku.contains("FIL"); if(!contains){ throw new Exception("Must be a valid filling SKU!"); } diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 9caa5457e..0eee29fd9 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -19,14 +19,30 @@ public class BasketTest { private Basket b; private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; + private Inventory inventory; public BasketTest() { - this.b = new Basket(); + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + this.b = new Basket(inventory); } @Test public void addBagelTest() throws Exception{ - Bagel bag = new Bagel("BGLO"); + Bagel bag = new Bagel("BGLO", inventory); b.addBagel(bag); Assertions.assertEquals( "BGLO Onion Bagel", b.toString()); } @@ -60,7 +76,7 @@ public void removeItemTest() throws Exception{ @Test public void getTotalCostTest() throws Exception{ - Bagel bag = new Bagel("BGLP"); + Bagel bag = new Bagel("BGLP", inventory); b.addBagel(bag); // 0.39 b.addCoffee("COFB"); // 0.99 bag.addFilling("FILB"); // 0.12 @@ -69,7 +85,7 @@ public void getTotalCostTest() throws Exception{ @Test void throwsWhenBasketIsFull() throws Exception { - Bagel bag = new Bagel("BGLO"); + Bagel bag = new Bagel("BGLO", inventory); for (int i = 0; i < b.getMaxSize(); i++){ b.addBagel(bag); @@ -80,7 +96,7 @@ void throwsWhenBasketIsFull() throws Exception { @Test void throwsWhenBasketIsFull_GigaBagel() throws Exception{ - Bagel bag = new Bagel("BGLO"); + Bagel bag = new Bagel("BGLO", inventory); for (int i = 0; i < b.getMaxSize(); i++){ bag.addFilling("FILB"); } @@ -90,9 +106,9 @@ void throwsWhenBasketIsFull_GigaBagel() throws Exception{ @Test public void discountAppliedTest() throws Exception{ - Bagel bagO = new Bagel("BGLO"); - Bagel bagP = new Bagel("BGLP"); - Bagel bagE = new Bagel("BGLE"); + Bagel bagO = new Bagel("BGLO", inventory); + Bagel bagP = new Bagel("BGLP", inventory); + Bagel bagE = new Bagel("BGLE", inventory); for(int i = 0; i < 16; i++){ @@ -123,7 +139,7 @@ void tearDown() { @Test void printsExpectedReceipt() throws Exception { - Bagel bagP = new Bagel("BGLP"); + Bagel bagP = new Bagel("BGLP", inventory); for(int i = 0; i < 16; i++){ b.addBagel(bagP); } diff --git a/src/test/java/com/booleanuk/core/CustomerTest.java b/src/test/java/com/booleanuk/core/CustomerTest.java index fe3b0e373..9497e0671 100644 --- a/src/test/java/com/booleanuk/core/CustomerTest.java +++ b/src/test/java/com/booleanuk/core/CustomerTest.java @@ -5,9 +5,26 @@ public class CustomerTest { private Customer c; + private Inventory inventory; public CustomerTest(){ - this.c = new Customer(); + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + this.c = new Customer(this.inventory); } @Test diff --git a/src/test/java/com/booleanuk/core/ItemTest.java b/src/test/java/com/booleanuk/core/ItemTest.java index 61480e2a8..80e82d975 100644 --- a/src/test/java/com/booleanuk/core/ItemTest.java +++ b/src/test/java/com/booleanuk/core/ItemTest.java @@ -7,33 +7,54 @@ import org.junit.jupiter.api.Test; public class ItemTest { + + private Inventory inventory; + + public ItemTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } @Test void throwsWhenSkuDoesNotExistAtAll() { - assertThrows(NullPointerException.class, () -> new Item("ZZZZ")); + assertThrows(NullPointerException.class, () -> new Item("ZZZZ", inventory)); } @Test public void getPriceTest(){ - Item i = new Item("COFB"); + Item i = new Item("COFB", inventory); Assertions.assertEquals(0.99, i.getPrice(), 0.0001); } @Test public void getSKUTest(){ - Item i = new Item("COFB"); + Item i = new Item("COFB", inventory); Assertions.assertEquals("COFB", i.getSKU()); } @Test public void getVarientTest(){ - Item i = new Item("COFB"); + Item i = new Item("COFB", inventory); Assertions.assertEquals("Coffee", i.getVarient()); } @Test public void getNameTest(){ - Item i = new Item("COFB"); + Item i = new Item("COFB", inventory); Assertions.assertEquals("Black", i.getName()); } } diff --git a/src/test/java/com/booleanuk/core/MangerTest.java b/src/test/java/com/booleanuk/core/MangerTest.java index 36fe5642a..9363a45f1 100644 --- a/src/test/java/com/booleanuk/core/MangerTest.java +++ b/src/test/java/com/booleanuk/core/MangerTest.java @@ -5,9 +5,25 @@ public class MangerTest { private Manger m; + private Inventory inventory; public MangerTest(){ - this.m = new Manger(); + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + this.m = new Manger(this.inventory); } @Test diff --git a/src/test/java/com/booleanuk/core/StockTest.java b/src/test/java/com/booleanuk/core/StockTest.java new file mode 100644 index 000000000..6b92b3bf5 --- /dev/null +++ b/src/test/java/com/booleanuk/core/StockTest.java @@ -0,0 +1,92 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Assertions; + +import com.booleanuk.core.item_types.Bagel; + +public class StockTest { + + private Stock stock; + private Inventory inventory; + private Basket b; + + public class MiniStock implements Stock{ + private String sku; + + public MiniStock(String sku){ + this.sku = sku; + } + + @Override + public boolean hasItem(String sku){ + return this.sku == sku; + } + + @Override + public float getPrice(String sku){ + return 0.49f; + } + + @Override + public String getVarient(String sku){ + return "Bagel"; + } + + @Override + public String getName(String sku){ + return "Whole Wheat"; + } + + @Override + public int getDiscountCondition(String sku){ + return 4; + } + + @Override + public float getDiscount(String sku){ + return -0.15f; + } + } + + public StockTest(){ + this.stock = new MiniStock("BGLW"); + + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + this.b = new Basket(stock); + } + + @Test + public void stockTest() throws Exception{ + Bagel bag = new Bagel("BGLW", this.stock); + b.addBagel(bag); + Assertions.assertEquals("BGLW Whole Wheat Bagel", b.toString()); + } + + @Test + void throwsSKUinvalid() throws Exception{ + assertThrows(NullPointerException.class, () -> new Bagel("BGLW", this.inventory)); + + Exception exception = assertThrows(Exception.class, () -> b.addFilling("FILB")); + assertEquals("Not a valid SKU", exception.getMessage()); + } +} diff --git a/src/test/java/com/booleanuk/core/item_types/BagelTest.java b/src/test/java/com/booleanuk/core/item_types/BagelTest.java index 2f1c0c9f6..3c170e392 100644 --- a/src/test/java/com/booleanuk/core/item_types/BagelTest.java +++ b/src/test/java/com/booleanuk/core/item_types/BagelTest.java @@ -8,17 +8,40 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; +import com.booleanuk.core.Inventory; + public class BagelTest{ + private Inventory inventory; + + public BagelTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } + @Test void throwsWhenSkuIsValidButNotABagel() { - Exception exception = assertThrows(Exception.class, () -> new Bagel("COFB")); + Exception exception = assertThrows(Exception.class, () -> new Bagel("COFB", inventory)); assertEquals("Must be a valid bagel SKU!", exception.getMessage()); } @Test public void getSize_addFillingTest() throws Exception{ - Bagel bag = new Bagel("BGLO"); + Bagel bag = new Bagel("BGLO", inventory); Assertions.assertEquals(0, bag.getSize()); bag.addFilling("FILB"); Assertions.assertEquals(1, bag.getSize()); @@ -26,7 +49,7 @@ public void getSize_addFillingTest() throws Exception{ @Test public void removeFillingTest() throws Exception{ - Bagel bag = new Bagel("BGLO"); + Bagel bag = new Bagel("BGLO", inventory); bag.addFilling("FILB"); Assertions.assertEquals(1, bag.getSize()); bag.removeFIlling("FILB"); @@ -35,7 +58,7 @@ public void removeFillingTest() throws Exception{ @Test public void getPriceTest() throws Exception{ - Bagel bag = new Bagel("BGLO"); //0.49 + Bagel bag = new Bagel("BGLO", inventory); //0.49 bag.addFilling("FILB"); // 0.12 Assertions.assertEquals(0.61, bag.getPrice(), 0.0001); } diff --git a/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java b/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java index 4a3826ec2..832021f23 100644 --- a/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java +++ b/src/test/java/com/booleanuk/core/item_types/CoffeeTest.java @@ -4,11 +4,34 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; +import com.booleanuk.core.Inventory; + class CoffeeTest { + private Inventory inventory; + + public CoffeeTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } + @Test void throwsWhenSkuIsValidButNotACoffee() { - Exception exception = assertThrows(Exception.class, () -> new Coffee("BGLO")); + Exception exception = assertThrows(Exception.class, () -> new Coffee("BGLO", inventory)); assertEquals("Must be a valid coffee SKU!", exception.getMessage()); } diff --git a/src/test/java/com/booleanuk/core/item_types/FillingTest.java b/src/test/java/com/booleanuk/core/item_types/FillingTest.java index 2f6396cd0..9dcec1609 100644 --- a/src/test/java/com/booleanuk/core/item_types/FillingTest.java +++ b/src/test/java/com/booleanuk/core/item_types/FillingTest.java @@ -4,11 +4,34 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; +import com.booleanuk.core.Inventory; + public class FillingTest { + + private Inventory inventory; + + public FillingTest(){ + this.inventory = new Inventory(); + this.inventory.addEntry("BGLO", 0.49f, "Bagel", "Onion", 6, -0.45f); + this.inventory.addEntry("BGLP", 0.39f, "Bagel", "Plain", 12, -0.69f); + this.inventory.addEntry("BGLE", 0.49f, "Bagel", "Everything", 6, -0.45f); + this.inventory.addEntry("BGLS", 0.49f, "Bagel", "Sesame"); + this.inventory.addEntry("COFB", 0.99f, "Coffee", "Black"); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping... + this.inventory.addEntry("COFW", 1.19f, "Coffee", "White"); + this.inventory.addEntry("COFC", 1.29f, "Coffee", "Capuccino"); + this.inventory.addEntry("COFL", 1.29f, "Coffee", "Latte"); + this.inventory.addEntry("FILB", 0.12f, "Filling", "Bacon"); + this.inventory.addEntry("FILE", 0.12f, "Filling", "Egg"); + this.inventory.addEntry("FILC", 0.12f, "Filling", "Cheese"); + this.inventory.addEntry("FILX", 0.12f, "Filling", "Cream Cheese"); + this.inventory.addEntry("FILS", 0.12f, "Filling", "Smoked Salmon"); + this.inventory.addEntry("FILH", 0.12f, "Filling", "Ham"); + + } @Test void throwsWhenSkuIsValidButNotAFilling() { - Exception exception = assertThrows(Exception.class, () -> new Filling("BGLO")); + Exception exception = assertThrows(Exception.class, () -> new Filling("BGLO", inventory)); assertEquals("Must be a valid filling SKU!", exception.getMessage()); } } From 3e0cb42fa2be3bd672d61c780b70a05af109f526 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Tue, 25 Aug 2026 09:05:07 +0200 Subject: [PATCH 11/15] changed remove item to be more clear --- src/main/java/com/booleanuk/core/Basket.java | 29 ++++++++++---------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 8d1f5aa96..9d365d004 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -11,7 +11,7 @@ public class Basket { protected static int size = 30; private int curSize; private Item[] arr; - private final Stock stock; + private final Stock stock; // Should be static? public Basket(Stock stock){ this.arr = new Item[Basket.size]; @@ -20,8 +20,8 @@ public Basket(Stock stock){ } - public void addBagel(Bagel bag) throws Exception{ - if(curSize + bag.getSize() >= size) + public void addBagel(Bagel bag) throws Exception{ // only one using acuall bagel, only because bagel has its own "basket" of fillings + if(curSize + bag.getSize() >= size) // could do String... n throw new Exception("Exceeds maximum basket size!"); this.arr[this.curSize] = bag; this.curSize += bag.getSize() + 1; // + 1 for bagel @@ -49,19 +49,20 @@ public void addFilling(String sku) throws Exception{ public boolean removeItem(String sku){ for (int i = 0; i < this.curSize; i++){ - - if(this.arr[i].getSKU() == sku){ - for (int j = i; j < this.curSize; j++){ - if (j+1 >= this.curSize){ - this.arr[j] = null; - break; - } - this.arr[j] = this.arr[j+1]; + if(!(this.arr[i].getSKU() == sku)){ + continue; + } + for (int j = i; j < this.curSize; j++){ + if (j+1 >= this.curSize){ + this.arr[j] = null; + break; } - this.arr[i] = this.arr[i+1]; - this.curSize--; - return true; + this.arr[j] = this.arr[j+1]; } + this.arr[i] = this.arr[i+1]; + this.curSize--; + return true; + } return false; } From ab7d4d92faa326a7659e9a1be056188d88e57071 Mon Sep 17 00:00:00 2001 From: David Bylund Date: Tue, 25 Aug 2026 10:13:24 +0200 Subject: [PATCH 12/15] made setters for item --- src/main/java/com/booleanuk/core/Item.java | 36 ++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index 3821342a9..5eb8ae7e3 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -12,12 +12,12 @@ public class Item { public Item(String sku, Stock stock) { this.stock = stock; try{ - this.sku = sku; - this.price = this.stock.getPrice(sku); - this.varient = this.stock.getVarient(sku); - this.name = this.stock.getName(sku); - this.discountCondition = this.stock.getDiscountCondition(sku); - this.discount = this.stock.getDiscount(sku); + setSKU(sku); + setPrice(this.stock.getPrice(sku)); + setVarient(this.stock.getVarient(sku)); + setName(this.stock.getName(sku)); + setDiscountCondition(this.stock.getDiscountCondition(sku)); + setDiscount(this.stock.getDiscount(sku)); } catch (Exception e){ System.out.println("Not a valid SKU"); @@ -49,5 +49,29 @@ public int getDiscountCondition(){ public float getDiscount(){ return this.discount; } + + private void setPrice(float price){ + this.price = price; + } + + private void setSKU(String sku){ + this.sku = sku; + } + + private void setVarient(String varient){ + this.varient = varient; + } + + private void setName(String name){ + this.name = name; + } + + private void setDiscountCondition(int discountCondition){ + this.discountCondition = discountCondition; + } + + private void setDiscount(float discount){ + this.discount = discount; + } } From df03ed4728a91d55d5ea7e26e5e3faa8a7b7298b Mon Sep 17 00:00:00 2001 From: David Bylund Date: Tue, 25 Aug 2026 10:20:35 +0200 Subject: [PATCH 13/15] made most variables private --- src/main/java/com/booleanuk/core/Item.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index 5eb8ae7e3..526b810a0 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -2,11 +2,11 @@ public class Item { protected float price; - protected String sku; - protected String varient; - protected String name; - protected int discountCondition; - protected float discount; + private String sku; + private String varient; + private String name; + private int discountCondition; + private float discount; protected Stock stock; public Item(String sku, Stock stock) { From d861acf8869ac793f00fa95a772918a30161b36e Mon Sep 17 00:00:00 2001 From: David Bylund Date: Tue, 25 Aug 2026 10:27:24 +0200 Subject: [PATCH 14/15] item and bagel changed to now use get and set for stock --- src/main/java/com/booleanuk/core/Item.java | 12 ++++++++++-- .../java/com/booleanuk/core/item_types/Bagel.java | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Item.java b/src/main/java/com/booleanuk/core/Item.java index 526b810a0..91710e1e2 100644 --- a/src/main/java/com/booleanuk/core/Item.java +++ b/src/main/java/com/booleanuk/core/Item.java @@ -7,10 +7,10 @@ public class Item { private String name; private int discountCondition; private float discount; - protected Stock stock; + private Stock stock; public Item(String sku, Stock stock) { - this.stock = stock; + setStock(stock); try{ setSKU(sku); setPrice(this.stock.getPrice(sku)); @@ -49,6 +49,10 @@ public int getDiscountCondition(){ public float getDiscount(){ return this.discount; } + + protected Stock getStock(){ + return this.stock; + } private void setPrice(float price){ this.price = price; @@ -74,4 +78,8 @@ private void setDiscount(float discount){ this.discount = discount; } + private void setStock(Stock stock){ + this.stock = stock; + } + } diff --git a/src/main/java/com/booleanuk/core/item_types/Bagel.java b/src/main/java/com/booleanuk/core/item_types/Bagel.java index fefaf7b81..2dc4486d7 100644 --- a/src/main/java/com/booleanuk/core/item_types/Bagel.java +++ b/src/main/java/com/booleanuk/core/item_types/Bagel.java @@ -26,7 +26,7 @@ public int getSize(){ } public void addFilling(String sku) throws Exception{ - Filling filling = new Filling(sku, this.stock); + Filling filling = new Filling(sku, this.getStock()); this.filling.add(filling); this.totalPrice += filling.getPrice(); } From d184f5c5ce1958011f88acebdf498d2b4cb4365f Mon Sep 17 00:00:00 2001 From: David Bylund Date: Tue, 25 Aug 2026 10:48:31 +0200 Subject: [PATCH 15/15] changed basket and manager to have get and set for basket size --- src/main/java/com/booleanuk/core/Basket.java | 14 +++++++++----- src/main/java/com/booleanuk/core/Manger.java | 2 +- src/test/java/com/booleanuk/core/BasketTest.java | 4 ++-- src/test/java/com/booleanuk/core/MangerTest.java | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 9d365d004..aa59a6501 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -8,7 +8,7 @@ import com.booleanuk.core.item_types.*; public class Basket { - protected static int size = 30; + private static int size = 30; private int curSize; private Item[] arr; private final Stock stock; // Should be static? @@ -21,14 +21,14 @@ public Basket(Stock stock){ } public void addBagel(Bagel bag) throws Exception{ // only one using acuall bagel, only because bagel has its own "basket" of fillings - if(curSize + bag.getSize() >= size) // could do String... n + if(curSize + bag.getSize() >= getBasketSize()) // could do String... n throw new Exception("Exceeds maximum basket size!"); this.arr[this.curSize] = bag; this.curSize += bag.getSize() + 1; // + 1 for bagel } public void addCoffee(String sku) throws Exception{ - if(curSize >= size) + if(curSize >= getBasketSize()) throw new Exception("Exceeds maximum basket size!"); if(!this.stock.hasItem(sku)){ throw new Exception("Not a valid SKU"); @@ -38,7 +38,7 @@ public void addCoffee(String sku) throws Exception{ } public void addFilling(String sku) throws Exception{ - if(curSize >= size) + if(curSize >= getBasketSize()) throw new Exception("Exceeds maximum basket size!"); if(!this.stock.hasItem(sku)){ throw new Exception("Not a valid SKU"); @@ -71,10 +71,14 @@ public float costOf(String sku){ return stock.getPrice(sku); } - public int getMaxSize(){ + public int getBasketSize(){ return Basket.size; } + protected static void setBasketSize(int size){ + Basket.size = size; + } + private record Pair(int count, float discount){} private String padding(int max, String str){ diff --git a/src/main/java/com/booleanuk/core/Manger.java b/src/main/java/com/booleanuk/core/Manger.java index ef4316ca0..8b82f4e00 100644 --- a/src/main/java/com/booleanuk/core/Manger.java +++ b/src/main/java/com/booleanuk/core/Manger.java @@ -8,7 +8,7 @@ public Manger(Inventory inventory) { } public static void changeBasketSize(int size){ - Basket.size = size; + Basket.setBasketSize(size); } //public diff --git a/src/test/java/com/booleanuk/core/BasketTest.java b/src/test/java/com/booleanuk/core/BasketTest.java index 0eee29fd9..02550319a 100644 --- a/src/test/java/com/booleanuk/core/BasketTest.java +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -87,7 +87,7 @@ public void getTotalCostTest() throws Exception{ void throwsWhenBasketIsFull() throws Exception { Bagel bag = new Bagel("BGLO", inventory); - for (int i = 0; i < b.getMaxSize(); i++){ + for (int i = 0; i < b.getBasketSize(); i++){ b.addBagel(bag); } Exception exception = assertThrows(Exception.class, () -> b.addBagel(bag)); @@ -97,7 +97,7 @@ void throwsWhenBasketIsFull() throws Exception { @Test void throwsWhenBasketIsFull_GigaBagel() throws Exception{ Bagel bag = new Bagel("BGLO", inventory); - for (int i = 0; i < b.getMaxSize(); i++){ + for (int i = 0; i < b.getBasketSize(); i++){ bag.addFilling("FILB"); } Exception exception = assertThrows(Exception.class, () -> b.addBagel(bag)); diff --git a/src/test/java/com/booleanuk/core/MangerTest.java b/src/test/java/com/booleanuk/core/MangerTest.java index 9363a45f1..7d9e326d9 100644 --- a/src/test/java/com/booleanuk/core/MangerTest.java +++ b/src/test/java/com/booleanuk/core/MangerTest.java @@ -29,6 +29,6 @@ public MangerTest(){ @Test public void changeBasketSizeTest(){ Manger.changeBasketSize(35); - Assertions.assertEquals(35, m.getMaxSize()); + Assertions.assertEquals(35, m.getBasketSize()); } }