forked from next-step/java-lotto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinningLottery.java
39 lines (30 loc) · 1.35 KB
/
WinningLottery.java
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
package lottery;
import java.util.EnumMap;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
public class WinningLottery {
private final Lottery winningLottery;
private final LotteryNumber bonusLotteryNumber;
public WinningLottery(List<LotteryNumber> lotteryNumbers, LotteryNumber bonusLotteryNumber) {
this.winningLottery = new Lottery(lotteryNumbers);
validate(bonusLotteryNumber);
this.bonusLotteryNumber = bonusLotteryNumber;
}
private void validate(LotteryNumber bonusLotteryNumber) {
if (winningLottery.contains(bonusLotteryNumber)) {
throw new IllegalArgumentException("로또 번호는 중복될 수 없습니다.");
}
}
public LotteryResult result(List<Lottery> lotteries) {
return new LotteryResult(winningCounts(lotteries), LotteryCompany.LOTTERY_PRICE);
}
private EnumMap<LotteryRank, Integer> winningCounts(List<Lottery> lotteries) {
return new EnumMap<>(lotteries.stream()
.map(this::lotteryRank)
.collect(Collectors.toMap(Function.identity(), value -> 1, (existing, replacement) -> existing + 1)));
}
private LotteryRank lotteryRank(Lottery lottery) {
return LotteryRank.rank(winningLottery.contains(lottery), winningLottery.contains(bonusLotteryNumber));
}
}