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
Binary file added BasketClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added InventoryClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ItemClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions domain-model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

## Item Class

| Methods | Variables | Scenario | Output |
|------------------------|-------------------|------------------------------------|----------------|
| | `String sku ` | | |
| | `String name ` | | |
| | `String variant ` | | |
| | `float price ` | | |
| `String getSku() ` | | Want to get the id of an item | Return sku |
| `String getName() ` | | Want to get the name of an item | Return name |
| `String getVariant() ` | | Want to get the variant of an item | Return variant |
| `float getPrice() ` | | Want to get the price of an item | Return price |
| | | | |
| | | | |
| | | | |


## Basket Class

| Methods | Variables | Scenario | Output |
|------------------------------------|--------------------------|--------------------------------------|--------------------------------|
| | `ArrayList<Item> items ` | | |
| | `boolean isFull ` | | |
| | `int capacity ` | | |
| | | | |
| `void addItem(String itemSku) ` | | Want to add item to basket with room | Adds item to basket |
| | | Want to add item to full basket | Informs about full basket |
| `void removeItem(String itemSku) ` | | Want to remove existing item | Removes item from basket |
| | | Want to remove non-existent item | Inform about non-existing item |
| `void checkCapacity() ` | | Want to check if basket is full | Updated isFull if needed |
| `void changeCapacity(int newCap) ` | | Want to change basket capacity | Change capacity to newCap |
| `float totalCost() ` | | Want to know cost of basket | Add together and return sum |
| `boolean containsItem() ` | | Item is in basket | True |
| | | Item is not in basket | False |
| `boolean containsItem() ` | | Item is in basket | True |


## Inventory Class

| Methods | Variables | Scenario | Output |
|---------------------------------------|----------------------------------------------|------------------------------------|---------------------------------|
| | `HashMap<String item, int stock> inventory ` | | |
| `void addItem(String itemSku) ` | | Want to add item to inventory | Updates inventory |
| `void removeItem(String itemSku) ` | | Want to remove item from inventory | Updates inventory |

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

import java.util.ArrayList;
import java.util.Scanner;

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

public Basket(Inventory inventory){
this.isFull = false;
this.inventory = inventory;
}

public Inventory getInventory(){
return this.inventory;
}

public ArrayList<Item> getItems(){
return items;
}

public boolean getIsFull(){
return isFull;
}

public int getCapacity(){
return capacity;
}

public void checkCapacity(){
if (items.size() == capacity){
isFull = true;
return;
}
isFull = false;
}

public boolean containsItem(String itemSku){
for (Item item : items){
if (item.getSku().equals(itemSku)){
return true;
}
}
return false;
}

public void addItem(String itemSku){
checkCapacity();
if (isFull){
System.out.println("Your basket is full, cant add item!");
return;
}
if (inventory.getItemStock(itemSku) == 0){
System.out.println("No stock left for chosen item!");
return;
}
Item item = new Item(itemSku);
System.out.println(item.getVariant() + " " + item.getName() + " price: $" + item.getPrice());
System.out.println("Do you want to add " + item.getVariant()
+ " " + item.getName() +" to the basket? (yes/no): ");

Scanner scanner = new Scanner(System.in);
String userInput = scanner.nextLine().trim().toLowerCase();

if (userInput.equals("yes")) {
items.add(item);
inventory.removeStockItem(itemSku, 1);
System.out.println(item.getVariant() + " " + item.getName() + " added to the basket.");
} else {
System.out.println(item.getVariant() + " " + item.getName() + " not added to the basket.");
}
}

public void removeItem(String itemSku){
for (Item item : items){
if (item.getSku().equals(itemSku)){
items.remove(item);
inventory.addStockItem(itemSku, 1);
return;
}
}
System.out.println("This item does not exist in the basket!");
}

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

public float totalCost(){
float total = 0;
for (Item item : items){
total += item.getPrice();
}
return total;
}
}
41 changes: 41 additions & 0 deletions src/main/java/com/booleanuk/core/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.booleanuk.core;

import java.util.HashMap;

public class Inventory {
private HashMap<String, Integer> stock = new HashMap<>();


public Inventory(){

}

public int getStockSize(){
return this.stock.size();
}

public int getItemStock(String sku){
return this.stock.get(sku);
}

public void addStockItem(String sku, int amount){
if (!stock.containsKey(sku)){
stock.put(sku, amount);
return;
}
stock.put(sku, stock.get(sku) + amount);
}

public void removeStockItem(String sku, int amount){
if (stock.containsKey(sku) && stock.get(sku) >= amount){
stock.put(sku, stock.get(sku) - amount);
if (stock.get(sku) == 0){
stock.remove(sku);
}
return;
}
System.out.println("Not enough stock in inventory!");
}


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

public class Item {
private String sku;
private String name;
private String variant;
private float price;

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

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

public String getSku() {return sku;}

public String getName() {return name;}

public String getVariant() {return variant;}

public float getPrice() {return price;}
}
Loading
Loading