Q1.

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

A1.

μ΅œμ’… μ½”λ“œ

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. 논리, μ‚¬κ³ μ˜ 흐름]μ—μ„œ ν•™μŠ΅ν–ˆλ˜ λ‚΄μš©λ“€ 쀑 적용이 ν•„μš”ν•˜λ‹€κ³  μƒκ°λ˜λŠ” μ£Όμ œλ“€μ„ λ½‘μ•„μ„œ ν•™μŠ΅ν–ˆλ˜ μˆœμ„œλŒ€λ‘œ ν•˜λ‚˜μ”© μ μš©μ‹œν‚€λŠ” λ°©μ‹μœΌλ‘œ μ§„ν–‰ν•˜μ˜€λ‹€.

μ•„λž˜μ— 각 κ³Όμ • λ³„λ‘œ κ°„λ‹¨ν•˜κ²Œ μ •λ¦¬ν•΄λ³΄μ•˜λ‹€.

1. Early Return μ μš©ν•˜κΈ°(else μ‚¬μš©μ„ μ§€μ–‘ν•˜κΈ°)

2. μ‚¬κ³ μ˜ depth 쀄이기

3. 곡백 라인으둜 의미 λ‹¨μœ„ λ‚˜λˆ„κΈ°

4. 가독성을 λ–¨μ–΄λœ¨λ¦¬λŠ” λΆ€μ • μ—°μ‚°μž μ œκ±°ν•˜κΈ°

5. μ˜λ„ν•œ μ˜ˆμ™Έ μƒμ„±ν•˜κΈ°

μ—¬κΈ°κΉŒμ§€κ°€ [Section 3. 논리, μ‚¬κ³ μ˜ 흐름]μ—μ„œ ν•™μŠ΅ν•œ μˆœμ„œλ‘œ λ¦¬νŒ©ν† λ§μ„ μ§„ν–‰ν•œ 결과이닀.

μ•„λž˜λŠ” 같은 λ‚  ν•™μŠ΅ν•œ [Section 4. 객체 지ν–₯νŒ¨λŸ¬λ‹€μž„ - 객체 μ„€κ³„ν•˜κΈ°]κΉŒμ§€μ˜ λ‚΄μš©μ„ μΆ”κ°€λ‘œ μ μš©ν•΄ λ³΄μ•˜λ‹€.

6. getter μ‚¬μš©μ„ μžμ œν•˜κ³  객체에 λ©”μ‹œμ§€λ₯Ό 보내기