Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stian Rusvik #123

Open
wants to merge 38 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
599f764
added setup code and made first test fail.
Aug 16, 2024
f3518f6
made first test pass.
Aug 16, 2024
6845820
updated test.
Aug 19, 2024
f16cae3
made remove products fail.
Aug 19, 2024
ffb6dd4
made remove products pass.
Aug 19, 2024
6c51982
updated remove product method.
Aug 19, 2024
168153a
made is full test fail.
Aug 19, 2024
db0ee7a
made is full test pass.
Aug 19, 2024
2f6db26
updated custom exception to inherit from the Exception class.
Aug 19, 2024
fd0258e
made change capacity test fail.
Aug 19, 2024
b28f272
made change capacity test pass.
Aug 19, 2024
79e2e65
made remove product when product does not exists fail.
Aug 19, 2024
ed32430
made remove product when product does not exists pass.
Aug 19, 2024
cb1e84b
.
Aug 19, 2024
567f832
wrote test for get total cost in basket.
Aug 20, 2024
f51fa72
created a product factory.
Aug 20, 2024
d641dbb
lots of refactoring to make the tests adapt to the new factory.
Aug 20, 2024
5997685
more refactoring of existing code.
Aug 20, 2024
8f1e681
added builder pattern to Bagel for fillings.
Aug 20, 2024
0ac043a
made testCreateBagelWithFillings fail.
Aug 20, 2024
32f6565
made testCreateBagelWithFillings pass.
Aug 20, 2024
c710111
made testPriceOfFilling fail.
Aug 20, 2024
5aa099d
made testPriceOfFilling pass.
Aug 20, 2024
093e33d
finished core exercises.
Aug 20, 2024
c9fceb1
lots of work
Aug 21, 2024
8a85f6a
lots of bug hunting!
Aug 22, 2024
aac70b0
wrote test on first discount - Extension 1
Aug 22, 2024
86f63df
wrote test on second discount - Extension 1
Aug 22, 2024
f2da9bc
wrote test on third discount - Extension 1
Aug 22, 2024
0ca633f
wrote some more tests for Extension 1
Aug 22, 2024
7efa7a4
trying to solve difficult bug.
Aug 22, 2024
022e75a
removed code that broke the tests.
Aug 22, 2024
e84a093
refactoring.
Aug 22, 2024
4a2fa4e
updated domain model.
Aug 22, 2024
e97f94d
Started implementing receipt for extension 2
Aug 22, 2024
76583cd
.
Aug 23, 2024
7e886ac
Finished extension 2 - Bobs bagel
Aug 23, 2024
93d24a0
Updated domain model and added class diagram.
Aug 23, 2024
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
Prev Previous commit
Next Next commit
more refactoring of existing code.
Stian Rusvik committed Aug 20, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 59976856089c3768038d14a492a9f20c48f8e5de
49 changes: 28 additions & 21 deletions src/main/java/com/booleanuk/core/Basket.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.booleanuk.core;

import com.booleanuk.core.enums.BagelType;
import com.booleanuk.core.enums.SKU;
import com.booleanuk.core.exceptions.FullBasketException;
import com.booleanuk.core.exceptions.NonExistingProductException;
import com.booleanuk.core.factory.ProductFactory;
import com.booleanuk.core.inherited.Bagel;
import com.booleanuk.core.inherited.Coffee;
import com.booleanuk.core.inherited.Filling;
import com.booleanuk.core.interfaces.MenuCategory;

