From 62391cda2512853230242c6c3f2fcb3f03e0aaa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Carpena?= Date: Fri, 10 Jun 2022 11:13:53 +0200 Subject: [PATCH] discount percentages (step4) --- src/main/java/group/rohlik/entity/Cart.java | 5 ++-- .../acceptance/steps/DiscountSteps.java | 2 +- .../features/add_discount_to_cart.feature | 24 +++++++++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/group/rohlik/entity/Cart.java b/src/main/java/group/rohlik/entity/Cart.java index a01fbee..8bf3574 100644 --- a/src/main/java/group/rohlik/entity/Cart.java +++ b/src/main/java/group/rohlik/entity/Cart.java @@ -74,7 +74,7 @@ private double totalLinesPrice() { .sum(); } - private double totalDiscountsPrice() { + private double totalDiscountsPercentage() { return discounts .stream() .mapToDouble(Discount::getValue) @@ -82,10 +82,11 @@ private double totalDiscountsPrice() { } public double totalPrice() { + double totalLinesPrice = totalLinesPrice(); return Math.max( 0, BigDecimal - .valueOf(totalLinesPrice() - totalDiscountsPrice()) + .valueOf(totalLinesPrice - (totalLinesPrice * totalDiscountsPercentage() / 100)) .setScale(2, RoundingMode.CEILING) .doubleValue() ); diff --git a/src/test/java/group/rohlik/acceptance/steps/DiscountSteps.java b/src/test/java/group/rohlik/acceptance/steps/DiscountSteps.java index 7e3ba00..7b68225 100644 --- a/src/test/java/group/rohlik/acceptance/steps/DiscountSteps.java +++ b/src/test/java/group/rohlik/acceptance/steps/DiscountSteps.java @@ -10,7 +10,7 @@ public class DiscountSteps { private final DiscountRepository discountRepository; - @Given("there is a cart discount {string} for {double} euros with code {string}") + @Given("there is a cart discount {string} for {double} % with code {string}") @Transactional public void thereIsACartDiscountWithCode(String name, double value, String code) { Discount discount = Discount.create(name, code, value); diff --git a/src/test/resources/features/add_discount_to_cart.feature b/src/test/resources/features/add_discount_to_cart.feature index 7eddf65..ed050a1 100644 --- a/src/test/resources/features/add_discount_to_cart.feature +++ b/src/test/resources/features/add_discount_to_cart.feature @@ -8,8 +8,8 @@ Feature: Add discount to cart | sku | name | price | | 001 | potatoes | 2.5 | | 002 | water | 0.95 | - And there is a cart discount "free shipping" for 1 euros with code "FREE-SHIPPING" - And there is a cart discount "special offer" for 10 euros with code "SPECIAL-OFFER" + And there is a cart discount "free shipping" for 10 % with code "FREE-SHIPPING" + And there is a cart discount "special offer" for 95 % with code "SPECIAL-OFFER" And I have a cart Scenario: No discounts @@ -20,14 +20,14 @@ Feature: Add discount to cart Scenario: Add discount Given I add 2 units of product "001" to my cart When I apply "FREE-SHIPPING" discount to my cart - Then the cart's total cost should be 4.0 euros + Then the cart's total cost should be 4.50 euros And there should be discount "free shipping" in my cart Scenario: Add discount only apply once Given I add 2 units of product "001" to my cart And I apply "FREE-SHIPPING" discount to my cart When I apply "FREE-SHIPPING" discount to my cart - Then the cart's total cost should be 4.0 euros + Then the cart's total cost should be 4.50 euros And there should be discount "free shipping" in my cart Scenario: Cart total should be always positive @@ -37,3 +37,19 @@ Feature: Add discount to cart Then the cart's total cost should be 0.0 euros And there should be discount "free shipping" in my cart And there should be discount "special offer" in my cart + + Scenario Outline: Add discount examples + Given the following products exist: + | sku | name | price | + | 003 | orange juice | | + And there is a cart discount "super discount" for % with code "SUPER-DISCOUNT" + Given I add units of product "003" to my cart + When I apply "SUPER-DISCOUNT" discount to my cart + Then the cart's total cost should be euros + And there should be discount "super discount" in my cart + Examples: + | product_price | product_quantity | percentage | cart_total | + | 21.0 | 1 | 10 | 18.90 | + | 20.0 | 2 | 15 | 34.0 | + | 5.0 | 3 | 3 | 14.55 | + | 5.0 | 3 | 0 | 15.00 | \ No newline at end of file