refactoring : long parameter list - remove flag argument
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package com.example.refactoring._04_long_parameter_list._15_remove_flag_argument;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class Order {
|
||||
|
||||
private LocalDate placedOn;
|
||||
private String deliveryState;
|
||||
|
||||
public Order(LocalDate placedOn, String deliveryState) {
|
||||
this.placedOn = placedOn;
|
||||
this.deliveryState = deliveryState;
|
||||
}
|
||||
|
||||
public LocalDate getPlacedOn() {
|
||||
return placedOn;
|
||||
}
|
||||
|
||||
public String getDeliveryState() {
|
||||
return deliveryState;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.refactoring._04_long_parameter_list._15_remove_flag_argument;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
public class Shipment {
|
||||
|
||||
public LocalDate deliveryDate(Order order, boolean isRush) {
|
||||
if (isRush) {
|
||||
return rushDeliveryDate(order);
|
||||
} else {
|
||||
return regularDeliveryDate(order);
|
||||
}
|
||||
}
|
||||
|
||||
public LocalDate regularDeliveryDate(Order order) {
|
||||
int deliveryTime = switch (order.getDeliveryState()) {
|
||||
case "WA", "CA" -> 2;
|
||||
case "OR", "TX", "NY" -> 3;
|
||||
default -> 4;
|
||||
};
|
||||
return order.getPlacedOn().plusDays(deliveryTime);
|
||||
}
|
||||
|
||||
public LocalDate rushDeliveryDate(Order order) {
|
||||
int deliveryTime = switch (order.getDeliveryState()) {
|
||||
case "WA", "CA", "OR" -> 1;
|
||||
case "TX", "NY", "FL" -> 2;
|
||||
default -> 3;
|
||||
};
|
||||
return order.getPlacedOn().plusDays(deliveryTime);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.example.refactoring._04_long_parameter_list._15_remove_flag_argument;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ShipmentTest {
|
||||
|
||||
@Test
|
||||
void deliveryDate() {
|
||||
LocalDate placedOn = LocalDate.of(2021, 12, 15);
|
||||
Order orderFromWA = new Order(placedOn, "WA");
|
||||
|
||||
Shipment shipment = new Shipment();
|
||||
assertEquals(placedOn.plusDays(1), shipment.rushDeliveryDate(orderFromWA)); // flag 파라미터 전달 보다는 메소드 명을 명확하게 한다.
|
||||
assertEquals(placedOn.plusDays(2), shipment.regularDeliveryDate(orderFromWA));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user