forked from knh4437/java-lotto
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBudget.java
More file actions
48 lines (40 loc) · 1.7 KB
/
Budget.java
File metadata and controls
48 lines (40 loc) · 1.7 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
package lotto.domain;
public class Budget {
private static final String NUMBER_FORMAT_EXCEPTION_MESSAGE = "[ERROR] 구입 금액은 숫자만 입력할 수 있습니다.";
private static final String DIVIDE_EXCEPTION_MESSAGE = "[ERROR] 구입 금액은 %d원 단위로 나누어 떨어져야합니다.";
private static final String MINIMUM_PRICE_EXCEPTION_MESSAGE = "[ERROR] 로또 구입 최소 금액은 %d원 입니다.";
private static final int LOTTO_UNIT_PRICE = 1000;
private static final int ZERO = 0;
private final int budget;
private Budget(final int budget) {
this.budget = budget;
}
public static Budget from(final String inputBudget) {
validateNumeric(inputBudget);
int budget = Integer.parseInt(inputBudget);
return new Budget(budget);
}
private static void validateNumeric(final String inputBudget) {
try {
Integer.parseInt(inputBudget);
} catch (NumberFormatException e) {
throw new IllegalArgumentException(NUMBER_FORMAT_EXCEPTION_MESSAGE, e);
}
}
private static void validateDivideResultIsZero(final int inputBudget) {
if(inputBudget % LOTTO_UNIT_PRICE != ZERO) {
throw new IllegalArgumentException(String.format(DIVIDE_EXCEPTION_MESSAGE, LOTTO_UNIT_PRICE));
}
}
private static void validateLowerPrice(final int inputBudget) {
if(inputBudget < LOTTO_UNIT_PRICE) {
throw new IllegalArgumentException(String.format(MINIMUM_PRICE_EXCEPTION_MESSAGE, LOTTO_UNIT_PRICE));
}
}
public int getCountOfLottoLines() {
return budget / LOTTO_UNIT_PRICE;
}
public int getBudget() {
return budget;
}
}