-
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.
- Loading branch information
Showing
9 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
src/main/java/io/github/akadir/casestudy/delivery/Deliverable.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,7 @@ | ||
package io.github.akadir.casestudy.delivery; | ||
|
||
public interface Deliverable { | ||
int getProductCount(); | ||
|
||
int getDeliveryCount(); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/io/github/akadir/casestudy/delivery/strategy/DeliveryStrategy.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,7 @@ | ||
package io.github.akadir.casestudy.delivery.strategy; | ||
|
||
import io.github.akadir.casestudy.delivery.Deliverable; | ||
|
||
public interface DeliveryStrategy { | ||
double calculateDeliveryCost(Deliverable deliverable); | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/github/akadir/casestudy/delivery/strategy/FedexDelivery.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,22 @@ | ||
package io.github.akadir.casestudy.delivery.strategy; | ||
|
||
import io.github.akadir.casestudy.delivery.Deliverable; | ||
|
||
public class FedexDelivery implements DeliveryStrategy { | ||
private final double costPerProduct; | ||
private final double costPerDelivery; | ||
|
||
public FedexDelivery(double costPerProduct, double costPerDelivery) { | ||
this.costPerProduct = costPerProduct; | ||
this.costPerDelivery = costPerDelivery; | ||
} | ||
|
||
@Override | ||
public double calculateDeliveryCost(Deliverable deliverable) { | ||
if (deliverable.getDeliveryCount() == 0) { | ||
return 0; | ||
} else { | ||
return costPerDelivery * deliverable.getDeliveryCount() + costPerProduct * deliverable.getProductCount(); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/github/akadir/casestudy/delivery/strategy/UPSDelivery.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,22 @@ | ||
package io.github.akadir.casestudy.delivery.strategy; | ||
|
||
import io.github.akadir.casestudy.delivery.Deliverable; | ||
|
||
public class UPSDelivery implements DeliveryStrategy { | ||
private final double costPerProduct; | ||
private final double costPerDelivery; | ||
|
||
public UPSDelivery(double costPerProduct, double costPerDelivery) { | ||
this.costPerProduct = costPerProduct; | ||
this.costPerDelivery = costPerDelivery; | ||
} | ||
|
||
@Override | ||
public double calculateDeliveryCost(Deliverable deliverable) { | ||
if (deliverable.getDeliveryCount() == 0) { | ||
return 0; | ||
} else { | ||
return costPerDelivery + deliverable.getProductCount() * costPerProduct; | ||
} | ||
} | ||
} |
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
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
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
79 changes: 79 additions & 0 deletions
79
src/test/java/io/github/akadir/casestudy/delivery/strategy/FedexDeliveryTest.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,79 @@ | ||
package io.github.akadir.casestudy.delivery.strategy; | ||
|
||
import io.github.akadir.casestudy.delivery.Deliverable; | ||
import io.github.akadir.casestudy.delivery.strategy.DeliveryStrategy; | ||
import io.github.akadir.casestudy.delivery.strategy.FedexDelivery; | ||
import io.github.akadir.casestudy.product.model.Category; | ||
import io.github.akadir.casestudy.product.model.Product; | ||
import io.github.akadir.casestudy.shopping.service.ShoppingCartService; | ||
import io.github.akadir.casestudy.shopping.service.impl.ConcreteShoppingCartServiceImpl; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class FedexDeliveryTest { | ||
private ShoppingCartService shoppingCartService; | ||
|
||
@Before | ||
public void init() { | ||
shoppingCartService = new ConcreteShoppingCartServiceImpl(); | ||
} | ||
|
||
@Test | ||
public void whenUPSDeliveryCostCalculatedWithNonEmptyProductCartThenExpectCorrectCost() { | ||
Category category = new Category("Accessories"); | ||
Product product = new Product("bag", 50, category); | ||
|
||
shoppingCartService.addProduct(product); | ||
|
||
double costPerDelivery = 3; | ||
double costPerProduct = 1; | ||
|
||
DeliveryStrategy deliveryStrategy = new FedexDelivery(costPerProduct, costPerDelivery); | ||
|
||
Map<Product, Integer> productsBackup = new HashMap<>(shoppingCartService.getProducts()); | ||
|
||
Deliverable deliverable = shoppingCartService.getShoppingCart(); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(deliverable)) | ||
.as("Delivery cost should be correct") | ||
.isEqualTo(costPerDelivery + costPerProduct); | ||
|
||
assertThat(shoppingCartService.getProducts()) | ||
.as("Shopping cart should not be modified in delivery cost calculation") | ||
.isEqualTo(productsBackup); | ||
|
||
Product anotherProduct = new Product("back bag", 100, category); | ||
|
||
shoppingCartService.addProduct(anotherProduct, 2); | ||
|
||
productsBackup = new HashMap<>(shoppingCartService.getProducts()); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(deliverable)) | ||
.as("Delivery cost should be correct") | ||
.isEqualTo((costPerDelivery * deliverable.getDeliveryCount()) + (costPerProduct * deliverable.getProductCount())); | ||
|
||
assertThat(shoppingCartService.getProducts()) | ||
.as("Shopping cart should not be modified in delivery cost calculation") | ||
.isEqualTo(productsBackup); | ||
} | ||
|
||
@Test | ||
public void whenUPSDeliveryCostCalculatedWithEmptyCartThenExpectZeroCost() { | ||
DeliveryStrategy deliveryStrategy = new FedexDelivery(1, 3); | ||
|
||
Map<Product, Integer> productsBackup = new HashMap<>(shoppingCartService.getProducts()); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(shoppingCartService.getShoppingCart())) | ||
.as("Delivery cost should be zero for empty cart") | ||
.isZero(); | ||
|
||
assertThat(shoppingCartService.getProducts()) | ||
.as("Shopping cart should not be modified in delivery cost calculation") | ||
.isEqualTo(productsBackup); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/test/java/io/github/akadir/casestudy/delivery/strategy/UPSDeliveryTest.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,78 @@ | ||
package io.github.akadir.casestudy.delivery.strategy; | ||
|
||
import io.github.akadir.casestudy.delivery.strategy.DeliveryStrategy; | ||
import io.github.akadir.casestudy.delivery.strategy.UPSDelivery; | ||
import io.github.akadir.casestudy.product.model.Category; | ||
import io.github.akadir.casestudy.product.model.Product; | ||
import io.github.akadir.casestudy.shopping.service.ShoppingCartService; | ||
import io.github.akadir.casestudy.shopping.service.impl.ConcreteShoppingCartServiceImpl; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class UPSDeliveryTest { | ||
|
||
private ShoppingCartService shoppingCartService; | ||
|
||
@Before | ||
public void init() { | ||
shoppingCartService = new ConcreteShoppingCartServiceImpl(); | ||
} | ||
|
||
@Test | ||
public void whenUPSDeliveryCostCalculatedWithNonEmptyProductCartThenExpectCorrectCost() { | ||
Category category = new Category("Accessories"); | ||
Product product = new Product("bag", 50, category); | ||
|
||
shoppingCartService.addProduct(product); | ||
|
||
double costPerDelivery = 3; | ||
double costPerProduct = 1; | ||
|
||
DeliveryStrategy deliveryStrategy = new UPSDelivery(costPerProduct, costPerDelivery); | ||
|
||
Map<Product, Integer> productsBackup = new HashMap<>(shoppingCartService.getProducts()); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(shoppingCartService.getShoppingCart())) | ||
.as("Delivery cost should be correct") | ||
.isEqualTo(costPerDelivery + costPerProduct); | ||
|
||
assertThat(shoppingCartService.getProducts()) | ||
.as("Shopping cart should not be modified in delivery cost calculation") | ||
.isEqualTo(productsBackup); | ||
|
||
Product anotherProduct = new Product("back bag", 100, category); | ||
|
||
shoppingCartService.addProduct(anotherProduct, 2); | ||
|
||
productsBackup = new HashMap<>(shoppingCartService.getProducts()); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(shoppingCartService.getShoppingCart())) | ||
.as("Delivery cost should be correct") | ||
.isEqualTo(costPerDelivery + 3 * costPerProduct); | ||
|
||
assertThat(shoppingCartService.getProducts()) | ||
.as("Shopping cart should not be modified in delivery cost calculation") | ||
.isEqualTo(productsBackup); | ||
|
||
} | ||
|
||
@Test | ||
public void whenUPSDeliveryCostCalculatedWithEmptyCartThenExpectZeroCost() { | ||
DeliveryStrategy deliveryStrategy = new UPSDelivery(1, 3); | ||
|
||
Map<Product, Integer> productsBackup = new HashMap<>(shoppingCartService.getProducts()); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(shoppingCartService.getShoppingCart())) | ||
.as("Delivery cost should be zero for empty cart") | ||
.isZero(); | ||
|
||
assertThat(shoppingCartService.getProducts()) | ||
.as("Shopping cart should not be modified in delivery cost calculation") | ||
.isEqualTo(productsBackup); | ||
} | ||
} |