-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathInputView.java
More file actions
92 lines (79 loc) · 2.88 KB
/
InputView.java
File metadata and controls
92 lines (79 loc) · 2.88 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
package lotto.view;
import lotto.domain.Lotto;
import lotto.domain.LottoNumber;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class InputView {
private static final Scanner scanner = new Scanner(System.in);
public static int inputPurchaseAmount() {
while (true) {
try {
System.out.println("구입금액을 입력해 주세요.");
String input = scanner.nextLine();
validateNotEmpty(input);
return Integer.parseInt(input.trim());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
public static Lotto inputWinningNumbers() {
System.out.println("지난 주 당첨 번호를 입력해 주세요.");
String input = scanner.nextLine();
return parseWinningNumbers(input);
}
private static Lotto parseWinningNumbers(String input) {
String[] tokens = input.split(",");
List<Integer> numbers = new ArrayList<>();
for (String token : tokens) {
numbers.add(Integer.parseInt(token.trim()));
}
return Lotto.from(numbers);
}
public static LottoNumber inputBonusNumber() {
System.out.println("보너스 볼을 입력해 주세요.");
return LottoNumber.of(Integer.parseInt(scanner.nextLine()));
}
public static int inputManualLottoCount() {
while (true) {
try {
System.out.println("\n수동으로 구매할 로또 수를 입력해 주세요.");
String input = scanner.nextLine();
validateNotEmpty(input);
return Integer.parseInt(input.trim());
} catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
}
public static List<String> inputManualLottos(int count) {
validateCount(count);
if (count == 0) {
return new ArrayList<>();
}
System.out.println("수동으로 구매할 번호를 입력해 주세요.");
List<String> manualLottos = new ArrayList<>();
for (int i = 0; i < count; i++) {
manualLottos.add(inputSingleManualLotto());
}
return manualLottos;
}
private static String inputSingleManualLotto() {
String input = scanner.nextLine();
validateNotEmpty(input);
//
// parseWinningNumbers(input);
return input.trim();
}
private static void validateCount(int count) {
if (count < 0) {
throw new IllegalArgumentException("음수를 입력할 수 없습니다.");
}
}
private static void validateNotEmpty(String input) {
if (input == null || input.trim().isEmpty()) {
throw new IllegalArgumentException("빈 값을 입력할 수 없습니다.");
}
}
}