package design.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonTypeName; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @JsonTypeName("stroke") public class StrokeLeague extends League { private final List participants; private final Map totalStrokes; @JsonCreator private StrokeLeague(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List participants, List schedule) { super(id, name, registrationDate, startDate, endDate, owner, schedule); this.participants = participants; this.totalStrokes = new HashMap<>(); participants.forEach(p -> totalStrokes.putIfAbsent(p, 0)); } public StrokeLeague(String name, Date registrationDate, Date startDate, Date endDate, Golfer owner) { super(name, registrationDate, startDate, endDate, owner); this.participants = new ArrayList<>(); this.totalStrokes = new HashMap<>(); } public boolean addParticipants(Golfer g) { totalStrokes.putIfAbsent(g, 0); return participants.add(g); } public boolean removeParticipants(Golfer g) { totalStrokes.remove(g); return participants.remove(g); } public Golfer[] getParticipants() { return participants.toArray(Golfer[]::new); } @Override public String getType() { return "stroke"; } public void recordPlay(Golfer player, Match match, Round round){ if(!isPlayable() || !participants.contains(player)) return; int index = participants.indexOf(player); totalStrokes.merge(player, round.getTotalSwings(), Integer::sum); match.addRound(round); match.addRoundFor(index, round); } @Override public void finalizeLeague(){ markCompleted(); participants.sort(Comparator.comparingInt(totalStrokes::get)); } public Map getTotalStrokes(){ return Collections.unmodifiableMap(totalStrokes); } @Override public Map getResults() { return getTotalStrokes(); } // @Override // public void finalizeMatch(Match match) { // getCompletedMatches().add(match); // for(int i = 0; i < participants.size(); i++){ // Golfer player = participants.get(i); // Round round = match.getRoundFor(i); // if(round != null){ // player.addRound(round); // } // } // } }