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
36 changes: 18 additions & 18 deletions java/src/main/java/dojo/supermarket/model/ShoppingCart.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,39 @@ public void addItemQuantity(Product product, double quantity) {
}

void handleOffers(Receipt receipt, Map<Product, Offer> offers, SupermarketCatalog catalog) {
for (Product p: productQuantities().keySet()) {
double quantity = productQuantities.get(p);
if (offers.containsKey(p)) {
Offer offer = offers.get(p);
double unitPrice = catalog.getUnitPrice(p);
for (Product product: productQuantities().keySet()) {
double quantity = productQuantities.get(product);
if (offers.containsKey(product)) {
Offer offer = offers.get(product);
double unitPrice = catalog.getUnitPrice(product);
int quantityAsInt = (int) quantity;
Discount discount = null;
int x = 1;
int itemsPerDeal = 1;
if (offer.offerType == SpecialOfferType.THREE_FOR_TWO) {
x = 3;
itemsPerDeal = 3;

} else if (offer.offerType == SpecialOfferType.TWO_FOR_AMOUNT) {
x = 2;
itemsPerDeal = 2;
if (quantityAsInt >= 2) {
double total = offer.argument * (quantityAsInt / x) + quantityAsInt % 2 * unitPrice;
double discountN = unitPrice * quantity - total;
discount = new Discount(p, "2 for " + offer.argument, -discountN);
double total = offer.argument * (quantityAsInt / itemsPerDeal) + quantityAsInt % 2 * unitPrice;
double discountAmount = unitPrice * quantity - total;
discount = new Discount(product, "2 for " + offer.argument, -discountAmount);
}

} if (offer.offerType == SpecialOfferType.FIVE_FOR_AMOUNT) {
x = 5;
itemsPerDeal = 5;
}
int numberOfXs = quantityAsInt / x;
int numberOfCompleteDeals = quantityAsInt / itemsPerDeal;
if (offer.offerType == SpecialOfferType.THREE_FOR_TWO && quantityAsInt > 2) {
double discountAmount = quantity * unitPrice - ((numberOfXs * 2 * unitPrice) + quantityAsInt % 3 * unitPrice);
discount = new Discount(p, "3 for 2", -discountAmount);
double discountAmount = quantity * unitPrice - ((numberOfCompleteDeals * 2 * unitPrice) + quantityAsInt % 3 * unitPrice);
discount = new Discount(product, "3 for 2", -discountAmount);
}
if (offer.offerType == SpecialOfferType.TEN_PERCENT_DISCOUNT) {
discount = new Discount(p, offer.argument + "% off", -quantity * unitPrice * offer.argument / 100.0);
discount = new Discount(product, offer.argument + "% off", -quantity * unitPrice * offer.argument / 100.0);
}
if (offer.offerType == SpecialOfferType.FIVE_FOR_AMOUNT && quantityAsInt >= 5) {
double discountTotal = unitPrice * quantity - (offer.argument * numberOfXs + quantityAsInt % 5 * unitPrice);
discount = new Discount(p, x + " for " + offer.argument, -discountTotal);
double totalDiscountAmount = unitPrice * quantity - (offer.argument * numberOfCompleteDeals + quantityAsInt % 5 * unitPrice);
discount = new Discount(product, itemsPerDeal + " for " + offer.argument, -totalDiscountAmount);
}
if (discount != null)
receipt.addDiscount(discount);
Expand Down
16 changes: 8 additions & 8 deletions java/src/main/java/dojo/supermarket/model/Teller.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ public void addSpecialOffer(SpecialOfferType offerType, Product product, double
offers.put(product, new Offer(offerType, product, argument));
}

public Receipt checksOutArticlesFrom(ShoppingCart theCart) {
public Receipt checkoutCart(ShoppingCart cart) {
Receipt receipt = new Receipt();
List<ProductQuantity> productQuantities = theCart.getItems();
for (ProductQuantity pq: productQuantities) {
Product p = pq.getProduct();
double quantity = pq.getQuantity();
double unitPrice = catalog.getUnitPrice(p);
List<ProductQuantity> productQuantities = cart.getItems();
for (ProductQuantity productQuantity: productQuantities) {
Product product = productQuantity.getProduct();
double quantity = productQuantity.getQuantity();
double unitPrice = catalog.getUnitPrice(product);
double price = quantity * unitPrice;
receipt.addProduct(p, quantity, unitPrice, price);
receipt.addProduct(product, quantity, unitPrice, price);
}
theCart.handleOffers(receipt, offers, catalog);
cart.handleOffers(receipt, offers, catalog);

return receipt;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void tenPercentDiscount() {
cart.addItemQuantity(apples, 2.5);

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

// ASSERT
assertEquals(4.975, receipt.getTotalPrice(), 0.01);
Expand Down
3 changes: 1 addition & 2 deletions python/receipt_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def print_receipt_item(self, item):
def format_line_with_whitespace(self, name, value):
line = name
whitespace_size = self.columns - len(name) - len(value)
for i in range(whitespace_size):
line += " "
line += " " * whitespace_size
line += value
line += "\n"
return line
Expand Down
38 changes: 19 additions & 19 deletions python/shopping_cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,41 +28,41 @@ def add_item_quantity(self, product, quantity):
self._product_quantities[product] = quantity

def handle_offers(self, receipt, offers, catalog):
for p in self._product_quantities.keys():
quantity = self._product_quantities[p]
if p in offers.keys():
offer = offers[p]
unit_price = catalog.unit_price(p)
for product in self._product_quantities.keys():
quantity = self._product_quantities[product]
if product in offers.keys():
offer = offers[product]
unit_price = catalog.unit_price(product)
quantity_as_int = int(quantity)
discount = None
x = 1
items_per_deal = 1
if offer.offer_type == SpecialOfferType.THREE_FOR_TWO:
x = 3
items_per_deal = 3

elif offer.offer_type == SpecialOfferType.TWO_FOR_AMOUNT:
x = 2
items_per_deal = 2
if quantity_as_int >= 2:
total = offer.argument * (quantity_as_int / x) + quantity_as_int % 2 * unit_price
discount_n = unit_price * quantity - total
discount = Discount(p, "2 for " + str(offer.argument), -discount_n)
total = offer.argument * (quantity_as_int / items_per_deal) + quantity_as_int % 2 * unit_price
discount_amount = unit_price * quantity - total
discount = Discount(product, "2 for " + str(offer.argument), -discount_amount)

if offer.offer_type == SpecialOfferType.FIVE_FOR_AMOUNT:
x = 5
items_per_deal = 5

number_of_x = math.floor(quantity_as_int / x)
number_of_complete_deals = math.floor(quantity_as_int / items_per_deal)
if offer.offer_type == SpecialOfferType.THREE_FOR_TWO and quantity_as_int > 2:
discount_amount = quantity * unit_price - (
(number_of_x * 2 * unit_price) + quantity_as_int % 3 * unit_price)
discount = Discount(p, "3 for 2", -discount_amount)
(number_of_complete_deals * 2 * unit_price) + quantity_as_int % 3 * unit_price)
discount = Discount(product, "3 for 2", -discount_amount)

if offer.offer_type == SpecialOfferType.TEN_PERCENT_DISCOUNT:
discount = Discount(p, str(offer.argument) + "% off",
discount = Discount(product, str(offer.argument) + "% off",
-quantity * unit_price * offer.argument / 100.0)

if offer.offer_type == SpecialOfferType.FIVE_FOR_AMOUNT and quantity_as_int >= 5:
discount_total = unit_price * quantity - (
offer.argument * number_of_x + quantity_as_int % 5 * unit_price)
discount = Discount(p, str(x) + " for " + str(offer.argument), -discount_total)
total_discount_amount = unit_price * quantity - (
offer.argument * number_of_complete_deals + quantity_as_int % 5 * unit_price)
discount = Discount(product, str(items_per_deal) + " for " + str(offer.argument), -total_discount_amount)

if discount:
receipt.add_discount(discount)
16 changes: 8 additions & 8 deletions python/teller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ def __init__(self, catalog):
def add_special_offer(self, offer_type, product, argument):
self.offers[product] = Offer(offer_type, product, argument)

def checks_out_articles_from(self, the_cart):
def checkout_cart(self, cart):
receipt = Receipt()
product_quantities = the_cart.items
for pq in product_quantities:
p = pq.product
quantity = pq.quantity
unit_price = self.catalog.unit_price(p)
product_quantities = cart.items
for product_quantity in product_quantities:
product = product_quantity.product
quantity = product_quantity.quantity
unit_price = self.catalog.unit_price(product)
price = quantity * unit_price
receipt.add_product(p, quantity, unit_price, price)
receipt.add_product(product, quantity, unit_price, price)

the_cart.handle_offers(receipt, self.offers, self.catalog)
cart.handle_offers(receipt, self.offers, self.catalog)

return receipt
23 changes: 22 additions & 1 deletion python/tests/test_supermarket.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def test_ten_percent_discount():
cart = ShoppingCart()
cart.add_item_quantity(apples, 2.5)

receipt = teller.checks_out_articles_from(cart)
receipt = teller.checkout_cart(cart)

assert 4.975 == pytest.approx(receipt.total_price(), 0.01)
assert [] == receipt.discounts
Expand All @@ -30,3 +30,24 @@ def test_ten_percent_discount():
assert 1.99 == receipt_item.price
assert 2.5 * 1.99 == pytest.approx(receipt_item.total_price, 0.01)
assert 2.5 == receipt_item.quantity


def test_three_for_two_offer():
"""Test to verify the renamed variables work correctly for three-for-two offers"""
catalog = FakeCatalog()
toothbrush = Product("toothbrush", ProductUnit.EACH)
catalog.add_product(toothbrush, 0.99)

teller = Teller(catalog)
teller.add_special_offer(SpecialOfferType.THREE_FOR_TWO, toothbrush, 0)

cart = ShoppingCart()
cart.add_item_quantity(toothbrush, 3)

receipt = teller.checkout_cart(cart)

# Should get 3 items for the price of 2
expected_price = 2 * 0.99 # Pay for 2, get 1 free
assert expected_price == pytest.approx(receipt.total_price(), 0.01)
assert 1 == len(receipt.discounts)
assert "3 for 2" in receipt.discounts[0].description