forked from knh4437/java-lotto
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBonus.java
More file actions
38 lines (31 loc) · 1.2 KB
/
Bonus.java
File metadata and controls
38 lines (31 loc) · 1.2 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
package lotto.domain;
public class Bonus {
private final int bonus;
private static final String NUMBER_FORMAT_EXCEPTION_MESSAGE = "[ERROR] 보너스 번호는 숫자만 입력할 수 있습니다.";
private static final String NUMBER_PARSE_EXCEPTION_MESSAGE = "[ERROR] 보너스 번호는 1부터 45 사이의 숫자를 입력해야 합니다.";
private static final int MAX_NUMBER = 45;
private static final int MIN_NUMBER = 1;
private Bonus(int bonus) {
validateOutOfRange(bonus);
this.bonus = bonus;
}
public static Bonus from(String bonus) {
return new Bonus(convertBonusNumber(bonus));
}
private static int convertBonusNumber(String bonusNumber) {
try {
Integer.parseInt(bonusNumber);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(NUMBER_FORMAT_EXCEPTION_MESSAGE);
}
return Integer.parseInt(bonusNumber);
}
public void validateOutOfRange(int bonus) {
if (bonus < MIN_NUMBER || bonus > MAX_NUMBER) {
throw new IllegalArgumentException(NUMBER_PARSE_EXCEPTION_MESSAGE);
}
}
public int getBonus() {
return bonus;
}
}