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
105 changes: 105 additions & 0 deletions model.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
```mermaid
classDiagram
class Inventory {
-Stock map<sku, Obejct[]>

+getPrice()
+getVarient()
+getName()
+getDiscountCondition()
+getDiscount()

}

class Stock {
hasItem(String sku)
getPrice(String sku)
getVarient(String sku)
getName(String sku)
getDiscountCondition(String sku)
etDiscount(String sku)
}

class Basket {
-int size
-Array items
-Inventory inventory

+Basket(Inventory)
+addBagel(Bagel) void
+addFilling(sku) void
+addCoffee(sku) void
+costOf(sku) float
+removeItem(sku) boolean
+totalCost()
+getBasket() String (what the basket has)

}

class Manager {
-name

+changeBasketSize() void
}

class Item {
-price
-sku
-varient
-name
-discountCondition
-discount
-inventory

+getters()

}

class Bagel{
-type
-fillings
-totalPrice

+addFilling() void
+removeFilling() void
+getPrice() float
+getSize() int

}

class Coffee{
-type

}

class Filling{
-type

}

class Customer{
-basket

+getBasket()

}


Customer o-- Basket : has a

Basket o-- Item : has

Manager o-- Basket : change values

Bagel o-- Item: extends
Coffee o-- Item: extends
Filling o-- Item: extends
Bagel o-- Filling: has

Item o-- Stock: uses
Basket o-- Stock: uses

Inventory o-- Stock: implements


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

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

import com.booleanuk.core.item_types.*;

public class Basket {
private static int size = 30;
private int curSize;
private Item[] arr;
private final Stock stock; // Should be static?

public Basket(Stock stock){
this.arr = new Item[Basket.size];
this.curSize = 0;
this.stock = stock;

}

public void addBagel(Bagel bag) throws Exception{ // only one using acuall bagel, only because bagel has its own "basket" of fillings
if(curSize + bag.getSize() >= getBasketSize()) // could do String... n
throw new Exception("Exceeds maximum basket size!");
this.arr[this.curSize] = bag;
this.curSize += bag.getSize() + 1; // + 1 for bagel
}

public void addCoffee(String sku) throws Exception{
if(curSize >= getBasketSize())
throw new Exception("Exceeds maximum basket size!");
if(!this.stock.hasItem(sku)){
throw new Exception("Not a valid SKU");
}
this.arr[this.curSize] = new Coffee(sku, this.stock);
this.curSize++;
}

public void addFilling(String sku) throws Exception{
if(curSize >= getBasketSize())
throw new Exception("Exceeds maximum basket size!");
if(!this.stock.hasItem(sku)){
throw new Exception("Not a valid SKU");
}
this.arr[this.curSize] = new Filling(sku, this.stock);
this.curSize++;
}

public boolean removeItem(String sku){
for (int i = 0; i < this.curSize; i++){
if(!(this.arr[i].getSKU() == sku)){
continue;
}
for (int j = i; j < this.curSize; j++){
if (j+1 >= this.curSize){
this.arr[j] = null;
break;
}
this.arr[j] = this.arr[j+1];
}
this.arr[i] = this.arr[i+1];
this.curSize--;
return true;

}
return false;
}

public float costOf(String sku){
return stock.getPrice(sku);
}

public int getBasketSize(){
return Basket.size;
}

protected static void setBasketSize(int size){
Basket.size = size;
}

private record Pair(int count, float discount){}

private String padding(int max, String str){
return " ".repeat((max/2) - (str.length()/2)) + str;
}

private String paddTwo(int max, String str1, String str2){
int padd = max- str1.length() - str2.length();
return str1 +" ".repeat(padd) + str2;
}

private String buildPrint(Map<String,Pair> skuDiscounts, float sum){
String print ="";
String bobString = "~~~ Bob's Bagels ~~~";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
String dateTime = LocalDateTime.now().format(formatter);
int maxStringSize = 28;
String div = "-".repeat(maxStringSize) + "\n";

print += padding(maxStringSize, bobString) + "\n" + padding(maxStringSize, dateTime) + "\n" + div;
String tmp = "";
float totDisc = 0f;
for (Map.Entry<String, Pair> entry : skuDiscounts.entrySet()) {
String sku = entry.getKey();
Pair pair = entry.getValue();
String nameVarient = this.stock.getName(sku) + " " + this.stock.getVarient(sku);
String price = "£" + String.format("%.2f", this.stock.getPrice(sku)*pair.count);
String amountPrice = paddTwo(10, ""+ pair.count, price);
tmp += paddTwo(maxStringSize, nameVarient, amountPrice)+ "\n";
Comment on lines +106 to +110
if(pair.discount != 0){
tmp += paddTwo(maxStringSize, "", "(-£" + Math.abs(pair.discount) + ")") + "\n";
totDisc += pair.discount;
}

}
print += tmp;
print += div + paddTwo(maxStringSize, "Total", "£"+ String.format("%.2f", sum))+ "\n";
print += "\n" + padding(maxStringSize, "You saved a total of " + "£"+ String.format("%.2f", Math.abs(totDisc))) + "\n";
print += "\n" + padding(maxStringSize, "Thank you for buying!");
return print;
}

public float getTotalCost(){
float sum = 0f;
Map<String, Pair> skuDiscounts = new HashMap<String, Pair>();

for (int i = 0; i < this.curSize; i++){
String sku = this.arr[i].getSKU();
int discountCondition = this.arr[i].getDiscountCondition();
boolean containsSKU = skuDiscounts.containsKey(sku);
// System.err.println(discountCondition);

if(!containsSKU){
skuDiscounts.put(sku, new Pair(1, 0f));
}else{
skuDiscounts.computeIfPresent(sku, (s, p) -> new Pair(p.count() + 1, p.discount()));
// System.err.println(skuDiscounts.get(sku).count);
}
if(containsSKU && discountCondition != 0 && skuDiscounts.get(sku).count % discountCondition == 0){
float discount = this.arr[i].getDiscount();
skuDiscounts.computeIfPresent(sku, (s, p) -> new Pair(p.count(), p.discount() + discount));
sum += discount;
}
sum += this.arr[i].getPrice();
}

System.out.println(this.buildPrint(skuDiscounts, sum));
// System.err.println(this.buildPrint(skuDiscounts, sum));
return sum;
}

@Override
public String toString(){
String tmp = "";
for (int i = 0; i < this.curSize; i++){
tmp += this.arr[i].getSKU() + " " + this.arr[i].getName() + " " + this.arr[i].getVarient() + "\n";
}
return tmp.trim();
}

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

public class Customer {
private Basket basket;

public Customer(Inventory inventory){
this.basket = new Basket(inventory);
}

public Basket getBasket(){
return this.basket;
}
}
78 changes: 78 additions & 0 deletions src/main/java/com/booleanuk/core/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.booleanuk.core;

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

public class Inventory implements Stock{
private Map<String, Object[]> inventory;

public Inventory(){
this.inventory = new HashMap<String, Object[]>();
// this.inventory.put("BGLO", new Object[]{0.49f, "Bagel", "Onion", 6, -0.45f});
// this.inventory.put("BGLP", new Object[]{0.39f, "Bagel", "Plain", 12, -0.69f});
// this.inventory.put("BGLE", new Object[]{0.49f, "Bagel", "Everything", 6, -0.45f});
// this.inventory.put("BGLS", new Object[]{0.49f, "Bagel", "Sesame", null});
// this.inventory.put("COFB", new Object[]{0.99f, "Coffee", "Black", null}); // says there should be a bagel + coffee deal but then the example orders does not show it so skipping...
// this.inventory.put("COFW", new Object[]{1.19f, "Coffee", "White", null});
// this.inventory.put("COFC", new Object[]{1.29f, "Coffee", "Capuccino", null});
// this.inventory.put("COFL", new Object[]{1.29f, "Coffee", "Latte", null});
// this.inventory.put("FILB", new Object[]{0.12f, "Filling", "Bacon", null});
// this.inventory.put("FILE", new Object[]{0.12f, "Filling", "Egg", null});
// this.inventory.put("FILC", new Object[]{0.12f, "Filling", "Cheese", null});
// this.inventory.put("FILX", new Object[]{0.12f, "Filling", "Cream Cheese", null});
// this.inventory.put("FILS", new Object[]{0.12f, "Filling", "Smoked Salmon", null});
// this.inventory.put("FILH", new Object[]{0.12f, "Filling", "Ham", null});
}

@Override
public float getPrice(String sku){
return (float) this.inventory.get(sku)[0];
}

@Override
public String getVarient(String sku){
return (String) this.inventory.get(sku)[1];
}
Comment on lines +28 to +35

@Override
public String getName(String sku){
return (String) this.inventory.get(sku)[2];
}

@Override
public int getDiscountCondition(String sku){
Object dsc = this.inventory.get(sku)[3];
if(dsc != null){
return (int) dsc;
}else{
return 0;
}
}

@Override
public float getDiscount(String sku){
Object dsc = this.inventory.get(sku)[3];
if(dsc != null){
return (float) this.inventory.get(sku)[4];
}else{
return 0f;
}
}

public void addEntry(String sku, float price, String varient, String name, int discountCondition, float discount){
this.inventory.put(sku, new Object[]{price, varient, name, discountCondition, discount});
}

public void addEntry(String sku, float price, String varient, String name){
this.inventory.put(sku, new Object[]{price, varient, name, null});
}

public void removeEntry(String sku){
this.inventory.remove(sku);
}

@Override
public boolean hasItem(String sku){
return this.inventory.containsKey(sku);
}
}
Loading
Loading