Skip to content
Draft
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
4 changes: 2 additions & 2 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>22</source>
<target>22</target>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
Expand Down
22 changes: 22 additions & 0 deletions java/src/main/java/dojo/supermarket/model/BundleOffer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dojo.supermarket.model;

import java.util.List;
import java.util.Collections;

public class BundleOffer {
private final List<Product> products;
private final double discountPercent;

public BundleOffer(List<Product> products, double discountPercent) {
this.products = Collections.unmodifiableList(products);
this.discountPercent = discountPercent;
}

public List<Product> getProducts() {
return products;
}

public double getDiscountPercent() {
return discountPercent;
}
}
36 changes: 36 additions & 0 deletions java/src/main/java/dojo/supermarket/model/ShoppingCart.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,40 @@ void handleOffers(Receipt receipt, Map<Product, Offer> offers, SupermarketCatalo
}
}
}

void handleBundleOffers(Receipt receipt, List<BundleOffer> bundleOffers, SupermarketCatalog catalog) {
for (BundleOffer bundleOffer : bundleOffers) {
// Calculate how many complete bundles can be made
int completeBundles = calculateCompleteBundles(bundleOffer.getProducts());

if (completeBundles > 0) {
// Calculate total price for one complete bundle
double bundlePrice = 0.0;
for (Product product : bundleOffer.getProducts()) {
bundlePrice += catalog.getUnitPrice(product);
}

// Calculate discount for all complete bundles
double totalBundlePrice = bundlePrice * completeBundles;
double discountAmount = totalBundlePrice * bundleOffer.getDiscountPercent() / 100.0;

// Create bundle discount
String description = "Bundle discount (" + bundleOffer.getDiscountPercent() + "% off)";
Discount bundleDiscount = new Discount(bundleOffer.getProducts().get(0), description, -discountAmount);
receipt.addDiscount(bundleDiscount);
}
}
}

