-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathInputView.java
More file actions
84 lines (74 loc) · 2.98 KB
/
Copy pathInputView.java
File metadata and controls
84 lines (74 loc) · 2.98 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
package lotto.view;
import camp.nextstep.edu.missionutils.Console;
import lotto.dto.LottoDto;
import java.util.ArrayList;
import java.util.List;
public class InputView {
/* 구입 금액 입력 */
public static int getPurchaseAmount() {
while (true) {
try {
System.out.println("구입 금액을 입력해주세요.");
String input = Console.readLine();
validatePurchaseAmount(input);
return Integer.parseInt(input);
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] " + e.getMessage());
}
}
}
private static void validatePurchaseAmount(String purchaseAmount) {
try {
int amount = Integer.parseInt(purchaseAmount);
if (amount <= 0) {
throw new IllegalArgumentException("구매 금액은 0보다 커야 합니다.");
}
if (amount % 1000 != 0) {
throw new IllegalArgumentException("구매 금액은 1000 단위로 나누어 떨어져야 합니다.");
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("구매 금액은 숫자여야 합니다.");
}
}
/* 당첨 금액 입력 */
public static LottoDto getWinningNumbers(){
while (true) {
try {
System.out.println("\n당첨 번호를 입력해주세요.");
String inputWinningNumbers = Console.readLine();
List<Integer> numbers = parseWinningNumbers(inputWinningNumbers);
return new LottoDto(numbers);
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] " + e.getMessage());
}
}
}
private static List<Integer> parseWinningNumbers(String inputWinningNumbers) {
String[] numberStrings = inputWinningNumbers.split(",");
List<Integer> numbers = new ArrayList<>();
try {
for (String numberString : numberStrings) {
numbers.add(Integer.parseInt(numberString.trim()));
}
} catch (NumberFormatException e) {
throw new IllegalArgumentException("로또 번호는 숫자여야 합니다.");
}
return numbers;
}
/* 보너스 번호 입력 */
public static int getBonusNumber(){
while (true) {
try {
System.out.println("\n보너스 번호를 입력해주세요.");
String input = Console.readLine();
int bonusNumber = Integer.parseInt(input.trim());
if (bonusNumber < 1 || bonusNumber > 45) {
throw new IllegalArgumentException("보너스 번호는 1과 45 사이의 숫자여야 합니다.");
}
return Integer.parseInt(input.trim());
} catch (IllegalArgumentException e) {
System.out.println("[ERROR] " + e.getMessage());
}
}
}
}