From 50873c0b1d5a99ebf1ba0456c2d973b1b5de05d1 Mon Sep 17 00:00:00 2001 From: andrechamoun555 Date: Mon, 18 Aug 2025 15:11:24 +0200 Subject: [PATCH 1/4] domain model done --- DOMAINMODEL.md | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 DOMAINMODEL.md diff --git a/DOMAINMODEL.md b/DOMAINMODEL.md new file mode 100644 index 000000000..23b6c2f24 --- /dev/null +++ b/DOMAINMODEL.md @@ -0,0 +1,43 @@ + +--- +# Domain Model – Bob’s Bagels + +| Class | Fields | Methods | What it does | Return values | +|--|----------------------------------------------------------------|----------------------------------|--------------------------------------------------|-------------------| +| **Inventory** | `static Map items` | `has(sku): boolean` | If given SKU exists in inventory | `true` otherwise `false` if not exist | +| | | `priceOf(sku): double` | Looks up price of given SKU | `double` | +| | | `typeOf(sku): String` | Tells which type of item | `String` | +| | | `nameOf(sku): String` | Which name the SKU has | `String` | +| | | `variantOf(sku): String` | Which variant --> Onion, ham, latte | `String` | +| | | `allSkus(): ArrayList` | List all SKU | `ArrayList` | +| **ItemInfo** | `price: double`
`type: String`
`name: String`
`variant: String` | | container for SKU --> type, price, name, variant | | +| **BasketEntry** | `sku: String`
`qty: int` | `inc(n): void` | Increases quantity | `void` | +| | | `dec(n): void` | Decreases quanitiy | `void` | +| | | `lineTotal(): double` | Calculate sum | `double` | +| **Basket** | `capacity: int`
`entries: ArrayList` | `add(sku): String` | Tries to add item | `String` | +| | | `remove(sku): String` | Removes one item | `String` | +| | | `isFull(): boolean` | Check if basket is full | `boolean` | +| | | `setCapacity(newCap: int): void` | Change capacity (manager) | `String`, `boolean`, `void`, `double` | +| | | `total(): double` | Sums up all the cost (call linetotal()) | `void` | +| | | `checkPrice(sku): double` | Looks up price |`double` | + + + + + +--- + +## User Stories + +1. **Add bagel to basket** +2. **Remove bagel from basket** +3. **Know when basket is full** +4. **Manager changes basket capacity** +5. **Remove non-existent item shows message** +6. **Total cost of basket** +7. **Cost of item before adding** +8. **Choose fillings for bagel** +9. **Know cost of fillings** +10. **Only order things in stock** + +--- From 166dd68568355177ec98e204837f3a67110acb5d Mon Sep 17 00:00:00 2001 From: andrechamoun555 Date: Tue, 19 Aug 2025 14:24:33 +0200 Subject: [PATCH 2/4] done with Inventory unit testing --- DOMAINMODEL.md | 2 +- .../java/com/booleanuk/core/Inventory.java | 56 ++++++++++++ .../java/com/booleanuk/core/ItemInfo.java | 32 +++++++ .../com/booleanuk/core/InventoryTest.java | 91 +++++++++++++++++++ 4 files changed, 180 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/booleanuk/core/Inventory.java create mode 100644 src/main/java/com/booleanuk/core/ItemInfo.java create mode 100644 src/test/java/com/booleanuk/core/InventoryTest.java diff --git a/DOMAINMODEL.md b/DOMAINMODEL.md index 23b6c2f24..57d9cc489 100644 --- a/DOMAINMODEL.md +++ b/DOMAINMODEL.md @@ -17,7 +17,7 @@ | **Basket** | `capacity: int`
`entries: ArrayList` | `add(sku): String` | Tries to add item | `String` | | | | `remove(sku): String` | Removes one item | `String` | | | | `isFull(): boolean` | Check if basket is full | `boolean` | -| | | `setCapacity(newCap: int): void` | Change capacity (manager) | `String`, `boolean`, `void`, `double` | +| | | `setCapacity(newCap: int): void` | Change capacity (manager) | `void` | | | | `total(): double` | Sums up all the cost (call linetotal()) | `void` | | | | `checkPrice(sku): double` | Looks up price |`double` | 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..239200e01 --- /dev/null +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -0,0 +1,56 @@ +package com.booleanuk.core; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +public class Inventory { + + static Map items = new HashMap<>(); + + static { +// // BAGEL + items.put("BGLO", new ItemInfo("Bagel", "BAGEL", 0.49, "Onion" )); + items.put("BGLP", new ItemInfo("Bagel", "BAGEL", 0.39, "Plain" )); +// items.put("BGLE", new ItemInfo("Bagel", "BAGEL", 0.49, "Everything" )); +// items.put("BGLS", new ItemInfo("Bagel", "BAGEL", 0.49, "Sesame" )); +// +// //COFFEE +// items.put("COFB", new ItemInfo("Coffe", "COFFEE", 0.99, "Black")); +// items.put("COFW", new ItemInfo("Coffe", "COFFEE", 1.19, "White")); +// items.put("COFC", new ItemInfo("Coffe", "COFFEE", 1.29, "Capuccino")); +// items.put("COFL", new ItemInfo("Coffe", "COFFEE", 1.29, "Latte")); +// +// //FILLING +// items.put("FILB", new ItemInfo("Filling", "FILLING", 0.12, "Bacon" )); +// items.put("FILE", new ItemInfo("Filling", "FILLING", 0.12, "Egg" )); +// items.put("FILC", new ItemInfo("Filling", "FILLING", 0.12, "Cheese" )); +// items.put("FILX", new ItemInfo("Filling", "FILLING", 0.12, "Cream Cheese" )); +// items.put("FILS", new ItemInfo("Filling", "FILLING", 0.12, "Smoked Salmon" )); +// items.put("FILH", new ItemInfo("Filling", "FILLING", 0.12, "Ham" )); + } + + public boolean has(String sku){ + return false; + } + + public Double priceOf(String sku){ + return -1.0; + } + + public String typeOf(String sku){ + return ""; + } + + public String nameOf(String sku){ + return ""; + } + + public String variantOf(String sku) { + return ""; + } + + public ArrayList allSkus(){ + return new ArrayList<>(); + } +} diff --git a/src/main/java/com/booleanuk/core/ItemInfo.java b/src/main/java/com/booleanuk/core/ItemInfo.java new file mode 100644 index 000000000..4fd166c39 --- /dev/null +++ b/src/main/java/com/booleanuk/core/ItemInfo.java @@ -0,0 +1,32 @@ +package com.booleanuk.core; + +public class ItemInfo { + private String name; + private String type; + private double price; + private String variant; + + + public ItemInfo(String name, String type, double price, String variant) { + this.name = name; + this.type = type; + this.price = price; + this.variant = variant; + } + + public String getName() { + return name; + } + + public String getVariant() { + return variant; + } + + public double getPrice() { + return price; + } + + public String getType() { + return type; + } +} diff --git a/src/test/java/com/booleanuk/core/InventoryTest.java b/src/test/java/com/booleanuk/core/InventoryTest.java new file mode 100644 index 000000000..2e966e23b --- /dev/null +++ b/src/test/java/com/booleanuk/core/InventoryTest.java @@ -0,0 +1,91 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; + +public class InventoryTest { + + @Test + public void returnTrueForValidSku(){ + Inventory inventory = new Inventory(); + String validSku = "BGLO"; + + Assertions.assertTrue(inventory.has(validSku)); + } + + @Test + public void returnFalseForInvalidSku(){ + Inventory inventory = new Inventory(); + String invalidSku = "BGLL"; + Assertions.assertFalse(inventory.has(invalidSku)); + } + + @Test + public void returnPriceOfValidSku(){ + Inventory inventory = new Inventory(); + String validSku = "BGLO"; + double price = 0.49; + + double result = inventory.priceOf(validSku); + + Assertions.assertEquals(price, result); + } + + @Test + public void returnPriceOfInvalidSku(){ + Inventory inventory = new Inventory(); + double inValidPrice = inventory.priceOf("BGLL"); + Assertions.assertFalse(inValidPrice > 0); + } + + @Test + public void returnNameOfValidSku(){ + Inventory inventory = new Inventory(); + + String validSku = "BAGEL"; + String name = inventory.nameOf(validSku); + Assertions.assertEquals(validSku, name); + } + + @Test + public void returnNameOfInvalidSku(){ + Inventory inventory = new Inventory(); + String invalidSku = "NON EXIST SKU"; + String name = inventory.nameOf(invalidSku); + Assertions.assertNull(name); + } + + @Test + public void returnVariantOfValidSku(){ + Inventory inventory = new Inventory(); + String validSku = "Onion"; + + String variant = inventory.variantOf(validSku); + Assertions.assertEquals(validSku, variant); + } + + @Test + public void returnVariantOfInvalidSku(){ + Inventory inventory = new Inventory(); + String invalidSku = "Onion"; + String variant = inventory.variantOf(invalidSku); + Assertions.assertNull(variant); + } + + @Test + public void returnAllValidSku() { + Inventory inventory = new Inventory(); + + ArrayList list = new ArrayList<>(); + list.add("BGLO"); + list.add("COFW"); + + ArrayList actual = inventory.allSkus(); + Assertions.assertEquals(list, actual); + Assertions.assertFalse(list.isEmpty()); + + } + +} From 38e439d6a08bea19eefd5f79c6ed21587b032389 Mon Sep 17 00:00:00 2001 From: andrechamoun555 Date: Tue, 19 Aug 2025 19:41:36 +0200 Subject: [PATCH 3/4] Done with BasketItem test --- src/main/java/com/booleanuk/core/Basket.java | 8 ++++ .../java/com/booleanuk/core/BasketItem.java | 30 +++++++++++++ .../java/com/booleanuk/core/ItemInfo.java | 2 + .../com/booleanuk/core/BasketItemTest.java | 45 +++++++++++++++++++ .../java/com/booleanuk/core/ItemInfoTest.java | 27 +++++++++++ 5 files changed, 112 insertions(+) create mode 100644 src/main/java/com/booleanuk/core/Basket.java create mode 100644 src/main/java/com/booleanuk/core/BasketItem.java create mode 100644 src/test/java/com/booleanuk/core/BasketItemTest.java create mode 100644 src/test/java/com/booleanuk/core/ItemInfoTest.java 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..075cc18da --- /dev/null +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -0,0 +1,8 @@ +package com.booleanuk.core; + +public class Basket { + + //This class should contain ArrayList + //Each time an Item is added, it should add a new basket line +} + diff --git a/src/main/java/com/booleanuk/core/BasketItem.java b/src/main/java/com/booleanuk/core/BasketItem.java new file mode 100644 index 000000000..c55a14ea9 --- /dev/null +++ b/src/main/java/com/booleanuk/core/BasketItem.java @@ -0,0 +1,30 @@ +package com.booleanuk.core; + +public class BasketItem { + private String sku; + private int quantity; + + public BasketItem(String sku, int quantity) { + this.sku = sku; + this.quantity = quantity; + } + + public String getSku() { + return sku; + } + + public int getQuantity() { + return quantity; + } + + public void increaseQuantity(int quantity){ + } + + public void decreaseQuantity(int quantity){ + } + + public double lineSumTotal(){ + // calculate the sum of each basket line + return -1.0; + } +} diff --git a/src/main/java/com/booleanuk/core/ItemInfo.java b/src/main/java/com/booleanuk/core/ItemInfo.java index 4fd166c39..14a0f95a0 100644 --- a/src/main/java/com/booleanuk/core/ItemInfo.java +++ b/src/main/java/com/booleanuk/core/ItemInfo.java @@ -14,6 +14,7 @@ public ItemInfo(String name, String type, double price, String variant) { this.variant = variant; } + public String getName() { return name; } @@ -29,4 +30,5 @@ public double getPrice() { public String getType() { return type; } + } diff --git a/src/test/java/com/booleanuk/core/BasketItemTest.java b/src/test/java/com/booleanuk/core/BasketItemTest.java new file mode 100644 index 000000000..adc12ec49 --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketItemTest.java @@ -0,0 +1,45 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketItemTest { + + @Test + public void testConstructor(){ + String expectedSku = "sku"; + int expectedQuantity = 1; + + BasketItem basketItem = new BasketItem(expectedSku, expectedQuantity); + String actualSku = basketItem.getSku(); + int actualQuantity = basketItem.getQuantity(); + + Assertions.assertEquals(expectedSku, actualSku); + + } + + @Test + public void testIfQuantityIncrease(){ + String expectedSku = "BGLO"; + BasketItem basketItem = new BasketItem(expectedSku, 1); + + basketItem.increaseQuantity(1); + Assertions.assertEquals(2, basketItem.getQuantity()); + } + + @Test + public void testIfQuantityDecrease(){ + String expectedSku = "BGLO"; + BasketItem basketItem = new BasketItem(expectedSku, 2); + basketItem.decreaseQuantity(1); + Assertions.assertEquals(1, basketItem.getQuantity()); + } + + @Test + public void testLineSumOfTotal(){ + BasketItem basketItem = new BasketItem("BGLO", 2); + double expected = 2 * 0.49; + + Assertions.assertEquals(expected, basketItem.lineSumTotal()); + } +} diff --git a/src/test/java/com/booleanuk/core/ItemInfoTest.java b/src/test/java/com/booleanuk/core/ItemInfoTest.java new file mode 100644 index 000000000..1781c5d70 --- /dev/null +++ b/src/test/java/com/booleanuk/core/ItemInfoTest.java @@ -0,0 +1,27 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class ItemInfoTest { + + @Test + public void testGetItem() { + String expectedName = "Bagel"; + String expectedType = "BAGEL"; + double expectedPrice = 0.49; + String expectedVariant = "Onion"; + + ItemInfo itemInfo = new ItemInfo(expectedName, expectedType, expectedPrice, expectedVariant ); + String actualName = itemInfo.getName(); + String actualType = itemInfo.getType(); + double actualPrice = itemInfo.getPrice(); + String actualVariant = itemInfo.getVariant(); + + Assertions.assertEquals(expectedName, actualName); + Assertions.assertEquals(expectedType, actualType); + Assertions.assertEquals(expectedPrice, actualPrice); + Assertions.assertEquals(expectedVariant, actualVariant); + + } +} From 7b251557bab5e7b0422468d653023f5b9eaa15fb Mon Sep 17 00:00:00 2001 From: andrechamoun555 Date: Tue, 9 Sep 2025 14:01:50 +0200 Subject: [PATCH 4/4] done --- DOMAINMODEL.md | 34 ++++---- src/main/java/com/booleanuk/core/Basket.java | 82 ++++++++++++++++++- .../java/com/booleanuk/core/BasketItem.java | 21 ++++- .../java/com/booleanuk/core/Inventory.java | 66 +++++++++------ .../java/com/booleanuk/core/BasketTest.java | 66 +++++++++++++++ .../com/booleanuk/core/InventoryTest.java | 57 ++++++------- 6 files changed, 248 insertions(+), 78 deletions(-) create mode 100644 src/test/java/com/booleanuk/core/BasketTest.java diff --git a/DOMAINMODEL.md b/DOMAINMODEL.md index 57d9cc489..14be3368d 100644 --- a/DOMAINMODEL.md +++ b/DOMAINMODEL.md @@ -2,24 +2,24 @@ --- # Domain Model – Bob’s Bagels -| Class | Fields | Methods | What it does | Return values | -|--|----------------------------------------------------------------|----------------------------------|--------------------------------------------------|-------------------| +| Class | Fields | Methods | What it does | Return values | +|--|----------------------------------------------------------------|----------------------------------|--------------------------------------------------|---------------------------------------| | **Inventory** | `static Map items` | `has(sku): boolean` | If given SKU exists in inventory | `true` otherwise `false` if not exist | -| | | `priceOf(sku): double` | Looks up price of given SKU | `double` | -| | | `typeOf(sku): String` | Tells which type of item | `String` | -| | | `nameOf(sku): String` | Which name the SKU has | `String` | -| | | `variantOf(sku): String` | Which variant --> Onion, ham, latte | `String` | -| | | `allSkus(): ArrayList` | List all SKU | `ArrayList` | -| **ItemInfo** | `price: double`
`type: String`
`name: String`
`variant: String` | | container for SKU --> type, price, name, variant | | -| **BasketEntry** | `sku: String`
`qty: int` | `inc(n): void` | Increases quantity | `void` | -| | | `dec(n): void` | Decreases quanitiy | `void` | -| | | `lineTotal(): double` | Calculate sum | `double` | -| **Basket** | `capacity: int`
`entries: ArrayList` | `add(sku): String` | Tries to add item | `String` | -| | | `remove(sku): String` | Removes one item | `String` | -| | | `isFull(): boolean` | Check if basket is full | `boolean` | -| | | `setCapacity(newCap: int): void` | Change capacity (manager) | `void` | -| | | `total(): double` | Sums up all the cost (call linetotal()) | `void` | -| | | `checkPrice(sku): double` | Looks up price |`double` | +| | | `priceOf(sku): double` | Looks up price of given SKU | `double` | +| | | `typeOf(sku): String` | Tells which type of item | `String` | +| | | `nameOf(sku): String` | Which name the SKU has | `String` | +| | | `variantOf(sku): String` | Which variant --> Onion, ham, latte | `String` | +| | | `allSkus(): ArrayList` | List all SKU | `ArrayList` | +| **ItemInfo** | `price: double`
`type: String`
`name: String`
`variant: String` | | container for SKU --> type, price, name, variant | | +| **BasketEntry** | `sku: String`
`qty: int` | `inc(n): void` | Increases quantity | `void` | +| | | `dec(n): void` | Decreases quanitiy | `void` | +| | | `lineTotal(): double` | Calculate sum | `double` | +| **Basket** | `capacity: int`
`entries: ArrayList` | `add(sku): String` | Tries to add item | `String` | +| | | `remove(sku): String` | Removes one item | `String` | +| | | `isFull(): boolean` | Check if basket is full | `boolean` | +| | | `setCapacity(newCap: int): void` | Change capacity (manager) | `void` | +| | | `total(): double` | Basket.total() --> loops line --> calls each entry.LineTotal --> adds them --> return the sum | `double` | +| | | `checkPrice(sku): double` | Looks up price | `double` | diff --git a/src/main/java/com/booleanuk/core/Basket.java b/src/main/java/com/booleanuk/core/Basket.java index 075cc18da..a8267f4e6 100644 --- a/src/main/java/com/booleanuk/core/Basket.java +++ b/src/main/java/com/booleanuk/core/Basket.java @@ -1,8 +1,86 @@ package com.booleanuk.core; +import java.util.ArrayList; +import java.util.List; + public class Basket { - //This class should contain ArrayList - //Each time an Item is added, it should add a new basket line + private int capacity; + private final List lines = new ArrayList<>(); + private final Inventory inventory = new Inventory(); + + public Basket(int capacity) { + setCapacity(capacity); + } + + public Basket() { + this(5); + } + + public String addItem(String sku) { + if (!Inventory.has(sku)){ + return "Not in Stock"; + } + + if (isFull()) { + return "Basket is full"; + } + + lines.add(new BasketItem(sku,1)); + return "Added"; + } + + public String removeItem(String sku) { + for (int i = 0; i < lines.size(); i++) { + BasketItem line = lines.get(i); + if (line.getSku().equals(sku)) { + if (line.getQuantity() > 1) { + line.setQuantity(line.getQuantity() - 1); + } else { + lines.remove(i); + } + return "Removed"; + } + } + return "Item not in basket"; + } + + public boolean isFull() { + if (lines.size() >= capacity) { + return true; + } + return false; + + } + + public void setCapacity(int capacity) { + this.capacity = capacity; + + } + + + public double total() { + double sum = 0.0; + for (BasketItem item : lines) { + ItemInfo info = Inventory.items.get(item.getSku()); + if (info != null) { + sum += info.getPrice() * item.getQuantity(); + } + } + return sum; + } + + public double checkPrice(String sku) { + if(!inventory.items.containsKey(sku)){ + throw new IllegalArgumentException("invalid sku" + sku); + } + return inventory.priceOf(sku); + } + + public int getCapacity() { + return capacity; + } } +// Basket.total() --> loops line --> calls each entry.LineTotal --> adds them --> return the sum + diff --git a/src/main/java/com/booleanuk/core/BasketItem.java b/src/main/java/com/booleanuk/core/BasketItem.java index c55a14ea9..c494f5a2b 100644 --- a/src/main/java/com/booleanuk/core/BasketItem.java +++ b/src/main/java/com/booleanuk/core/BasketItem.java @@ -17,14 +17,31 @@ public int getQuantity() { return quantity; } + public void setSku(String sku) { + this.sku = sku; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + public void increaseQuantity(int quantity){ + if (quantity <= 0){ + throw new IllegalArgumentException("Amount must be greater than zero."); + } + this.quantity += quantity; } public void decreaseQuantity(int quantity){ + if (quantity <= 0){ + throw new IllegalArgumentException("amount must be greater than zero."); + } + this.quantity -= quantity; } public double lineSumTotal(){ - // calculate the sum of each basket line - return -1.0; + Inventory inventory = new Inventory(); + return this.quantity * inventory.priceOf(this.sku); } } diff --git a/src/main/java/com/booleanuk/core/Inventory.java b/src/main/java/com/booleanuk/core/Inventory.java index 239200e01..f0db0a8d7 100644 --- a/src/main/java/com/booleanuk/core/Inventory.java +++ b/src/main/java/com/booleanuk/core/Inventory.java @@ -9,48 +9,62 @@ public class Inventory { static Map items = new HashMap<>(); static { -// // BAGEL + // BAGEL items.put("BGLO", new ItemInfo("Bagel", "BAGEL", 0.49, "Onion" )); items.put("BGLP", new ItemInfo("Bagel", "BAGEL", 0.39, "Plain" )); -// items.put("BGLE", new ItemInfo("Bagel", "BAGEL", 0.49, "Everything" )); -// items.put("BGLS", new ItemInfo("Bagel", "BAGEL", 0.49, "Sesame" )); -// -// //COFFEE -// items.put("COFB", new ItemInfo("Coffe", "COFFEE", 0.99, "Black")); -// items.put("COFW", new ItemInfo("Coffe", "COFFEE", 1.19, "White")); -// items.put("COFC", new ItemInfo("Coffe", "COFFEE", 1.29, "Capuccino")); -// items.put("COFL", new ItemInfo("Coffe", "COFFEE", 1.29, "Latte")); -// -// //FILLING -// items.put("FILB", new ItemInfo("Filling", "FILLING", 0.12, "Bacon" )); -// items.put("FILE", new ItemInfo("Filling", "FILLING", 0.12, "Egg" )); -// items.put("FILC", new ItemInfo("Filling", "FILLING", 0.12, "Cheese" )); -// items.put("FILX", new ItemInfo("Filling", "FILLING", 0.12, "Cream Cheese" )); -// items.put("FILS", new ItemInfo("Filling", "FILLING", 0.12, "Smoked Salmon" )); -// items.put("FILH", new ItemInfo("Filling", "FILLING", 0.12, "Ham" )); + items.put("BGLE", new ItemInfo("Bagel", "BAGEL", 0.49, "Everything" )); + items.put("BGLS", new ItemInfo("Bagel", "BAGEL", 0.49, "Sesame" )); + + //COFFEE + items.put("COFB", new ItemInfo("Coffe", "COFFEE", 0.99, "Black")); + items.put("COFW", new ItemInfo("Coffe", "COFFEE", 1.19, "White")); + items.put("COFC", new ItemInfo("Coffe", "COFFEE", 1.29, "Capuccino")); + items.put("COFL", new ItemInfo("Coffe", "COFFEE", 1.29, "Latte")); + + //FILLING + items.put("FILB", new ItemInfo("Filling", "FILLING", 0.12, "Bacon" )); + items.put("FILE", new ItemInfo("Filling", "FILLING", 0.12, "Egg" )); + items.put("FILC", new ItemInfo("Filling", "FILLING", 0.12, "Cheese" )); + items.put("FILX", new ItemInfo("Filling", "FILLING", 0.12, "Cream Cheese" )); + items.put("FILS", new ItemInfo("Filling", "FILLING", 0.12, "Smoked Salmon" )); + items.put("FILH", new ItemInfo("Filling", "FILLING", 0.12, "Ham" )); } - public boolean has(String sku){ - return false; + + public static boolean has(String sku) { + return items.containsKey(sku); } - public Double priceOf(String sku){ - return -1.0; + public double priceOf(String sku) { + ItemInfo info = items.get(sku); + return info != null ? info.getPrice() : 0.0; } public String typeOf(String sku){ - return ""; + ItemInfo info = items.get(sku); + if (info == null){ + throw new IllegalArgumentException("invalid sku: " + sku); + } + return info.getType(); } public String nameOf(String sku){ - return ""; + ItemInfo info = items.get(sku); + if (info == null){ + throw new IllegalArgumentException("invalid sku: " + sku); + } + return info.getName(); } public String variantOf(String sku) { - return ""; + ItemInfo info = items.get(sku); + return info != null ? info.getVariant() : null; + } public ArrayList allSkus(){ - return new ArrayList<>(); + return new ArrayList<>(items.keySet()); } -} + + +} \ No newline at end of file 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..0c43a89bd --- /dev/null +++ b/src/test/java/com/booleanuk/core/BasketTest.java @@ -0,0 +1,66 @@ +package com.booleanuk.core; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class BasketTest { + + @Test + public void ConstructorStartEmptySetsCapacity() { + Basket basket = new Basket(5); + + Assertions.assertEquals(5, basket.getCapacity()); + Assertions.assertFalse(basket.isFull()); + } + + @Test + public void zeroCapicityIsFull() { + Basket basket = new Basket(0); + Assertions.assertEquals(0, basket.getCapacity()); + Assertions.assertTrue(basket.isFull()); + } + + @Test + public void addNewItem_createsLine_andAffectsTotal() { + Basket basket = new Basket(5); + + String msg = basket.addItem("BGLO"); // 0.49 + Assertions.assertEquals("Added", msg); + Assertions.assertEquals(0.49, basket.total(), 1e-6); + Assertions.assertFalse(basket.isFull()); + } + @Test + public void addSkuIncrementQtyAffectTotal() { + Basket basket = new Basket(5); + basket.addItem("BGLO"); + basket.addItem("BGLO"); + + Assertions.assertEquals(0.98, basket.total(), 1e-6); + } + + @Test + void remove_decrements_thenRemoves_andMessages() { + Basket basket = new Basket(5); + + basket.addItem("BGLO"); + basket.addItem("BGLO"); + + String msg1 = basket.removeItem("BGLO"); + Assertions.assertEquals("Removed", msg1); + Assertions.assertEquals(0.49, basket.total(), 1e-6); + + String msg2 = basket.removeItem("BGLO"); + Assertions.assertEquals("Removed", msg2); + Assertions.assertEquals(0.0, basket.total(), 1e-6); + + Assertions.assertEquals("Item not in basket", basket.removeItem("BGLO")); + } + + @Test + void checkPrice_delegatesToInventory() { + Basket basket = new Basket(5); + Assertions.assertEquals(0.49, basket.checkPrice("BGLO"), 1e-6); + Assertions.assertEquals(1.19, basket.checkPrice("COFW"), 1e-6); + } + +} diff --git a/src/test/java/com/booleanuk/core/InventoryTest.java b/src/test/java/com/booleanuk/core/InventoryTest.java index 2e966e23b..3b491a60e 100644 --- a/src/test/java/com/booleanuk/core/InventoryTest.java +++ b/src/test/java/com/booleanuk/core/InventoryTest.java @@ -4,11 +4,12 @@ import org.junit.jupiter.api.Test; import java.util.ArrayList; +import java.util.Collections; public class InventoryTest { @Test - public void returnTrueForValidSku(){ + public void returnTrueForValidSku() { Inventory inventory = new Inventory(); String validSku = "BGLO"; @@ -16,14 +17,24 @@ public void returnTrueForValidSku(){ } @Test - public void returnFalseForInvalidSku(){ + void has_returnsTrueForValidSku() { + Assertions.assertTrue(Inventory.has("BGLO")); // static call + } + + @Test + void has_returnsFalseForInvalidSku() { + Assertions.assertFalse(Inventory.has("BGLL")); // static call + } + + @Test + public void returnFalseForInvalidSku() { Inventory inventory = new Inventory(); String invalidSku = "BGLL"; Assertions.assertFalse(inventory.has(invalidSku)); } @Test - public void returnPriceOfValidSku(){ + public void returnPriceOfValidSku() { Inventory inventory = new Inventory(); String validSku = "BGLO"; double price = 0.49; @@ -34,58 +45,42 @@ public void returnPriceOfValidSku(){ } @Test - public void returnPriceOfInvalidSku(){ + public void returnPriceOfInvalidSku() { Inventory inventory = new Inventory(); double inValidPrice = inventory.priceOf("BGLL"); Assertions.assertFalse(inValidPrice > 0); } @Test - public void returnNameOfValidSku(){ + public void returnNameOfValidSku() { Inventory inventory = new Inventory(); - String validSku = "BAGEL"; + String validSku = "BGLO"; String name = inventory.nameOf(validSku); - Assertions.assertEquals(validSku, name); + Assertions.assertEquals("Bagel", name); } @Test - public void returnNameOfInvalidSku(){ + public void returnNameOfInvalidSku() { Inventory inventory = new Inventory(); - String invalidSku = "NON EXIST SKU"; - String name = inventory.nameOf(invalidSku); - Assertions.assertNull(name); + Assertions.assertThrows(IllegalArgumentException.class, + () -> inventory.nameOf("NON EXIST SKU")); } @Test - public void returnVariantOfValidSku(){ + public void returnVariantOfValidSku() { Inventory inventory = new Inventory(); - String validSku = "Onion"; + String sku = "BGLO"; - String variant = inventory.variantOf(validSku); - Assertions.assertEquals(validSku, variant); + String variant = inventory.variantOf(sku); + Assertions.assertEquals("Onion", variant); } @Test - public void returnVariantOfInvalidSku(){ + public void returnVariantOfInvalidSku() { Inventory inventory = new Inventory(); String invalidSku = "Onion"; String variant = inventory.variantOf(invalidSku); Assertions.assertNull(variant); } - - @Test - public void returnAllValidSku() { - Inventory inventory = new Inventory(); - - ArrayList list = new ArrayList<>(); - list.add("BGLO"); - list.add("COFW"); - - ArrayList actual = inventory.allSkus(); - Assertions.assertEquals(list, actual); - Assertions.assertFalse(list.isEmpty()); - - } - }