refactoring : divergent change - split phase

This commit is contained in:
haerong22
2022-03-15 11:36:42 +09:00
parent 2fcf164ccb
commit b807790cd1
8 changed files with 102 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
package com.example.refactoring._07_divergent_change._24_split_phase;
public record PriceData(double basePrice, double discount, int quantity) {
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,5 @@
package com.example.refactoring._07_divergent_change._24_split_phase;
public record Product(double basePrice, double discountThreshold, double discountRate) {
}

View File

@@ -0,0 +1,4 @@
package com.example.refactoring._07_divergent_change._24_split_phase;
public record ShippingMethod(double discountThreshold, double discountedFee, double feePerCase) {
}

View File

@@ -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;
}
}

View File

@@ -0,0 +1,5 @@
package com.example.refactoring._07_divergent_change._24_split_phase._before;
public record Product(double basePrice, double discountThreshold, double discountRate) {
}

View File

@@ -0,0 +1,4 @@
package com.example.refactoring._07_divergent_change._24_split_phase._before;
public record ShippingMethod(double discountThreshold, double discountedFee, double feePerCase) {
}

View File

@@ -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);
}
}