-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEventPlannerController.java
More file actions
89 lines (70 loc) · 2.65 KB
/
EventPlannerController.java
File metadata and controls
89 lines (70 loc) · 2.65 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package christmas.controller;
import christmas.domain.Menu;
import christmas.domain.Order;
import christmas.domain.OrderItem;
import christmas.domain.EventResult;
import christmas.service.EventService;
import christmas.view.InputView;
import christmas.view.OutputView;
import christmas.validator.InputValidator;
import java.util.ArrayList;
import java.util.List;
public class EventPlannerController {
private final EventService eventService;
private final OutputView outputView;
private final InputView inputView;
public EventPlannerController(EventService eventService, InputView inputView, OutputView outputView) {
this.eventService = eventService;
this.outputView = outputView;
this.inputView = inputView;
}
public void run() {
int date = readDateWithRetry();
List<OrderItem> orderItems = readOrderWithRetry();
Order order = new Order(orderItems);
EventResult eventResult = eventService.planEvent(order, date);
outputView.printEventResult(eventResult);
}
private int readDateWithRetry() {
while (true) {
try {
int date = InputValidator.validateDate(inputView.readDate());
return date;
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
private List<OrderItem> readOrderWithRetry() {
while (true) {
try {
String input = inputView.readOrder();
return parseOrder(input);
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
private List<OrderItem> parseOrder(String input) {
List<OrderItem> orderItems = new ArrayList<>();
String[] items = input.split(",");
for (String item : items) {
String[] details = item.split("-");
InputValidator.validateMenuFormat(details);
String menuName = details[0].trim();
String quantityStr = details[1].trim();
InputValidator.validateMenuName(menuName);
int quantity = parseQuantity(quantityStr);
InputValidator.validateQuantity(quantity);
orderItems.add(new OrderItem(Menu.fromName(menuName), quantity));
}
return orderItems;
}
private int parseQuantity(String quantityStr) {
try {
return Integer.parseInt(quantityStr);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("[ERROR] 유효하지 않은 주문입니다. 다시 입력해 주세요.");
}
}
}