forked from knh4437/java-lotto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLottoRanks.java
More file actions
44 lines (37 loc) · 1.29 KB
/
LottoRanks.java
File metadata and controls
44 lines (37 loc) · 1.29 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
package lotto.domain;
import java.util.Arrays;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.function.Function;
import java.util.stream.Collectors;
public class LottoRanks {
private final Map<LottoRank, Integer> ranks;
private LottoRanks(Map<LottoRank, Integer> ranks) {
this.ranks = ranks;
}
public static LottoRanks from(List<LottoRank> ranks) {
return new LottoRanks(
ranks.stream().collect(Collectors.toMap(Function.identity(), rank -> 1, Integer::sum))
);
}
public int getProfit() {
return this.ranks.entrySet()
.stream()
.mapToInt(entry -> entry.getKey().getPrize() * entry.getValue())
.sum();
}
@Override
public String toString() {
Arrays.stream(LottoRank.values())
.forEach(rank -> this.ranks.computeIfAbsent(rank, value -> 0));
return ranks.entrySet()
.stream()
.filter(entry -> entry.getKey().isNotOtherRank())
.sorted(Entry.comparingByKey())
.map(entry -> String.format("%s - %d개", entry.getKey(), entry.getValue()))
.collect(Collectors.joining("\n"));
}
}