-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrders.java
More file actions
29 lines (22 loc) · 1.31 KB
/
Copy pathOrders.java
File metadata and controls
29 lines (22 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.cleancoder.splitphase;
record Product(double basePrice, double discountThreshold, double discountRate) {}
record ShippingMethod(double discountThreshold, double discountedFee, Double feePerCase) {}
record PricingData(double basePrice, double discount, int quantity) {}
public class Orders {
double priceOrder(Product product, int quantity, ShippingMethod shippingMethod) {
PricingData pricingData = calculatePricingData(product, quantity);
return applyShippingCost(shippingMethod, pricingData);
}
private static PricingData calculatePricingData(Product product, int quantity) {
double basePrice = product.basePrice() * quantity;
double discount = Math.max(quantity - product.discountThreshold(), 0)
* product.basePrice() * product.discountRate();
return new PricingData(basePrice, discount, quantity);
}
private static double applyShippingCost(ShippingMethod shippingMethod, PricingData pricingData) {
double shippingPerCase = (pricingData.basePrice() > shippingMethod.discountThreshold())
? shippingMethod.discountedFee() : shippingMethod.feePerCase();
double shippingCost = pricingData.quantity() * shippingPerCase;
return pricingData.basePrice() - pricingData.discount() * shippingCost;
}
}