forked from woowacourse-precourse/java-lotto-6
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathConsoleInput.java
More file actions
68 lines (62 loc) · 2.57 KB
/
ConsoleInput.java
File metadata and controls
68 lines (62 loc) · 2.57 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
package lotto.view;
import camp.nextstep.edu.missionutils.Console;
import java.util.HashSet;
import java.util.Set;
public class ConsoleInput {
public int getPurchaseAmount() {
System.out.println("구입금액을 입력해 주세요.");
String input = Console.readLine();
int purchaseAmount;
try {
purchaseAmount = Integer.parseInt(input);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("구입 금액은 숫자여야 합니다.");
}
if (purchaseAmount % 1000 != 0) {
throw new IllegalArgumentException("구입 금액은 1,000원 단위여야 합니다.");
}
return purchaseAmount;
}
public Set<Integer> getWinningNumbers() {
System.out.println("당첨 번호를 입력해 주세요.");
String input = Console.readLine();
String[] split = input.split(",");
if (split.length != 6) {
throw new IllegalArgumentException("당첨 번호는 6개여야 합니다.");
}
Set<Integer> winningNumbers = new HashSet<>();
for (String number : split) {
int num;
try {
num = Integer.parseInt(number);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("당첨 번호는 숫자여야 합니다.");
}
if (num < 1 || num > 45) {
throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다.");
}
winningNumbers.add(num);
}
if (winningNumbers.size() != 6) {
throw new IllegalArgumentException("당첨 번호는 중복되지 않는 6개의 숫자여야 합니다.");
}
return winningNumbers;
}
public int getBonusNumber(Set<Integer> winningNumbers) {
System.out.println("보너스 번호를 입력해 주세요.");
String input = Console.readLine();
int bonusNumber;
try {
bonusNumber = Integer.parseInt(input);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("보너스 번호는 숫자여야 합니다.");
}
if (bonusNumber < 1 || bonusNumber > 45) {
throw new IllegalArgumentException("로또 번호는 1부터 45 사이의 숫자여야 합니다.");
}
if (winningNumbers.contains(bonusNumber)) {
throw new IllegalArgumentException("보너스 번호는 당첨 번호와 중복되지 않아야 합니다.");
}
return bonusNumber;
}
}