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
61 changes: 61 additions & 0 deletions domain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# class diagram

```
------------------
| Product |
------------------
| - sku: String |
| - name: String |
| - variant: String |
| - price: double |
------------------
| + getPrice(): double |
| + getName(): String |
| + getVariant(): String |
| + getSku(): String |
------------------

------------------
| Filling |
------------------
| - inherits from Product |
------------------

------------------
| Bagel |
------------------
| - inherits from Product |
| - fillings: List<Filling> |
------------------
| + addFilling(Filling): void |
| + removeFilling(Filling): void |
| + getFillings(): List<Filling> |
| + getTotalPrice(): double |
------------------

------------------
| Basket |
------------------
| - items: List<Bagel> |
| - capacity: int |
------------------
| + addItem(Bagel): boolean |
| + removeItem(Bagel): boolean |
| + isFull(): boolean |
| + getTotalCost(): double |
| + getItems(): List<Bagel> |
| + setCapacity(int): void |
| + hasItem(Bagel): boolean |
------------------

------------------
| Inventory |
------------------
| - products: List<Product> |
------------------
| + getProductBySku(String): Product |
| + isInStock(String): boolean |
| + getPrice(String): double |
| + getAll(): List<Product> |
------------------
```
28 changes: 28 additions & 0 deletions src/main/java/com/booleanuk/core/Bagel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.booleanuk.core;

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

public class Bagel extends Item {
private double price;
private List<Filling> fillings = new ArrayList<>();

public Bagel(String sku, String name, String variant, double price) {
super(sku, name, variant, price);
this.price = price;
}


@Override
public double getPrice() {
return price;
}

public void addFilling(Filling filling) {
fillings.add(filling);
}

public List<Filling> getFillings() {
return fillings;
}
}
56 changes: 56 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.booleanuk.core;

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

