Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.

Commit 62391cd

Browse files
author
Félix Carpena
committed
discount percentages (step4)
1 parent 76810f6 commit 62391cd

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/main/java/group/rohlik/entity/Cart.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,19 @@ private double totalLinesPrice() {
7474
.sum();
7575
}
7676

77-
private double totalDiscountsPrice() {
77+
private double totalDiscountsPercentage() {
7878
return discounts
7979
.stream()
8080
.mapToDouble(Discount::getValue)
8181
.sum();
8282
}
8383

8484
public double totalPrice() {
85+
double totalLinesPrice = totalLinesPrice();
8586
return Math.max(
8687
0,
8788
BigDecimal
88-
.valueOf(totalLinesPrice() - totalDiscountsPrice())
89+
.valueOf(totalLinesPrice - (totalLinesPrice * totalDiscountsPercentage() / 100))
8990
.setScale(2, RoundingMode.CEILING)
9091
.doubleValue()
9192
);

src/test/java/group/rohlik/acceptance/steps/DiscountSteps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public class DiscountSteps {
1111
private final DiscountRepository discountRepository;
1212

13-
@Given("there is a cart discount {string} for {double} euros with code {string}")
13+
@Given("there is a cart discount {string} for {double} % with code {string}")
1414
@Transactional
1515
public void thereIsACartDiscountWithCode(String name, double value, String code) {
1616
Discount discount = Discount.create(name, code, value);

src/test/resources/features/add_discount_to_cart.feature

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ Feature: Add discount to cart
88
| sku | name | price |
99
| 001 | potatoes | 2.5 |
1010
| 002 | water | 0.95 |
11-
And there is a cart discount "free shipping" for 1 euros with code "FREE-SHIPPING"
12-
And there is a cart discount "special offer" for 10 euros with code "SPECIAL-OFFER"
11+
And there is a cart discount "free shipping" for 10 % with code "FREE-SHIPPING"
12+
And there is a cart discount "special offer" for 95 % with code "SPECIAL-OFFER"
1313
And I have a cart
1414

1515
Scenario: No discounts
@@ -20,14 +20,14 @@ Feature: Add discount to cart
2020
Scenario: Add discount
2121
Given I add 2 units of product "001" to my cart
2222
When I apply "FREE-SHIPPING" discount to my cart
23-
Then the cart's total cost should be 4.0 euros
23+
Then the cart's total cost should be 4.50 euros
2424
And there should be discount "free shipping" in my cart
2525

2626
Scenario: Add discount only apply once
2727
Given I add 2 units of product "001" to my cart
2828
And I apply "FREE-SHIPPING" discount to my cart
2929
When I apply "FREE-SHIPPING" discount to my cart
30-
Then the cart's total cost should be 4.0 euros
30+
Then the cart's total cost should be 4.50 euros
3131
And there should be discount "free shipping" in my cart
3232

3333
Scenario: Cart total should be always positive
@@ -37,3 +37,19 @@ Feature: Add discount to cart
3737
Then the cart's total cost should be 0.0 euros
3838
And there should be discount "free shipping" in my cart
3939
And there should be discount "special offer" in my cart
40+
41+
Scenario Outline: Add discount examples
42+
Given the following products exist:
43+
| sku | name | price |
44+
| 003 | orange juice | <product_price> |
45+
And there is a cart discount "super discount" for <percentage> % with code "SUPER-DISCOUNT"
46+
Given I add <product_quantity> units of product "003" to my cart
47+
When I apply "SUPER-DISCOUNT" discount to my cart
48+
Then the cart's total cost should be <cart_total> euros
49+
And there should be discount "super discount" in my cart
50+
Examples:
51+
| product_price | product_quantity | percentage | cart_total |
52+
| 21.0 | 1 | 10 | 18.90 |
53+
| 20.0 | 2 | 15 | 34.0 |
54+
| 5.0 | 3 | 3 | 14.55 |
55+
| 5.0 | 3 | 0 | 15.00 |

0 commit comments

Comments
 (0)