ppj/dev/meowmeow/MatchCollection.java
2024-05-11 23:30:10 +02:00

58 lines
1.6 KiB
Java

/*
* TASK 6
* archived at https://git.femboy.science/femsci/ppj/src/branch/task/6
* by femsci
*/
package dev.meowmeow;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
public class MatchCollection {
public MatchCollection(List<Match> matches) {
_matches = matches;
}
private List<Match> _matches;
public List<Match> getMatches() {
return _matches;
}
public Map<String, Integer> getPointsDistribution() {
Map<String, Integer> pointMap = new HashMap<>();
Function<String, Consumer<Integer>> incrementBy = c -> i -> pointMap.put(c, pointMap.getOrDefault(c, 0) + i);
// calculate scores from each match
for (Match match : _matches) {
if (match.isDraw()) {
// increment by one for each participant during draw
incrementBy.apply(match.getParticipants()[0]).accept(1);
incrementBy.apply(match.getParticipants()[1]).accept(1);
continue;
}
incrementBy.apply(match.getWinner()).accept(3);
// ensure that the loser is embedded in the map
pointMap.putIfAbsent(match.getLoser(), 0);
}
return pointMap;
}
// parse this silly structure
public static MatchCollection Parse(String[][] data) {
return new MatchCollection(
Arrays.asList(data)
.stream()
.map(Match::Parse)
.toList());
}
}