private int calculateCompleteBundles(List<Product> bundleProducts) {
int minQuantity = Integer.MAX_VALUE;

for (Product product : bundleProducts) {
double quantity = productQuantities.getOrDefault(product, 0.0);
int quantityAsInt = (int) quantity;
minQuantity = Math.min(minQuantity, quantityAsInt);
}

return minQuantity == Integer.MAX_VALUE ? 0 : minQuantity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public enum SpecialOfferType {
TEN_PERCENT_DISCOUNT,
TWO_FOR_AMOUNT,
FIVE_FOR_AMOUNT,
BUNDLE,
}
7 changes: 7 additions & 0 deletions java/src/main/java/dojo/supermarket/model/Teller.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;

public class Teller {

private final SupermarketCatalog catalog;
private final Map<Product, Offer> offers = new HashMap<>();
private final List<BundleOffer> bundleOffers = new ArrayList<>();

public Teller(SupermarketCatalog catalog) {
this.catalog = catalog;
Expand All @@ -17,6 +19,10 @@ public void addSpecialOffer(SpecialOfferType offerType, Product product, double
offers.put(product, new Offer(offerType, product, argument));
}

public void addBundleOffer(List<Product> products, double discountPercent) {
bundleOffers.add(new BundleOffer(products, discountPercent));
}

public Receipt checksOutArticlesFrom(ShoppingCart theCart) {
Receipt receipt = new Receipt();
List<ProductQuantity> productQuantities = theCart.getItems();
Expand All @@ -28,6 +34,7 @@ public Receipt checksOutArticlesFrom(ShoppingCart theCart) {
receipt.addProduct(p, quantity, unitPrice, price);
}
theCart.handleOffers(receipt, offers, catalog);
theCart.handleBundleOffers(receipt, bundleOffers, catalog);

return receipt;
}
Expand Down
29 changes: 29 additions & 0 deletions java/src/test/java/dojo/supermarket/model/BundleDemo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dojo.supermarket.model;

import dojo.supermarket.ReceiptPrinter;
import java.util.Arrays;
import java.util.List;

public class BundleDemo {
public static void main(String[] args) {
SupermarketCatalog catalog = new FakeCatalog();
Product toothbrush = new Product("toothbrush", ProductUnit.EACH);
catalog.addProduct(toothbrush, 0.99);
Product toothpaste = new Product("toothpaste", ProductUnit.EACH);
catalog.addProduct(toothpaste, 1.79);

Teller teller = new Teller(catalog);
List<Product> bundleProducts = Arrays.asList(toothbrush, toothpaste);
teller.addBundleOffer(bundleProducts, 10.0);

ShoppingCart cart = new ShoppingCart();
cart.addItemQuantity(toothbrush, 1.0);
cart.addItemQuantity(toothpaste, 1.0);

Receipt receipt = teller.checksOutArticlesFrom(cart);

String receiptText = new ReceiptPrinter().printReceipt(receipt);
System.out.println(receiptText);
System.out.println("Total Price: €" + String.format("%.2f", receipt.getTotalPrice()));
}
}
146 changes: 146 additions & 0 deletions java/src/test/java/dojo/supermarket/model/SupermarketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand Down Expand Up @@ -41,5 +43,149 @@ void tenPercentDiscount() {

}

@Test
void bundleDiscountBasicCase() {
SupermarketCatalog catalog = new FakeCatalog();
Product toothbrush = new Product("toothbrush", ProductUnit.EACH);
catalog.addProduct(toothbrush, 0.99);
Product toothpaste = new Product("toothpaste", ProductUnit.EACH);
catalog.addProduct(toothpaste, 1.79);

Teller teller = new Teller(catalog);
List<Product> bundleProducts = Arrays.asList(toothbrush, toothpaste);
teller.addBundleOffer(bundleProducts, 10.0);

ShoppingCart cart = new ShoppingCart();
cart.addItemQuantity(toothbrush, 1.0);
cart.addItemQuantity(toothpaste, 1.0);

// ACT
Receipt receipt = teller.checksOutArticlesFrom(cart);

// ASSERT
// Total should be (0.99 + 1.79) - 10% discount = 2.78 - 0.278 = 2.502
assertEquals(2.502, receipt.getTotalPrice(), 0.01);
assertEquals(1, receipt.getDiscounts().size());
assertEquals(2, receipt.getItems().size());

Discount discount = receipt.getDiscounts().get(0);
assertEquals(-0.278, discount.getDiscountAmount(), 0.01);
assertEquals("Bundle discount (10.0% off)", discount.getDescription());
}

@Test
void bundleDiscountOnlyCompleteBundle() {
SupermarketCatalog catalog = new FakeCatalog();
Product toothbrush = new Product("toothbrush", ProductUnit.EACH);
catalog.addProduct(toothbrush, 0.99);
Product toothpaste = new Product("toothpaste", ProductUnit.EACH);
catalog.addProduct(toothpaste, 1.79);

Teller teller = new Teller(catalog);
List<Product> bundleProducts = Arrays.asList(toothbrush, toothpaste);
teller.addBundleOffer(bundleProducts, 10.0);

ShoppingCart cart = new ShoppingCart();
cart.addItemQuantity(toothbrush, 2.0); // 2 toothbrushes
cart.addItemQuantity(toothpaste, 1.0); // 1 toothpaste

// ACT
Receipt receipt = teller.checksOutArticlesFrom(cart);

// ASSERT
// Only 1 complete bundle (limited by toothpaste), so discount on 0.99 + 1.79 = 2.78
// Total: (2 * 0.99 + 1 * 1.79) - 0.278 = 3.77 - 0.278 = 3.492
assertEquals(3.492, receipt.getTotalPrice(), 0.01);
assertEquals(1, receipt.getDiscounts().size());

Discount discount = receipt.getDiscounts().get(0);
assertEquals(-0.278, discount.getDiscountAmount(), 0.01);
}

@Test
void bundleDiscountMultipleBundles() {
SupermarketCatalog catalog = new FakeCatalog();
Product toothbrush = new Product("toothbrush", ProductUnit.EACH);
catalog.addProduct(toothbrush, 0.99);
Product toothpaste = new Product("toothpaste", ProductUnit.EACH);
catalog.addProduct(toothpaste, 1.79);

Teller teller = new Teller(catalog);
List<Product> bundleProducts = Arrays.asList(toothbrush, toothpaste);
teller.addBundleOffer(bundleProducts, 10.0);

ShoppingCart cart = new ShoppingCart();
cart.addItemQuantity(toothbrush, 2.0);
cart.addItemQuantity(toothpaste, 2.0);

// ACT
Receipt receipt = teller.checksOutArticlesFrom(cart);

// ASSERT
// 2 complete bundles, so discount on 2 * (0.99 + 1.79) = 2 * 2.78 = 5.56
// Discount: 5.56 * 0.1 = 0.556
// Total: 5.56 - 0.556 = 5.004
assertEquals(5.004, receipt.getTotalPrice(), 0.01);
assertEquals(1, receipt.getDiscounts().size());

Discount discount = receipt.getDiscounts().get(0);
assertEquals(-0.556, discount.getDiscountAmount(), 0.01);
}

@Test
void noBundleDiscountWhenMissingProduct() {
SupermarketCatalog catalog = new FakeCatalog();
Product toothbrush = new Product("toothbrush", ProductUnit.EACH);
catalog.addProduct(toothbrush, 0.99);
Product toothpaste = new Product("toothpaste", ProductUnit.EACH);
catalog.addProduct(toothpaste, 1.79);

Teller teller = new Teller(catalog);
List<Product> bundleProducts = Arrays.asList(toothbrush, toothpaste);
teller.addBundleOffer(bundleProducts, 10.0);

ShoppingCart cart = new ShoppingCart();
cart.addItemQuantity(toothbrush, 1.0); // Only toothbrush, no toothpaste

// ACT
Receipt receipt = teller.checksOutArticlesFrom(cart);

// ASSERT
// No bundle discount since toothpaste is missing
assertEquals(0.99, receipt.getTotalPrice(), 0.01);
assertEquals(Collections.emptyList(), receipt.getDiscounts());
}

@Test
void bundleDiscountWithRegularOffers() {
SupermarketCatalog catalog = new FakeCatalog();
Product toothbrush = new Product("toothbrush", ProductUnit.EACH);
catalog.addProduct(toothbrush, 0.99);
Product toothpaste = new Product("toothpaste", ProductUnit.EACH);
catalog.addProduct(toothpaste, 1.79);
Product apples = new Product("apples", ProductUnit.KILO);
catalog.addProduct(apples, 1.99);

Teller teller = new Teller(catalog);
List<Product> bundleProducts = Arrays.asList(toothbrush, toothpaste);
teller.addBundleOffer(bundleProducts, 10.0);
teller.addSpecialOffer(SpecialOfferType.TEN_PERCENT_DISCOUNT, apples, 20.0);

ShoppingCart cart = new ShoppingCart();
cart.addItemQuantity(toothbrush, 1.0);
cart.addItemQuantity(toothpaste, 1.0);
cart.addItemQuantity(apples, 1.0);

// ACT
Receipt receipt = teller.checksOutArticlesFrom(cart);

// ASSERT
// Bundle discount: (0.99 + 1.79) * 0.1 = 0.278
// Apples discount: 1.99 * 0.2 = 0.398
// Total: (0.99 + 1.79 + 1.99) - 0.278 - 0.398 = 4.77 - 0.676 = 4.094
assertEquals(4.094, receipt.getTotalPrice(), 0.01);
assertEquals(2, receipt.getDiscounts().size());
}


}