Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions DOMAINMODEL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

---
# Domain Model – Bob’s Bagels

| Class | Fields | Methods | What it does | Return values |
|--|----------------------------------------------------------------|----------------------------------|--------------------------------------------------|---------------------------------------|
| **Inventory** | `static Map<String, ItemInfo> 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<String>` | List all SKU | `ArrayList<String>` |
| **ItemInfo** | `price: double`<br>`type: String`<br>`name: String`<br>`variant: String` | | container for SKU --> type, price, name, variant | |
| **BasketEntry** | `sku: String`<br>`qty: int` | `inc(n): void` | Increases quantity | `void` |
| | | `dec(n): void` | Decreases quanitiy | `void` |
| | | `lineTotal(): double` | Calculate sum | `double` |
| **Basket** | `capacity: int`<br>`entries: ArrayList<BasketEntry>` | `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` |





---

## 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**

---
86 changes: 86 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.List;

public class Basket {

private int capacity;
private final List<BasketItem> 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

47 changes: 47 additions & 0 deletions src/main/java/com/booleanuk/core/BasketItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 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(){
Inventory inventory = new Inventory();
return this.quantity * inventory.priceOf(this.sku);
}
}
70 changes: 70 additions & 0 deletions src/main/java/com/booleanuk/core/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.booleanuk.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Inventory {

static Map<String, ItemInfo> 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 static boolean has(String sku) {
return items.containsKey(sku);
}

public double priceOf(String sku) {
ItemInfo info = items.get(sku);
return info != null ? info.getPrice() : 0.0;
}

public String typeOf(String sku){
ItemInfo info = items.get(sku);
if (info == null){
throw new IllegalArgumentException("invalid sku: " + sku);
}
return info.getType();
}

public String nameOf(String sku){
ItemInfo info = items.get(sku);
if (info == null){
throw new IllegalArgumentException("invalid sku: " + sku);
}
return info.getName();
}

public String variantOf(String sku) {
ItemInfo info = items.get(sku);
return info != null ? info.getVariant() : null;

}

public ArrayList<String> allSkus(){
return new ArrayList<>(items.keySet());
}


}
34 changes: 34 additions & 0 deletions src/main/java/com/booleanuk/core/ItemInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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;
}

}
45 changes: 45 additions & 0 deletions src/test/java/com/booleanuk/core/BasketItemTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
66 changes: 66 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
Loading
Loading