Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ gradle-app.setting
# JDT-specific (Eclipse Java Development Tools)
.classpath

.idea
.idea

bin/
.settings/
Binary file added class-diagrams.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Domain model

## Item

| Method | Variable | Scenario | Output |
| ------------ | -------------- | -------- | ------------ |
| | SKU sku | | |
| | float price | | |
| | String type | | |
| | String variant | | |
| getSku() | | | return sku |
| getPrice() | | | return price |
| getType() | | | return type |
| getVariant() | | | return price |

## Basket

| Method | Variable | Scenario | Output |
| ------------------- | ---------------------- | ------------------------------------------- | ----------------------------------- |
| | Inventory inventory | | |
| | int capacity | | |
| | ArrayList\<Item> items | | |
| addItem(Item item) | | basket is not full and item is in inventory | add item to items, return true |
| addItem(Item item) | | item is not in inventory | return false |
| addItem(Item item) | | basket is full | return false |
| removeItem(SKU sku) | | item exists in basket | remove item from items, return true |
| removeItem(SKU sku) | | item doesn't exist in basket | return false |
| getTotalCost() | | | return total cost of items |
| getItems() | | | return items |

## Inventory

| Method | Variable | Scenario | Output |
| -------------------------------- | ---------------------------- | ----------------- | ----------------------------------- |
| | HashMap<SKU, Integer> invMap | | |
| checkStock(SKU sku) | | in stock | return true |
| checkStock(SKU sku) | | not in stock | return false |
| addStock(SKU sku, int amount) | | amount > 0 | add item to invMap, return true |
| addStock(SKU sku, int amount) | | amount <= 0 | return false |
| removeStock(SKU sku, int amount) | | item in stock | remove item from stock, return true |
| removeStock(SKU sku, int amount) | | item not in stock | return false |

## Receipt

### User stories

As a member of the public,
So I can assess the damage after paying,
I'd like to see the total cost of my order on the receipt.

As a member of the public,
So I can check if my order was handled correctly,
I'd like to see all items in my order listed on the receipt.

As a member of the public,
So I can double check the total cost,
I'd like to see the individual price of every item listed on the receipt.

As a member of the public,
So I can keep my finances in order,
I'd like to see the time of my purchase listed on the receipt.

| Method | Variable | Scenario | Output |
| -------------- | ---------------- | -------- | ------------------------- |
| | Basket basket | | |
| | String time | | |
| printReceipt() | | | print receipt to terminal |
| getReceipt() | | | return receipt String |
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/core/Bagel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core;

public class Bagel extends Item {
public Bagel(SKU sku) {
super(sku);
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.booleanuk.core;

import java.util.ArrayList;

public class Basket {
private Inventory inventory;
private int capacity;
private ArrayList<Item> items = new ArrayList<>();

public Basket(Inventory inventory, int capacity) {
this.inventory = inventory;
this.capacity = capacity;
}

boolean addItem(Item item) {
// if basket is not full and item is in stock
if (items.size() < capacity && inventory.checkStock(item.getSku())) {
items.add(item);
inventory.removeStock(item.getSku(), 1); // remove item from stock
return true;
}
return false;
}

boolean removeItem(SKU sku) {
for (int i = 0; i < items.size(); i++) {
if (items.get(i).getSku() == sku) {
items.remove(i);
inventory.addStock(sku, 1);
return true;
}
}
return false;
}

float getTotalCost() {
float total = 0f;
for (Item i : items) {
total += i.getPrice();
}
return total;
}

ArrayList<Item> getItems() {
return items;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/core/Coffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core;

public class Coffee extends Item {
public Coffee(SKU sku) {
super(sku);
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/core/Filling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core;

public class Filling extends Item {
public Filling(SKU sku) {
super(sku);
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/booleanuk/core/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.booleanuk.core;

import java.util.HashMap;

public class Inventory {
private HashMap<SKU, Integer> invMap = new HashMap<>();

boolean checkStock(SKU sku) {
if (invMap.containsKey(sku) && invMap.get(sku) > 0) {
return true;
}
return false;
}

boolean addStock(SKU sku, int amount) {
if (amount <= 0) {
return false;
}
// if sku already in stock
if(invMap.containsKey(sku)) {
invMap.put(sku, invMap.get(sku) + amount);
}
// if sku not in stock
else {
invMap.put(sku, amount);
}
return true;
}

boolean removeStock(SKU sku, int amount) {
// if amount is more than in stock return false
if (!checkStock(sku) || amount > invMap.get(sku)) {
return false;
}

invMap.put(sku, invMap.get(sku) - amount);
return true;
}

}
89 changes: 89 additions & 0 deletions src/main/java/com/booleanuk/core/Item.java

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be difficult to maintain. Consider usg something like the factory pattern to refactor this

Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.booleanuk.core;

public abstract class Item {
private SKU sku;
private float price;
private String type;
private String variant;

public Item(SKU sku) {
this.sku = sku;

// Set price, type, and variant based on SKU
if (sku == SKU.BGLO) {
price = 0.49f;
type = "Bagel";
variant = "Onion";
} else if (sku == SKU.BGLP) {
price = 0.39f;
type = "Bagel";
variant = "Plain";
} else if (sku == SKU.BGLE) {
price = 0.49f;
type = "Bagel";
variant = "Everything";
} else if (sku == SKU.BGLS) {
price = 0.49f;
type = "Bagel";
variant = "Sesame";
} else if (sku == SKU.COFB) {
price = 0.99f;
type = "Coffee";
variant = "Black";
} else if (sku == SKU.COFW) {
price = 1.19f;
type = "Coffee";
variant = "White";
} else if (sku == SKU.COFC) {
price = 1.29f;
type = "Coffee";
variant = "Capuccino";
} else if (sku == SKU.COFL) {
price = 1.29f;
type = "Coffee";
variant = "Latte";
} else if (sku == SKU.FILB) {
price = 0.12f;
type = "Filling";
variant = "Bacon";
} else if (sku == SKU.FILE) {
price = 0.12f;
type = "Filling";
variant = "Egg";
} else if (sku == SKU.FILC) {
price = 0.12f;
type = "Filling";
variant = "Cheese";
} else if (sku == SKU.FILX) {
price = 0.12f;
type = "Filling";
variant = "Cream Cheese";
} else if (sku == SKU.FILS) {
price = 0.12f;
type = "Filling";
variant = "Smoked Salmon";
} else if (sku == SKU.FILH) {
price = 0.12f;
type = "Filling";
variant = "Ham";
} else {
throw new IllegalArgumentException("Invalid SKU: " + sku);
}
}

SKU getSku() {
return sku;
}

float getPrice() {
return price;
}

String getType() {
return type;
}

String getVariant() {
return variant;
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/booleanuk/core/SKU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.booleanuk.core;

public enum SKU {
BGLO,
BGLP,
BGLE,
BGLS,
COFB,
COFW,
COFC,
COFL,
FILB,
FILE,
FILC,
FILX,
FILS,
FILH,
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/extension/Bagel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.extension;

public class Bagel extends Item {
public Bagel(SKU sku) {
super(sku);
}
}
Loading
Loading