-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLottoNumber.java
More file actions
69 lines (56 loc) · 1.78 KB
/
LottoNumber.java
File metadata and controls
69 lines (56 loc) · 1.78 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
package domain;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public class LottoNumber implements Comparable<LottoNumber> {
private static final int MINIMUM_LOTTO_NUMBER = 1;
private static final int MAXIMUM_LOTTO_NUMBER = 45;
private static final String PRINT_IF_NUMBER_IS_INVALID_NUMBER = "유효한 로또 번호가 아닙니다.";
private static final Map<Integer, LottoNumber> MAPPING_BALL = new HashMap<>();
private final int number;
static {
DisposeBall();
}
private static void DisposeBall() {
for (int ballNumber = MINIMUM_LOTTO_NUMBER; ballNumber <= MAXIMUM_LOTTO_NUMBER; ballNumber++) {
MAPPING_BALL.put(ballNumber, new LottoNumber(ballNumber));
}
}
private LottoNumber(int number) {
this.number = number;
}
public static LottoNumber of(int number) {
if (MAPPING_BALL.containsKey(number)) {
return MAPPING_BALL.get(number);
}
throw new IllegalArgumentException(PRINT_IF_NUMBER_IS_INVALID_NUMBER);
}
public static List<LottoNumber> getBalls() {
return new ArrayList<>(MAPPING_BALL.values());
}
@Override
public String toString() {
return Integer.toString(number);
}
@Override
public int compareTo(LottoNumber o) {
return Integer.compare(number, o.number);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
LottoNumber ball = (LottoNumber) o;
return number == ball.number;
}
@Override
public int hashCode() {
return Objects.hash(number);
}
}