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
69 changes: 69 additions & 0 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.booleanuk.core;

import com.booleanuk.core.Products.Bagel;

import java.util.ArrayList;

public class Basket {
private ArrayList<Bagel> bagels = new ArrayList<>();
private String basketType;
private int capacity;

public Basket(String basketType, int capacity) {
setBasketType(basketType);
setCapacity(capacity);
}

public String getBasketType() {
return basketType;
}

public void setBasketType(String basketType) {
if (basketType == null || basketType.toLowerCase().trim().isEmpty())
basketType = "small";

this.basketType = basketType;
}

public int getCapacity() {
return capacity;
}

public boolean isFull() {
return bagels.size() == this.capacity;
}

public void setCapacity(int capacity) {
if (capacity <= 0)
capacity = 5;

this.capacity = capacity;
}

public ArrayList<Bagel> getBagels() {
return bagels;
}

// public void setBagels(ArrayList<Bagel> bagels) {
// this.bagels = bagels;
// }

public boolean addBagel(Bagel bagel) {
if (!isFull())
return bagels.add(bagel);
return false;
}

public boolean removeBagel(Bagel bagel) {
return bagels.remove(bagel);
}

public float getTotalCost() {
float totCost = 0;
for (Bagel bagel: bagels)
totCost += bagel.getPrice();

return totCost;
}

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

import com.booleanuk.core.Products.Bagel;
import com.booleanuk.core.Products.Filling;
import com.booleanuk.core.Products.Product;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

public class Customer {
private Basket basket = null;
// private Inventory inventory = null;
private List<Product> products = null;

public Customer(Basket basket, List<Product> inventory) {
this.basket = basket;
this.products = inventory;
}

public void addBagelToBasket(Bagel bagel) {
for (Product p: products)
if (p.getSku().equals(bagel.getSku())) {
if (basket.addBagel(bagel)) {
System.out.println("Added to basket.");
break;
} else
System.out.println("Basket is full, you cant add more bagelZ");
}
}

public void removeBagelFromBasket(Bagel bagel) {
if (basket.removeBagel(bagel))
System.out.println("Bagel removed from basket");
else
System.out.println("Nothing to remove.");
}

public float getTotalCost() {
ArrayList<Bagel> bagels = basket.getBagels();

float totCost = 0;
for (Bagel bagel : bagels)
totCost += bagel.getPrice();

return totCost;
}

public float getBagelPrice(Bagel bagel) {
ArrayList<Bagel> bagels = basket.getBagels();
for (Bagel b : bagels)
if (b.getSku().equals(bagel.getSku()))
return b.getPrice();

return -1;
}

public void viewFillingsAndPrice() {
for (Product product: products)
if (product.getType() == Product.ProductType.FILLING) {
System.out.printf("Filling: %-20s --> %.2f SEK\n", product.getVariant(), product.getPrice());
}
}

public Bagel getBagelAndPickFilling(String filling){
Filling fill = (Filling) products.stream()
.filter(product -> product.getType() == Product.ProductType.FILLING &&
product.getVariant().equalsIgnoreCase(filling.trim()))
.map(product -> (Filling) product).findFirst().orElse(null);

Bagel bagel = (Bagel) products.stream()
.filter(product -> product.getType() == Product.ProductType.BAGEL &&
product.getVariant().equalsIgnoreCase("plain"))
.map(product -> (Bagel) product).findFirst().orElse(null);

if (bagel != null && fill != null)
bagel.setFilling(fill);
basket.addBagel(bagel);
System.out.printf("Bagel with %s added. \n", fill.getVariant());

// bagel.setFilling(fill);
// basket.addBagel(bagel);

return bagel;
}

public String orderBagelAtSpecificTime(Time time, String bagelType) {

Bagel bagel = (Bagel) products.stream().filter(product -> product.getVariant()
.equalsIgnoreCase(bagelType))
.map(product -> (Bagel) product).findFirst().orElse(null);

return (bagel != null) ? "Order: " + time + ", Bagel: " + bagel.toString() : "Camon little boy";
}

public int getAmountOfBagelsInBasket() {
return this.basket.getBagels().size();
}

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

import com.booleanuk.core.Products.Product;

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

public class Inventory {
private List<Product> products = new ArrayList<>();

public void addProduct(Product product) {
products.add(product);
}

public void removeProduct(Product product) {
products.remove(product);
}

public List<Product> getInventoryList() {
return this.products;
}

public Product getProduct(Product.ProductType type, String variant) {
for (Product p: products)
if (p.getType() == type && p.getVariant().toLowerCase().equals(variant.toLowerCase().trim()))
return p;
return null;
}

public void printInventory() {
for (Product p : products) {
System.out.println(p);
// if (p instanceof Bagel) {
// System.out.println("ISSA BAGEL");
// }
}
}

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

import com.booleanuk.core.Products.Bagel;
import com.booleanuk.core.Products.Filling;
import com.booleanuk.core.Products.Product;

import java.sql.Time;
import java.util.List;

public class Main {
public static void main(String[] args) {
System.out.println("\n");
// Load inventory on start
Inventory inv = loadInventoryOnStart();
List<Product> list = inv.getInventoryList();
// inv.printInventory();

Basket basket = new Basket("small", 5);
Customer customer = new Customer(basket, list);

Bagel bagel1 = new Bagel("BGLO", 0.49f, "Onion", Product.ProductType.BAGEL);
Bagel bagel2 = new Bagel("BGLP", 0.39f, "Plain", Product.ProductType.BAGEL);
Bagel bagel3 = new Bagel("BGLE", 0.49f, "Everything", Product.ProductType.BAGEL);
Bagel bagel4 = new Bagel("BGLS", 0.49f, "Sesame", Product.ProductType.BAGEL);
Bagel bagel5 = new Bagel("BGLS", 0.49f, "Sesame", Product.ProductType.BAGEL);
Bagel bagel6 = new Bagel("BGLS", 0.49f, "Sesame", Product.ProductType.BAGEL);

// Story 1:
Time time = Time.valueOf("07:45:00");
String s = customer.orderBagelAtSpecificTime(time, "everything");
System.out.println(s);

// Story 2:
customer.addBagelToBasket(bagel1);
customer.removeBagelFromBasket(bagel1);

// Story 3:
customer.addBagelToBasket(bagel1);
customer.addBagelToBasket(bagel2);
customer.addBagelToBasket(bagel3);
customer.addBagelToBasket(bagel4);
customer.addBagelToBasket(bagel5);
customer.addBagelToBasket(bagel6); // full

// Story 4:
Manager manager = new Manager(inv);
manager.changeBasketCapacity(basket, 10);
customer.addBagelToBasket(bagel6); // not full anymore

// Story 5:
customer.removeBagelFromBasket(bagel1); // should remove
customer.removeBagelFromBasket(bagel1); // nothing to remove

// Story 6:
System.out.println(customer.getTotalCost());

// Story 7:
System.out.println(customer.getBagelPrice(bagel4));

// Story 8 and 9:
customer.viewFillingsAndPrice();
customer.getBagelAndPickFilling("cheese");

// Story 10:
Bagel test = new Bagel("sku123", 10f, "test", Product.ProductType.BAGEL);
Bagel test2 = new Bagel("sku999", 10f, "test", Product.ProductType.BAGEL);
manager.addItemToInv(test);
customer.addBagelToBasket(test); // should work
customer.addBagelToBasket(test2); // should not work
}

public static Inventory loadInventoryOnStart() {
// Add all items to the inventory
Inventory inventory = new Inventory();
// Add Bagels
inventory.addProduct(new Bagel("BGLO", 0.49f, "Onion", Product.ProductType.BAGEL));
inventory.addProduct(new Bagel("BGLP", 0.39f, "Plain", Product.ProductType.BAGEL));
inventory.addProduct(new Bagel("BGLE", 0.49f, "Everything", Product.ProductType.BAGEL));
inventory.addProduct(new Bagel("BGLS", 0.49f, "Sesame", Product.ProductType.BAGEL));

// Add Fillings
inventory.addProduct(new Filling("FILB", 0.12f, "Bacon", Product.ProductType.FILLING));
inventory.addProduct(new Filling("FILE", 0.12f, "Egg", Product.ProductType.FILLING));
inventory.addProduct(new Filling("FILC", 0.12f, "Cheese", Product.ProductType.FILLING));
inventory.addProduct(new Filling("FILX", 0.12f, "Cream Cheese", Product.ProductType.FILLING));
inventory.addProduct(new Filling("FILS", 0.12f, "Smoked Salmon", Product.ProductType.FILLING));
inventory.addProduct(new Filling("FILH", 0.12f, "Ham", Product.ProductType.FILLING));

// Add Coffees

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


import com.booleanuk.core.Products.Product;

public class Manager {
private Inventory inventory;

public Manager(Inventory inventory) {
this.inventory = inventory;
}

public void changeBasketCapacity(Basket basket, int num) {
basket.setCapacity(num);
}

public void addItemToInv(Product product) {
inventory.addProduct(product);
}

public void removeItemToInv(Product product) {
inventory.removeProduct(product);
}

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

public class Bagel extends Product {
private Filling filling;

public Bagel(String sku, float price, String variant, ProductType type) {
super(sku, price, variant, type);

setPrice(price);
}

public void setPrice(float price) {
if (price <= 0)
throw new IllegalArgumentException("Price must be more than 0");

super.setPrice(price);
}

@Override
public float getPrice() {
if (filling != null)
return super.getPrice() + filling.getPrice();
else
return super.getPrice();
}

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

public void setFilling(Filling filling) {
this.filling = filling;
}

public float getFillingCost() {
return (this.filling != null) ? this.filling.getPrice() : 0f;
}

@Override
public String toString() {
return this.getVariant();
}
}
Loading
Loading