-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathLotto.java
More file actions
43 lines (33 loc) · 1.32 KB
/
Lotto.java
File metadata and controls
43 lines (33 loc) · 1.32 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
package lotto;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Lotto {
public static final int LOTTO_PRICE = 1000;
public static final long LOTTO_NUM_COUNT = 6;
private final Set<LottoNumber> lottoNumbers;
public Lotto(List<Integer> numbers) {
this(new HashSet<>(numbers));
}
public Lotto(Set<Integer> numbers) {
this.lottoNumbers = numbers.stream().map(LottoNumber::of).collect(Collectors.toSet());
validateLottoNumbers();
}
public Set<LottoNumber> getLottoNumbers() {
return lottoNumbers;
}
public LottoRank getLottoRank(Lotto winningNumbers, int bonusNumber) {
int matchCount = (int) this.lottoNumbers.stream().filter(winningNumbers.getLottoNumbers()::contains).count();
boolean isBonusNumber = lottoNumbers.contains(LottoNumber.of(bonusNumber));
return LottoRank.fromMatchCount(matchCount, isBonusNumber);
}
private void validateLottoNumbers() {
if (lottoNumbers.size() > LOTTO_NUM_COUNT) {
throw new IllegalArgumentException("로또 번호는 6개여야 합니다.");
}
if (lottoNumbers.size() < LOTTO_NUM_COUNT) {
throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다.");
}
}
}