public class Basket {
private List<Item> items;
private int capacity;

public Basket() {
this(5);
}

public boolean addItem(Item item) {
if (this.items.size() >= this.capacity) {
return false;
}
return this.items.add(item);
}

public List<Item> getItems() {
return this.items;
}

public boolean removeItem(Item item) {
return this.items.remove(item);
}

public Basket(int capacity) {
this.items = new ArrayList<>();
this.capacity = capacity;
}

public int getCapacity() {
return this.capacity;
}

public void setCapacity(int capacity) {
this.capacity = capacity;
}

public double getTotalCost(){
double total = 0.0;
for (Item item : this.items){
total += item.getPrice();

if (item instanceof Bagel) {
Bagel bagel = (Bagel) item;
for (Filling filling : bagel.getFillings()) {
total += filling.getPrice();
}
}
}
return total;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/booleanuk/core/Filling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.booleanuk.core;

public class Filling extends Item {
public Filling(String sku, String name, double price) {
super(sku, name, null, price);
}
}

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

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

public class Inventory {
private Map<String, Item> stock;

public Inventory() {
this.stock = new HashMap<>();
stock.put("BGLO", new Bagel("BGLO", "Bagel", "Onion", 0.49));
stock.put("BGLP", new Bagel("BGLP", "Bagel", "Plain", 0.39));
stock.put("BGLE", new Bagel("BGLE", "Bagel", "Everything", 0.49));
stock.put("BGLS", new Bagel("BGLS", "Bagel", "Sesame", 0.49));
stock.put("COFB", new Bagel("COFB", "Coffee", "Black", 0.99));
stock.put("COFW", new Bagel("COFW", "Coffee", "White", 1.19));
stock.put("COFC", new Bagel("COFC", "Coffee", "Capuccino", 1.29));
stock.put("COFL", new Bagel("COFL", "Coffee", "Latte", 1.29));
stock.put("FILB", new Filling("FILB", "Bacon", 0.12));
stock.put("FILE", new Filling("FILE", "Egg", 0.12));
stock.put("FILC", new Filling("FILC", "Cheese", 0.12));
stock.put("FILX", new Filling("FILX", "Cream Cheese", 0.12));
stock.put("FILS", new Filling("FILS", "Smoked Salmon", 0.12));
stock.put("FILH", new Filling("FILH", "Ham", 0.12));
}

public double getPriceBySku(String sku) {
Item item = stock.get(sku);
if (item != null) {
return item.getPrice();
}
throw new IllegalArgumentException("Item not found in inventory");
}
}
34 changes: 34 additions & 0 deletions src/main/java/com/booleanuk/core/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.booleanuk.core;

public abstract class Item implements Product {
protected String sku;
protected String name;
protected double price;
protected String variant;

public Item(String sku, String name, String variant, double price) {
this.sku = sku;
this.name = name;
this.variant = variant;
this.price = price;
}
@Override
public String getSku() {
return sku;
}

@Override
public String getName() {
return name;
}

@Override
public String getVariant() {
return variant;
}

@Override
public double getPrice() {
return price;
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/booleanuk/core/PricedItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.booleanuk.core;

public interface PricedItem {
double getPrice();
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/core/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core;

public interface Product extends PricedItem{
String getSku();
String getName();
String getVariant();
}
138 changes: 138 additions & 0 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.booleanuk.core;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;

public class BasketTest {
@Test
public void testAddBagelToBasket() {
Basket basket = new Basket();
Bagel bagel = new Bagel("BGLO", "Bagel", "Onion", 0.49);

boolean added = basket.addItem(bagel);

Assertions.assertTrue(added);
Assertions.assertEquals(1, basket.getItems().size());
Assertions.assertEquals("Onion", basket.getItems().get(0).getVariant());
}

@Test
public void testRemoveBagelFromBasket() {
Basket basket = new Basket();
Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49);
Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39);

basket.addItem(bagel1);
basket.addItem(bagel2);

boolean removed = basket.removeItem(bagel1);

Assertions.assertTrue(removed);
Assertions.assertEquals(1, basket.getItems().size());
Assertions.assertEquals("Plain", basket.getItems().get(0).getVariant());
}

@Test
public void testBasketIsFull() {
Basket basket = new Basket(2);
Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49);
Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39);
Bagel bagel3 = new Bagel("BGLE", "Bagel", "Everything", 0.49);

Assertions.assertTrue(basket.addItem(bagel1));
Assertions.assertTrue(basket.addItem(bagel2));
Assertions.assertFalse(basket.addItem(bagel3));
}

@Test
public void testChangeBasketCapacity() {
Basket basket = new Basket();

Assertions.assertEquals(5, basket.getCapacity());

basket.setCapacity(10);

Assertions.assertEquals(10, basket.getCapacity());
}

@Test
public void testRemoveNonexistentBagelFromBasket() {
Basket basket = new Basket();
Bagel onionBagel = new Bagel("BGLO", "Bagel", "Onion", 0.49);
Bagel plainBagel = new Bagel("BGLP", "Bagel", "Plain", 0.39);

basket.addItem(onionBagel);
boolean result = basket.removeItem(plainBagel);

Assertions.assertFalse(result);
Assertions.assertEquals(1, basket.getItems().size());
}

@Test
public void testGetTotalCostBasket(){
Basket basket = new Basket();

Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49);
Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39);
Bagel bagel3 = new Bagel("BGLE", "Bagel", "Everything", 0.49);
Bagel bagel4 = new Bagel("BGLS","Bagel", "Sesame",0.49);

basket.addItem(bagel1);
basket.addItem(bagel2);
basket.addItem(bagel3);
basket.addItem(bagel4);

double total = basket.getTotalCost();

Assertions.assertEquals(1.86, total, 0.001);

}

@Test
public void testGetBagelPriceFromInventory() {
Inventory inventory = new Inventory();
double price = inventory.getPriceBySku("BGLO"); // Onion Bagel

Assertions.assertEquals(0.49, price, 0.001);
}

@Test
public void testCannotAddMoreThanCapacity() {
Basket basket = new Basket(2); // max 2 items
Bagel bagel1 = new Bagel("BGLO", "Bagel", "Onion", 0.49);
Bagel bagel2 = new Bagel("BGLP", "Bagel", "Plain", 0.39);
Bagel bagel3 = new Bagel("BGLE", "Bagel", "Everything", 0.49);

Assertions.assertTrue(basket.addItem(bagel1));
Assertions.assertTrue(basket.addItem(bagel2));
Assertions.assertFalse(basket.addItem(bagel3));

Assertions.assertEquals(2, basket.getItems().size());
}

@Test
public void testGetFillingPriceBeforeAdding() {
Filling bacon = new Filling("FILB", "Bacon", 0.12);
Assertions.assertEquals("Bacon", bacon.getName());
Assertions.assertEquals(0.12, bacon.getPrice());
Assertions.assertEquals("FILB", bacon.getSku());
}

@Test
public void testAddFillingToBagel() {
Basket basket = new Basket();
Bagel bagel = new Bagel("BGLO", "Bagel", "Onion", 0.49);
Filling bacon = new Filling("FILB", "Bacon", 0.12);
Filling egg = new Filling("FILE", "Egg", 0.12);

bagel.addFilling(bacon);
bagel.addFilling(egg);
basket.addItem(bagel);

Assertions.assertEquals(2, bagel.getFillings().size());
Assertions.assertEquals(0.73, basket.getTotalCost(), 0.001); // 0.49 + 0.12 + 0.12
}


}
Loading