-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathLotto.java
More file actions
53 lines (45 loc) · 2.02 KB
/
Copy pathLotto.java
File metadata and controls
53 lines (45 loc) · 2.02 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
package lotto.model;
import static lotto.utils.ExceptionConstants.DUPLICATED_LOTTO_NUMBER;
import static lotto.utils.ExceptionConstants.INVALID_LOTTO_RANGE;
import static lotto.utils.LottoConstants.LOWER_BOUND_NUMBER;
import static lotto.utils.LottoConstants.UPPER_BOUND_NUMBER;
import java.util.List;
import java.util.stream.Stream;
import lotto.utils.LottoPrize;
import lotto.utils.Utility;
public class Lotto {
private final List<Integer> numbers;
public Lotto(List<Integer> numbers) {
validate(numbers);
this.numbers = numbers;
}
private void validate(List<Integer> numbers) {
if (numbers.size() != 6) {
throw new IllegalArgumentException("[ERROR] 로또 번호는 6개여야 합니다.");
}
if (Utility.hasDuplicatedValue(numbers)) {
throw new IllegalArgumentException(DUPLICATED_LOTTO_NUMBER.getMessage());
}
if (!Utility.isInRange(numbers, LOWER_BOUND_NUMBER, UPPER_BOUND_NUMBER)) {
throw new IllegalArgumentException(INVALID_LOTTO_RANGE.getMessage());
}
}
public LottoPrize checkWinning(List<Integer> winningNumbers, int bonusNumber) {
List<Integer> totalWinningNumber = Stream.concat(winningNumbers.stream(), Stream.of(bonusNumber)).toList();
int matchCount = (int) numbers.stream().filter(totalWinningNumber::contains).count();
boolean matchBonus = numbers.contains(bonusNumber);
return LottoPrize.getLottoPrize(matchCount, matchBonus);
}
public void validateAdditionalNumber(int value) {
List<Integer> tempNumbers = Stream.concat(numbers.stream(), Stream.of(value)).toList();
if (Utility.hasDuplicatedValue(tempNumbers)) {
throw new IllegalArgumentException(DUPLICATED_LOTTO_NUMBER.getMessage());
}
if (!Utility.isInRange(value, LOWER_BOUND_NUMBER, UPPER_BOUND_NUMBER)) {
throw new IllegalArgumentException(INVALID_LOTTO_RANGE.getMessage());
}
}
public List<Integer> getNumbers() {
return numbers;
}
}