import java.util.ArrayList;
@@ -24,30 +23,25 @@ public Basket() {

public void addProduct(MenuCategory variant) throws FullBasketException {
if (!isFull()) {
products.add(factory.getProduct(variant));
this.getProducts().add(factory.getProduct(variant));
} else {
throw new FullBasketException("Your basket is full, cannot add product!");
}
}

public void removeProduct(MenuCategory variant) {
for (Product product : products) {
if (product instanceof Bagel bagel) {
if (bagel.getVariant() == variant) {
products.remove(product);
break;
}
public void removeProduct(MenuCategory variant) throws NonExistingProductException {
for (Product product : this.getProducts()) {
Product specificProduct = checkAndGetProduct(product);

if (!specificProduct.getVariant().equals(variant)) {
String message = "Product does not exist in the basket, cannot remove.";
throw new NonExistingProductException(message);
}
}
}

public void removeProduct(Product product) throws NonExistingProductException {
boolean exists = this.getProducts().contains(product);
if (!exists) {
String message = "Product does not exist in the basket, cannot remove.";
throw new NonExistingProductException(message);
} else {
this.products.remove(product);
if (specificProduct.getVariant() == variant) {
this.getProducts().remove(product);
break;
}
}
}

@@ -56,7 +50,20 @@ public void changeCapacity(int newCapacity) {
}

private Boolean isFull() {
return this.products.size() == capacity;
return this.getProducts().size() == capacity;
}

private Product checkAndGetProduct(Product product) {
if (product instanceof Bagel bagel) {
return bagel;
}
if (product instanceof Coffee coffee) {
return coffee;
}
if (product instanceof Filling filling) {
return filling;
}
return null;
}

public ArrayList<Product> getProducts() {
11 changes: 9 additions & 2 deletions src/main/java/com/booleanuk/core/Product.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package com.booleanuk.core;

import com.booleanuk.core.enums.SKU;
import com.booleanuk.core.interfaces.MenuCategory;

public abstract class Product {
public abstract class Product implements MenuCategory {
private String name;
private Double price;
private SKU sku;
private MenuCategory variant;

public Product(String name, Double price, SKU sku) {
public Product(String name, Double price, SKU sku, MenuCategory variant) {
this.name = name;
this.price = price;
this.sku = sku;
this.variant = variant;
}

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

public MenuCategory getVariant() {
return variant;
}
}
8 changes: 1 addition & 7 deletions src/main/java/com/booleanuk/core/inherited/Bagel.java
Original file line number Diff line number Diff line change
@@ -5,14 +5,8 @@
import com.booleanuk.core.enums.SKU;

public class Bagel extends Product {
private final BagelType variant;

public Bagel(String name, Double price, SKU sku, BagelType variant) {
super(name, price, sku);
this.variant = variant;
}

public BagelType getVariant() {
return variant;
super(name, price, sku, variant);
}
}
5 changes: 1 addition & 4 deletions src/main/java/com/booleanuk/core/inherited/Coffee.java
Original file line number Diff line number Diff line change
@@ -5,10 +5,7 @@
import com.booleanuk.core.enums.SKU;

public class Coffee extends Product {
private final CoffeeType variant;

public Coffee(String name, Double price, SKU sku, CoffeeType variant) {
super(name, price, sku);
this.variant = variant;
super(name, price, sku, variant);
}
}
5 changes: 1 addition & 4 deletions src/main/java/com/booleanuk/core/inherited/Filling.java
Original file line number Diff line number Diff line change
@@ -5,10 +5,7 @@
import com.booleanuk.core.enums.SKU;

public class Filling extends Product {
private final FillingType variant;

public Filling(String name, Double price, SKU sku, FillingType variant) {
super(name, price, sku);
this.variant = variant;
super(name, price, sku, variant);
}
}
5 changes: 2 additions & 3 deletions src/test/java/com/booleanuk/core/BasketTest.java
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@
import com.booleanuk.core.exceptions.FullBasketException;
import com.booleanuk.core.exceptions.NonExistingProductException;
import com.booleanuk.core.inherited.Bagel;
import com.booleanuk.core.inherited.Coffee;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@@ -28,7 +27,7 @@ public void testAddProduct() {
}

@Test
public void testRemoveProduct() {
public void testRemoveProduct() throws NonExistingProductException {
Basket basket = this.order.getBasket();
basket.removeProduct(BagelType.ONION);
Assertions.assertTrue(basket.getProducts().isEmpty());
@@ -59,7 +58,7 @@ public void testRemoveProductThrowException() {
Basket basket = this.order.getBasket();

Assertions.assertThrows(NonExistingProductException.class, () -> {
basket.removeProduct(new Coffee("Coffee", 0.99, SKU.COFB, CoffeeType.BLACK));
basket.removeProduct(CoffeeType.BLACK);
});
}