<aside> π‘
μλ μ½λμ μ€λͺ μ λ³΄κ³ , [μΉμ 3. λ Όλ¦¬, μ¬κ³ μ νλ¦]μμ μ΄μΌκΈ°νλ λ΄μ©μ μ€μ¬μΌλ‘ μ½κΈ° μ’μ μ½λλ‘ λ¦¬ν©ν λ§ν΄ λ΄ μλ€.
βοΈ μ¬μ©μκ° μμ±ν 'μ£Όλ¬Έ'μ΄ μ ν¨νμ§λ₯Ό κ²μ¦νλ λ©μλ. βοΈ Orderλ μ£Όλ¬Έ κ°μ²΄μ΄κ³ , νμνλ€λ©΄ Orderμ μΆκ°μ μΈ λ©μλλ₯Ό λ§λ€μ΄λ λλ€. (Order λ΄λΆμ ꡬνμ ꡬ체μ μΌλ‘ ν νμλ μλ€.) βοΈ νμνλ€λ©΄ λ©μλλ₯Ό μΆμΆν μ μλ€.
</aside>
public boolean validateOrder(Order order) {
if (order.getItems().size() == 0) {
log.info("μ£Όλ¬Έ νλͺ©μ΄ μμ΅λλ€.");
return false;
} else {
if (order.getTotalPrice() > 0) {
if (!order.hasCustomerInfo()) {
log.info("μ¬μ©μ μ λ³΄κ° μμ΅λλ€.");
return false;
} else {
return true;
}
} else if (!(order.getTotalPrice() > 0)) {
log.info("μ¬λ°λ₯΄μ§ μμ μ΄ κ°κ²©μ
λλ€.");
return false;
}
}
return true;
}
public class OrderException extends RuntimeException {
public OrderException(String message) {
super(message);
}
}
public class EmptyItemException extends OrderException {
public EmptyItemException(String message) {
super(message);
}
}
public class TotalPriceNotValidException extends OrderException {
public TotalPriceNotValidException(String message) {
super(message);
}
}
public class EmptyCustomerInfoException extends OrderException {
public EmptyCustomerInfoException(String message) {
super(message);
}
}
public class Order {
private List<Item> items;
private Customer customerInfo;
private Order(List<Item> items, Customer customerInfo) {
this.items = items;
this.customerInfo = customerInfo;
}
public static Order createOrder(List<Item> items, Customer customerInfo) {
return new Order(items, customerInfo);
}
public void validateOrder(Order order) {
if (order.isEmptyItem()) {
throw new EmptyItemException("μ£Όλ¬Έ νλͺ©μ΄ μμ΅λλ€.");
}
if (order.doesNotHaveCustomerInfo()) {
throw new EmptyCustomerInfoException("μ¬μ©μ μ λ³΄κ° μμ΅λλ€.");
}
}
public long calculateTotalPrice() {
return this.items.stream()
.mapToLong(Item::getPrice)
.sum();
}
}
리ν©ν λ§μ κ°μμ [Section 3. λ Όλ¦¬, μ¬κ³ μ νλ¦]μμ νμ΅νλ λ΄μ©λ€ μ€ μ μ©μ΄ νμνλ€κ³ μκ°λλ μ£Όμ λ€μ λ½μμ νμ΅νλ μμλλ‘ νλμ© μ μ©μν€λ λ°©μμΌλ‘ μ§ννμλ€.
μλμ κ° κ³Όμ λ³λ‘ κ°λ¨νκ² μ 리ν΄λ³΄μλ€.
μ¬κΈ°κΉμ§κ° [Section 3. λ Όλ¦¬, μ¬κ³ μ νλ¦]μμ νμ΅ν μμλ‘ λ¦¬ν©ν λ§μ μ§νν κ²°κ³Όμ΄λ€.
μλλ κ°μ λ νμ΅ν [Section 4. κ°μ²΄ μ§ν₯ν¨λ¬λ€μ - κ°μ²΄ μ€κ³νκΈ°]κΉμ§μ λ΄μ©μ μΆκ°λ‘ μ μ©ν΄ 보μλ€.