-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathConsole.java
More file actions
129 lines (109 loc) · 4.47 KB
/
Console.java
File metadata and controls
129 lines (109 loc) · 4.47 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package org.programmers.springorder.console;
import org.programmers.springorder.consts.ErrorMessage;
import org.programmers.springorder.consts.Message;
import org.programmers.springorder.customer.dto.CustomerResponseDto;
import org.programmers.springorder.utils.MenuType;
import org.programmers.springorder.utils.Validation;
import org.programmers.springorder.voucher.dto.GiveVoucherRequestDto;
import org.programmers.springorder.voucher.dto.VoucherRequestDto;
import org.programmers.springorder.voucher.dto.VoucherResponseDto;
import org.programmers.springorder.voucher.model.VoucherType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.InputMismatchException;
import java.util.List;
import java.util.UUID;
@Component
public class Console {
private final Logger logger = LoggerFactory.getLogger(Console.class);
private final Input input;
private final Output output;
public Console() {
this.input = new Input();
this.output = new Output();
}
public void printMessage(String message) {
output.printMessage(message);
}
public MenuType inputMenu() {
printMessage(Message.MENU_SELECT_MESSAGE);
output.printPrompt();
try {
String menuNum = Validation.validateString(input.getInput());
return MenuType.selectMenu(menuNum);
} catch (InputMismatchException e) {
logger.error("errorMessage = {}", e.getMessage());
printMessage(e.getMessage());
return inputMenu(); // TODO:재귀로 호출하는 게 괜찮은 건지 확인 필요
}
}
private VoucherType inputVoucherType() {
printMessage(Message.VOUCHER_SELECT_MESSAGE);
output.printPrompt();
try {
String voucherNum = Validation.validateString(input.getInput());
return VoucherType.selectVoucherType(voucherNum);
} catch (InputMismatchException e) {
logger.error("errorMessage = {}", e.getMessage());
printMessage(e.getMessage());
return inputVoucherType();
}
}
private long inputVoucherValue(VoucherType voucherType) {
String discountValueMessage = voucherType == VoucherType.FIXED ? Message.INPUT_FIXED_DISCOUNT_VALUE_MESSAGE
: Message.INPUT_PERCENT_DISCOUNT_VALUE_MESSAGE;
printMessage(discountValueMessage);
output.printPrompt();
try {
return Validation.validateDiscountValue(input.getInput(), voucherType);
} catch (InputMismatchException | NumberFormatException e) {
//TODO: Message Const 로 넣을 수 있는 방법 있는지 확인
String message = String.format("잘못된 입력 값입니다. %d ~ %d 사이의 값을 입력해주세요.",
voucherType.getMinimumValue(),
voucherType.getMaximumValue());
logger.error("errorMessage = {}", message);
printMessage(message);
return inputVoucherValue(voucherType);
}
}
public VoucherRequestDto inputVoucherInfo() {
VoucherType voucherType = inputVoucherType();
long discountValue = inputVoucherValue(voucherType);
return new VoucherRequestDto(discountValue, voucherType);
}
public void showList(List<VoucherResponseDto> allVoucher) {
if (allVoucher.isEmpty()) {
printMessage(ErrorMessage.VOUCHER_NOT_EXIST_MESSAGE);
} else {
allVoucher.forEach(System.out::println);
}
}
public void showBlackList(List<CustomerResponseDto> blackList) {
if (blackList.isEmpty()) {
printMessage(ErrorMessage.BLACK_CONSUMER_NOT_EXIST_MESSAGE);
} else {
blackList.forEach(System.out::println);
}
}
public GiveVoucherRequestDto giveVoucherInfo() {
UUID customerId = getCustomerId();
UUID voucherId = getVoucherId();
return new GiveVoucherRequestDto(voucherId, customerId);
}
public UUID getCustomerId() {
printMessage(Message.INPUT_USER_ID);
return UUID.fromString(input.getInput());
}
public UUID getVoucherId() {
printMessage(Message.INPUT_VOUCHER_ID);
return UUID.fromString(input.getInput());
}
public void showCustomer(CustomerResponseDto customer) {
printMessage(customer.toString());
}
public String chooseMedia() {
printMessage(Message.CHOOSING_MEDIA);
return input.getInput();
}
}