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

Commit e51f81f

Browse files
author
Félix Carpena
committed
discount percentages (step4)
1 parent 7ee02c6 commit e51f81f

5 files changed

Lines changed: 54 additions & 24 deletions

File tree

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ private double totalLinesPrice() {
7373
.sum();
7474
}
7575

76-
private double totalDiscountsPrice() {
76+
private double totalDiscountsPercentage() {
7777
return discounts
7878
.stream()
79-
.mapToDouble(Discount::getAmount)
79+
.mapToDouble(Discount::getPercentage)
8080
.sum();
8181
}
8282

@@ -92,10 +92,13 @@ public void recalculateAutomaticDiscounts(List<Discount> discounts) {
9292
}
9393

9494
public double totalPrice() {
95-
return BigDecimal
96-
.valueOf(totalLinesPrice() - totalDiscountsPrice())
97-
.setScale(2, RoundingMode.CEILING)
98-
.doubleValue();
95+
return Math.max(
96+
0,
97+
BigDecimal
98+
.valueOf(totalLinesPrice() - (totalLinesPrice() * totalDiscountsPercentage() / 100))
99+
.setScale(2, RoundingMode.CEILING)
100+
.doubleValue()
101+
);
99102
}
100103

101104
public boolean hasProduct(String sku) {
@@ -124,6 +127,7 @@ public Integer quantityOfProduct(String sku) {
124127
.map(CartLine::getQuantity)
125128
.orElse(0);
126129
}
130+
127131
public void applyDiscount(Discount discount) {
128132
Assert.isTrue(!discount.isAutomatic(), "Discount can not be of type automatic");
129133
Assert.isTrue(!discounts.contains(discount), "Discount already applied");

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Discount {
2424
@Column(nullable = false)
2525
private float minPrice;
2626
@Column(nullable = false)
27-
private float amount;
27+
private float percentage;
2828
@Column
2929
private String code;
3030
@Column
@@ -35,21 +35,21 @@ public enum DiscountType {
3535
CODE, MIN_PRICE
3636
}
3737

38-
public static Discount minimumPriceDiscount(String name, float amount, float minPrice) {
38+
public static Discount minimumPriceDiscount(String name, float percentage, float minPrice) {
3939
Discount discount = new Discount();
4040
discount.name = name;
41-
discount.amount = amount;
41+
discount.percentage = percentage;
4242
discount.minPrice = minPrice;
4343
discount.code = null;
4444
discount.type = DiscountType.MIN_PRICE;
4545

4646
return discount;
4747
}
4848

49-
public static Discount codeDiscount(String name, float amount, String code) {
49+
public static Discount codeDiscount(String name, float percentage, String code) {
5050
Discount discount = new Discount();
5151
discount.name = name;
52-
discount.amount = amount;
52+
discount.percentage = percentage;
5353
discount.minPrice = 0;
5454
discount.code = code;
5555
discount.type = DiscountType.CODE;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ public class DiscountSteps {
1111

1212
private final DiscountRepository discountRepository;
1313

14-
@Given("there is a cart discount {string} for {float} euros with a minimum total price of {float} euros")
14+
@Given("there is a cart discount {string} for {float} % with a minimum total price of {float} euros")
1515
@Transactional
1616
public void thereIsACartDiscountWithAMinimumTotalPrice(String name, float amount, float minPrice) {
1717
Discount discount = Discount.minimumPriceDiscount(name, amount, minPrice);
1818
discountRepository.save(discount);
1919
}
2020

21-
@Given("there is a cart discount {string} for {float} euros with code {string}")
21+
@Given("there is a cart discount {string} for {float} % with code {string}")
2222
@Transactional
2323
public void thereIsACartDiscountWithCode(String name, float amount, String code) {
2424
Discount discount = Discount.codeDiscount(name, amount, code);

src/test/resources/features/add_discount_to_cart.feature

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,60 @@ 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 "Amazing discount" for 1 euros with a minimum total price of 6 euros
11+
And there is a cart discount "free shipping" for 10 % with code "FREE-SHIPPING"
1312
And I have a cart
1413

1514
Scenario: Add discount
1615
Given I add 2 units of product "001" to my cart
1716
When I apply "FREE-SHIPPING" discount to my cart
18-
Then the cart's total cost should be 4.0 euros
17+
Then the cart's total cost should be 4.50 euros
1918
And there should be discount "free shipping" in my cart
2019

2120
Scenario: Add discount only apply once
2221
Given I add 2 units of product "001" to my cart
2322
And I apply "FREE-SHIPPING" discount to my cart
2423
When I apply "FREE-SHIPPING" discount to my cart
25-
Then the cart's total cost should be 4.0 euros
24+
Then the cart's total cost should be 4.5 euros
2625
And there should be discount "free shipping" in my cart
2726

2827
Scenario: Mix automatic and code discounts
29-
Given I add 3 units of product "001" to my cart
28+
Given there is a cart discount "Amazing discount" for 10 % with a minimum total price of 6 euros
29+
And I add 3 units of product "001" to my cart
3030
When I apply "FREE-SHIPPING" discount to my cart
31-
Then the cart's total cost should be 5.5 euros
31+
Then the cart's total cost should be 6.0 euros
3232
And there should be discount "free shipping" in my cart
3333
And there should be discount "Amazing discount" in my cart
3434

3535
Scenario: Code discounts are no recalculated
36-
Given I add 2 units of product "001" to my cart
36+
Given there is a cart discount "Amazing discount" for 10 % with a minimum total price of 6 euros
37+
And I add 2 units of product "001" to my cart
3738
And I add 2 units of product "002" to my cart
3839
And I apply "FREE-SHIPPING" discount to my cart
3940
When I remove product "002" of my cart
40-
Then the cart's total cost should be 4.0 euros
41-
And there should be discount "free shipping" in my cart
41+
Then the cart's total cost should be 4.5 euros
42+
And there should be discount "free shipping" in my cart
43+
44+
Scenario Outline: Add discount examples
45+
Given the following products exist:
46+
| sku | name | price |
47+
| 003 | orange juice | <product_price> |
48+
And there is a cart discount "super discount" for <percentage> % with code "SUPER-DISCOUNT"
49+
Given I add <product_quantity> units of product "003" to my cart
50+
When I apply "SUPER-DISCOUNT" discount to my cart
51+
Then the cart's total cost should be <cart_total> euros
52+
And there should be discount "super discount" in my cart
53+
Examples:
54+
| product_price | product_quantity | percentage | cart_total |
55+
| 21.0 | 1 | 10 | 18.90 |
56+
| 20.0 | 2 | 15 | 34.0 |
57+
| 5.0 | 3 | 3 | 14.55 |
58+
59+
Scenario: Cart value is always positive
60+
Given there is a cart discount "super discount" for 50 % with code "SUPER-DISCOUNT"
61+
And there is a cart discount "another super discount" for 55 % with code "ANOTHER-SUPER-DISCOUNT"
62+
And I add 5 units of product "001" to my cart
63+
When I apply "SUPER-DISCOUNT" discount to my cart
64+
And I apply "ANOTHER-SUPER-DISCOUNT" discount to my cart
65+
Then the cart's total cost should be 0 euros
66+
And there should be discount "super discount" in my cart
67+
And there should be discount "another super discount" in my cart

src/test/resources/features/add_product_to_cart.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Feature: Add product to cart
88
| sku | name | price |
99
| 001 | potatoes | 2.5 |
1010
| 002 | water | 0.95 |
11-
And there is a cart discount "Amazing discount" for 1 euros with a minimum total price of 6 euros
11+
And there is a cart discount "Amazing discount" for 10 % with a minimum total price of 6 euros
1212
And I have a cart
1313

1414
Scenario: Add product
@@ -34,7 +34,7 @@ Feature: Add product to cart
3434
Then there should be 2 units of product "001" in my cart
3535
And there should be 2 units of product "002" in my cart
3636
And there should be discount 1 in my cart
37-
And the cart's total cost should be 5.90 euros
37+
And the cart's total cost should be 6.21 euros
3838

3939
Scenario: Recalculate discount when remove products
4040
Given I add 2 units of product "001" to my cart

0 commit comments

Comments
 (0)