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
104 changes: 104 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.booleanuk.core;
import com.booleanuk.core.Item.Item;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

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

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

public boolean add(Item item){
if (!inventory.exists(item)){
System.out.println("The item is out of stock!");
return false;
} else if (isFull()){
System.out.println("The basket is full!");
return false;
} else {
items.add(item);
inventory.reduceCount(item);
return true;
}
}

public boolean remove(Item item){
if (items.contains(item)){
items.remove(item);
inventory.increaseCount(item);
return true;
} else {
System.out.println("This item does not exist in your basket!");
return false;
}
}

public boolean isFull() {
return items.size() >= capacity;
}

public double total(){
return items.stream().map(Item::getPrice).reduce(0.0, Double::sum) - findDiscount();
}

public double findDiscount(){
double totalDiscount = 0.0;

int bagelsPlainCount = Math.toIntExact(items.stream().filter(item -> item.getSku().equals("BGLP")).count());
int bagelsNotPlainCount = Math.toIntExact(items.stream().filter(item -> !item.getSku().equals("BGLP")).count());
int coffeeBlackCoffeeCount = Math.toIntExact(items.stream().filter(item -> item.getSku().equals("COFB")).count());
if (bagelsPlainCount >= 12) {
// adding the difference between normal price of 6 plain bagels vs discounted price
totalDiscount += 0.69;
}
if (bagelsNotPlainCount >= 6){
totalDiscount += 0.45;
}
if (bagelsPlainCount > 0 && coffeeBlackCoffeeCount > 0){
totalDiscount += 0.13;
} else if (bagelsNotPlainCount > 0 && coffeeBlackCoffeeCount > 0){
totalDiscount += 0.23;
}
return totalDiscount;
}

