forked from woowacourse-precourse/java-lotto-6
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathWinningResultModel.java
More file actions
46 lines (38 loc) · 1.79 KB
/
WinningResultModel.java
File metadata and controls
46 lines (38 loc) · 1.79 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
package lotto.model;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
public class WinningResultModel {
private final Map<RankEnum, Integer> result = new EnumMap<>(RankEnum.class);
public WinningResultModel() {
for (RankEnum rank : RankEnum.values()) {
result.put(rank, 0);
}
}
public void checkWinning(List<LottoModel> purchasedLottos, Set<Integer> winningNumbers, int bonusNumber) {
for (LottoModel lotto : purchasedLottos) {
Set<Integer> intersection = new HashSet<>(lotto.getNumbers());
intersection.retainAll(winningNumbers);
int matchCount = intersection.size();
boolean matchBonus = lotto.getNumbers().contains(bonusNumber);
RankEnum rank = RankEnum.valueOf(matchCount, matchBonus);
result.put(rank, result.get(rank) + 1);
}
}
public void printWinningResult() {
System.out.println("당첨 통계\n---");
System.out.printf("3개 일치 (5,000원) - %d개%n", result.get(RankEnum.FIFTH));
System.out.printf("4개 일치 (50,000원) - %d개%n", result.get(RankEnum.FOURTH));
System.out.printf("5개 일치 (1,500,000원) - %d개%n", result.get(RankEnum.THIRD));
System.out.printf("5개 일치, 보너스 볼 일치 (30,000,000원) - %d개%n", result.get(RankEnum.SECOND));
System.out.printf("6개 일치 (2,000,000,000원) - %d개%n", result.get(RankEnum.FIRST));
}
public double calculateProfitRate(int purchaseAmount) {
int totalWinnings = result.entrySet().stream()
.mapToInt(entry -> entry.getKey().getPrize() * entry.getValue())
.sum();
return ((double) totalWinnings / purchaseAmount) * 100;
}
}