Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.
Open
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
5 changes: 3 additions & 2 deletions src/main/java/group/rohlik/entity/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,19 @@ private double totalLinesPrice() {
.sum();
}

private double totalDiscountsPrice() {
private double totalDiscountsPercentage() {
return discounts
.stream()
.mapToDouble(Discount::getValue)
.sum();
}

public double totalPrice() {
double totalLinesPrice = totalLinesPrice();
return Math.max(
0,
BigDecimal
.valueOf(totalLinesPrice() - totalDiscountsPrice())
.valueOf(totalLinesPrice - (totalLinesPrice * totalDiscountsPercentage() / 100))
.setScale(2, RoundingMode.CEILING)
.doubleValue()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 20 additions & 4 deletions src/test/resources/features/add_discount_to_cart.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 | <product_price> |
And there is a cart discount "super discount" for <percentage> % with code "SUPER-DISCOUNT"
Given I add <product_quantity> units of product "003" to my cart
When I apply "SUPER-DISCOUNT" discount to my cart
Then the cart's total cost should be <cart_total> 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 |