public void printReceipt(){
// A little bit messy, but I didn't want to rewrite the other parts of the code
// to be able to simplify this one, but it works.
// There is definitely a better way to structure the print-statements,
// but I figured that weren't the main objective of the task anyway.

HashMap<String, Integer> res = new HashMap<>();
for (Item item : items){
res.put(item.getType(), res.getOrDefault(item.getType(), 0) + 1);
}
LocalDateTime currentDateTime = LocalDateTime.now();

String firstPart =
"\n\n ~~~ Bob's Bagels ~~~\n\n " + currentDateTime + "\n\n" +
"-------------------------------------" + "\n";

System.out.println(firstPart);
res.forEach((key,val) -> {
double price = items.stream().filter(itm -> itm.getType().equals(key)).toList().getFirst().getPrice()*val;
System.out.println(key + " " + " " + val + " £" + price);
});
String lastPart =
"-------------------------------------" + "\n" +
"Sum £" + String.format("%.2f", total()+findDiscount()) + "\n" +
"Discount (-£" + String.format("%.2f", findDiscount()) + ")\n" +
"Total £" + String.format("%.2f", total()) +
"\n\nYou saved a total of: £" + String.format("%.2f", findDiscount()) +
"\n\n Thank you \n for your order!\n\n";

System.out.println(lastPart);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/booleanuk/core/BasketCapacity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.booleanuk.core;

// Basically the class I used as a "global" variable to be used as the capacity
// values of baskets once they're created. The logic is that every basket that is already
// in use by the time this value changes, will remain at the previous capacity-value,
// while new baskets will receive the updated capacity-value, which made sense to me.

public class BasketCapacity {
public int basketCapacity;

public BasketCapacity(int capacity){
this.basketCapacity = capacity;
}

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

public boolean setCapacity(int capacity){
if (capacity != this.basketCapacity){
this.basketCapacity = capacity;
return true;
} else {
return false;
}
}
}
Binary file added src/main/java/com/booleanuk/core/ClassDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions src/main/java/com/booleanuk/core/Inventory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.booleanuk.core;
import com.booleanuk.core.Item.Item;
import java.util.HashMap;

public class Inventory {
HashMap<String, Integer> mapOfItems;

public Inventory(HashMap<String, Integer> mapOfItems){
this.mapOfItems = mapOfItems;
}

public void increaseCount(Item item){
mapOfItems.put(item.getType(), mapOfItems.getOrDefault(item.getType(), 0) + 1);
}

public boolean reduceCount(Item item){
if (!mapOfItems.containsKey(item.getType())){
return false;
} else {
if (mapOfItems.get(item.getType()) < 1) {
return false;
}
else {
mapOfItems.put(item.getType(), mapOfItems.getOrDefault(item.getType(), 0) - 1);
return true;
}
}
}

public boolean exists(Item item){
return mapOfItems.containsKey(item.getType()) && mapOfItems.get(item.getType()) > 0;
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/booleanuk/core/Item/Bagel/Bagel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.booleanuk.core.Item.Bagel;
import com.booleanuk.core.Item.Filling.Filling;
import com.booleanuk.core.Item.Item;
import java.util.ArrayList;
import java.util.List;

public class Bagel implements Item {
private final List<Filling> filling;
private final double price;
private final String type;
private final String sku;

public Bagel(BagelType type){
this.price = type.equals(BagelType.PLAIN)? 0.39 : 0.49;
filling = new ArrayList<>();
this.type = type.toString();
this.sku = switch(type){
case PLAIN -> "BGLP";
case ONION -> "BGLO";
case EVERYTHING -> "BGLE";
case SESAME -> "BGLS";
};
}

public String getSku(){
return this.sku;
}

public double getPrice(){
return this.price;
}

public String getType(){
return this.type;
}

public List<Filling> getFilling(){
return this.filling;
}

public void addFilling(Filling filling){
this.filling.add(filling);
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/booleanuk/core/Item/Bagel/BagelType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.booleanuk.core.Item.Bagel;

public enum BagelType{
ONION,
PLAIN,
EVERYTHING,
SESAME
}
36 changes: 36 additions & 0 deletions src/main/java/com/booleanuk/core/Item/Coffee/Coffee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.booleanuk.core.Item.Coffee;
import com.booleanuk.core.Item.Item;

public class Coffee implements Item {
private final double price;
private final String type;
private final String sku;

public Coffee(CoffeeType type){
this.price = switch(type){
case BLACK -> 0.99;
case WHITE -> 1.19;
default -> 1.29;
};
this.type = type.toString();
this.sku = switch(type){
case BLACK -> "COFB";
case WHITE -> "COFW";
case CAPPUCCINO -> "COFC";
case LATTE -> "COFL";
};
}

public String getSku(){
return this.sku;
}

public double getPrice(){
return this.price;
}

@Override
public String getType(){
return this.type;
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/booleanuk/core/Item/Coffee/CoffeeType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.booleanuk.core.Item.Coffee;

public enum CoffeeType{
BLACK,
WHITE,
CAPPUCCINO,
LATTE
}
34 changes: 34 additions & 0 deletions src/main/java/com/booleanuk/core/Item/Filling/Filling.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.booleanuk.core.Item.Filling;
import com.booleanuk.core.Item.Item;

public class Filling implements Item {
private final double price;
private final String type;
private final String sku;

public Filling(FillingType type){
this.price = 0.12;
this.type = type.toString();
this.sku = switch(type){
case BACON -> "FILB";
case EGG -> "FILE";
case CHEESE -> "FILC";
case CREAM_CHEESE -> "FILX";
case SMOKED_SALMON -> "FILS";
case HAM -> "FILH";
};
}

public String getSku(){
return this.sku;
}

public double getPrice(){
return this.price;
}

@Override
public String getType(){
return this.type;
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/booleanuk/core/Item/Filling/FillingType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.booleanuk.core.Item.Filling;

public enum FillingType{
BACON,
EGG,
CHEESE,
CREAM_CHEESE,
SMOKED_SALMON,
HAM
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/core/Item/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.core.Item;

public interface Item {
double getPrice();
String getType();
String getSku();
}
15 changes: 15 additions & 0 deletions src/main/java/com/booleanuk/core/Person/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.booleanuk.core.Person;
import com.booleanuk.core.Basket;

public class Customer extends Member{
private final int customerId;

public Customer(String name, int id, Basket basket, int customerId) {
super(name, id, basket);
this.customerId = customerId;
}

public int getCustomerId(){
return this.customerId;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/booleanuk/core/Person/Manager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.booleanuk.core.Person;
import com.booleanuk.core.BasketCapacity;

public class Manager extends Person{

public Manager(String name) {
super(name);
}

public boolean changeBasketCapacity(BasketCapacity basketCapacity, int capacity){
return basketCapacity.setCapacity(capacity);
}
}
44 changes: 44 additions & 0 deletions src/main/java/com/booleanuk/core/Person/Member.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.booleanuk.core.Person;
import com.booleanuk.core.Basket;
import com.booleanuk.core.Item.Bagel.Bagel;
import com.booleanuk.core.Item.Filling.Filling;
import com.booleanuk.core.Item.Item;

public class Member extends Person{
private final int id;
private final Basket basket;

public Member(String name, int id, Basket basket) {
super(name);
this.id = id;
this.basket = basket;
}

public int getMemberId(){
return this.id;
}

public boolean addItemToBasket(Item item){
return basket.add(item);
}

public boolean removeItemFromBasket(Item item){
return basket.remove(item);
}

public double totalCost(){
return basket.total();
}

public double getPriceOfItem(Item item){
return item.getPrice();
}

public void chooseFillingForBagel(Bagel bagel, Filling filling){
bagel.addFilling(filling);
}

public void printReceiptFromBasket(){
basket.printReceipt();
}
}
Loading
Loading