-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathLotto.java
More file actions
65 lines (53 loc) · 2.01 KB
/
Lotto.java
File metadata and controls
65 lines (53 loc) · 2.01 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
package domain;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Lotto {
private static final String LOTTO_NUMBER_COUNT_EXCEPTION_MESSAGE = "6개를 입력해주세요";
private static final String DELIMITER = ", ";
private static final int NUMBER_COUNT_ZERO = 0;
private static final int NUMBER_COUNT = 6;
private final Set<LottoNumber> balls;
private Lotto(Set<LottoNumber> balls) {
validateBallCount(balls);
this.balls = Collections.unmodifiableSet(new HashSet<>(balls));
}
private void validateBallCount(Set<LottoNumber> balls) {
if (balls.size() != NUMBER_COUNT) {
throw new IllegalArgumentException(LOTTO_NUMBER_COUNT_EXCEPTION_MESSAGE);
}
}
public static Lotto of(String rawLotto) {
String[] numbers = rawLotto.split(DELIMITER);
Set<LottoNumber> balls = new HashSet<>();
for (String number : numbers) {
balls.add(LottoNumber.of(Integer.parseInt(number)));
}
return new Lotto(balls);
}
public static Lotto newAutoLotto() {
List<LottoNumber> balls = LottoNumber.getBalls();
Collections.shuffle(balls);
Set<LottoNumber> randomBalls = new HashSet<>(balls.subList(NUMBER_COUNT_ZERO, NUMBER_COUNT));
return new Lotto(randomBalls);
}
public static boolean moreThanBallCount(int matchCount) {
return matchCount > NUMBER_COUNT;
}
public boolean contains(LottoNumber ball) {
return balls.contains(ball);
}
public int countCommonBalls(Lotto lotto) {
Set<LottoNumber> sameBalls = new HashSet<>(balls);
sameBalls.retainAll(lotto.balls);
return sameBalls.size();
}
public List<String> getLotto() {
List<String> ballsData = balls.stream()
.map(LottoNumber::toString)
.collect(Collectors.toList());
return Collections.unmodifiableList(ballsData);
}
}