refactoring : divergent change - split phase
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase;
|
||||
|
||||
public record PriceData(double basePrice, double discount, int quantity) {
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase;
|
||||
|
||||
public class PriceOrder {
|
||||
|
||||
public double priceOrder(Product product, int quantity, ShippingMethod shippingMethod) {
|
||||
final PriceData priceData = calculatePriceData(product, quantity);
|
||||
|
||||
return applyShipping(priceData, shippingMethod);
|
||||
}
|
||||
|
||||
private PriceData calculatePriceData(Product product, int quantity) {
|
||||
final double basePrice = product.basePrice() * quantity;
|
||||
final double discount = Math.max(quantity - product.discountThreshold(), 0)
|
||||
* product.basePrice() * product.discountRate();
|
||||
|
||||
return new PriceData(basePrice, discount, quantity);
|
||||
}
|
||||
|
||||
private double applyShipping(PriceData priceData, ShippingMethod shippingMethod) {
|
||||
final double shippingPerCase = (priceData.basePrice() > shippingMethod.discountThreshold()) ?
|
||||
shippingMethod.discountedFee() : shippingMethod.feePerCase();
|
||||
final double shippingCost = priceData.quantity() * shippingPerCase;
|
||||
return priceData.basePrice() - priceData.discount() + shippingCost;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase;
|
||||
|
||||
public record Product(double basePrice, double discountThreshold, double discountRate) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase;
|
||||
|
||||
public record ShippingMethod(double discountThreshold, double discountedFee, double feePerCase) {
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase._before;
|
||||
|
||||
public class PriceOrder {
|
||||
|
||||
public double priceOrder(Product product, int quantity, ShippingMethod shippingMethod) {
|
||||
final double basePrice = product.basePrice() * quantity;
|
||||
final double discount = Math.max(quantity - product.discountThreshold(), 0)
|
||||
* product.basePrice() * product.discountRate();
|
||||
final double shippingPerCase = (basePrice > shippingMethod.discountThreshold()) ?
|
||||
shippingMethod.discountedFee() : shippingMethod.feePerCase();
|
||||
final double shippingCost = quantity * shippingPerCase;
|
||||
final double price = basePrice - discount + shippingCost;
|
||||
return price;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase._before;
|
||||
|
||||
public record Product(double basePrice, double discountThreshold, double discountRate) {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase._before;
|
||||
|
||||
public record ShippingMethod(double discountThreshold, double discountedFee, double feePerCase) {
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.example.refactoring._07_divergent_change._24_split_phase;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PriceOrderTest {
|
||||
|
||||
@Test
|
||||
void priceOrder_discountedFee() {
|
||||
PriceOrder priceOrder = new PriceOrder();
|
||||
double price = priceOrder.priceOrder(new Product(10, 2, 0.5),
|
||||
4,
|
||||
new ShippingMethod(20, 1, 5));
|
||||
|
||||
// basePrice = 10 * 4 = 40
|
||||
// discount = 2 * 10 * 0.5 = 10
|
||||
// shippingPerCase = 1 (40 > 20)
|
||||
// shippingCost = 4 * 1 = 4
|
||||
// price = 40 - 10 + 4 = 34
|
||||
|
||||
assertEquals(34, price);
|
||||
}
|
||||
|
||||
@Test
|
||||
void priceOrder_feePerCase() {
|
||||
PriceOrder priceOrder = new PriceOrder();
|
||||
double price = priceOrder.priceOrder(new Product(10, 2, 0.5),
|
||||
2,
|
||||
new ShippingMethod(20, 1, 5));
|
||||
|
||||
// basePrice = 10 * 2 = 20
|
||||
// discount = 0 * 10 * 0.5 = 0
|
||||
// shippingPerCase = 5
|
||||
// shippingCost = 2 * 5 = 10
|
||||
// price = 20 - 0 + 10 = 30
|
||||
|
||||
assertEquals(30, price);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user