-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce ShoppingCart model and use it in ShoppingCartService
- Loading branch information
Showing
3 changed files
with
151 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/io/github/akadir/casestudy/shopping/service/model/ShoppingCart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.github.akadir.casestudy.shopping.service.model; | ||
|
||
import io.github.akadir.casestudy.discount.campaign.base.Campaign; | ||
import io.github.akadir.casestudy.discount.coupon.base.Coupon; | ||
import io.github.akadir.casestudy.product.model.Product; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ShoppingCart { | ||
private final Map<Product, Integer> products; | ||
private Coupon appliedCoupon; | ||
private Campaign appliedCampaign; | ||
private double discount; | ||
|
||
public ShoppingCart() { | ||
this.products = new HashMap<>(); | ||
} | ||
|
||
public Map<Product, Integer> getProducts() { | ||
return products; | ||
} | ||
|
||
public Coupon getAppliedCoupon() { | ||
return appliedCoupon; | ||
} | ||
|
||
public void setAppliedCoupon(Coupon appliedCoupon) { | ||
this.appliedCoupon = appliedCoupon; | ||
} | ||
|
||
public Campaign getAppliedCampaign() { | ||
return appliedCampaign; | ||
} | ||
|
||
public void setAppliedCampaign(Campaign appliedCampaign) { | ||
this.appliedCampaign = appliedCampaign; | ||
} | ||
|
||
public double getDiscount() { | ||
return discount; | ||
} | ||
|
||
public void setDiscount(double discount) { | ||
this.discount = discount; | ||
} | ||
|
||
public double getCartPrice() { | ||
return products.entrySet().stream() | ||
.mapToDouble(e -> e.getKey().getPrice() * e.getValue()).sum(); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/test/java/io/github/akadir/casestudy/shopping/model/ShoppingCartTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.github.akadir.casestudy.shopping.model; | ||
|
||
import io.github.akadir.casestudy.product.model.Category; | ||
import io.github.akadir.casestudy.product.model.Product; | ||
import io.github.akadir.casestudy.shopping.service.model.ShoppingCart; | ||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ShoppingCartTest { | ||
|
||
@Test | ||
public void whenShoppingCartCreatedThenExpectProductsInitializedAndDiscountsEmpty() { | ||
ShoppingCart shoppingCart = new ShoppingCart(); | ||
|
||
assertThat(shoppingCart.getProducts()) | ||
.as("Shopping cart's products should be empty") | ||
.isEmpty(); | ||
|
||
assertThat(shoppingCart.getAppliedCampaign()) | ||
.as("Applied campaign should be null") | ||
.isNull(); | ||
|
||
assertThat(shoppingCart.getAppliedCoupon()) | ||
.as("Applied coupon should be null") | ||
.isNull(); | ||
|
||
assertThat(shoppingCart.getCartPrice()) | ||
.as("Cart price should be zero") | ||
.isZero(); | ||
|
||
assertThat(shoppingCart.getDiscount()) | ||
.as("Cart discount should be zero") | ||
.isZero(); | ||
} | ||
|
||
|
||
@Test | ||
public void whenProductAddedToShoppingCartThenExpectProductsIsNotEmptyAndPriceIncreased() { | ||
ShoppingCart shoppingCart = new ShoppingCart(); | ||
|
||
Product foo = new Product("foo", 10, new Category("bar")); | ||
int amount = 1; | ||
|
||
shoppingCart.getProducts().put(foo, amount); | ||
|
||
assertThat(shoppingCart.getProducts()) | ||
.as("Shopping cart's products size should be " + amount) | ||
.hasSize(amount); | ||
|
||
assertThat(shoppingCart.getAppliedCampaign()) | ||
.as("Applied campaign should be null") | ||
.isNull(); | ||
|
||
assertThat(shoppingCart.getAppliedCoupon()) | ||
.as("Applied coupon should be null") | ||
.isNull(); | ||
|
||
assertThat(shoppingCart.getCartPrice()) | ||
.as("Cart price should be same as product price times amount: " + foo.getPrice() * amount) | ||
.isEqualTo(foo.getPrice() * amount); | ||
|
||
assertThat(shoppingCart.getDiscount()) | ||
.as("Cart discount should be zero") | ||
.isZero(); | ||
} | ||
} |