From 4d1ac032652e857a6d6bd63cadad0727ddb3a345 Mon Sep 17 00:00:00 2001 From: Jacob Shimp Date: Tue, 11 Nov 2025 19:32:27 -0500 Subject: potential implementation for playing in a league --- src/main/java/design/model/StrokeLeague.java | 39 ++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src/main/java/design/model/StrokeLeague.java') diff --git a/src/main/java/design/model/StrokeLeague.java b/src/main/java/design/model/StrokeLeague.java index f09cabf..487a2b5 100644 --- a/src/main/java/design/model/StrokeLeague.java +++ b/src/main/java/design/model/StrokeLeague.java @@ -3,32 +3,61 @@ package design.model; import com.fasterxml.jackson.annotation.JsonCreator; 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; public class StrokeLeague extends League { - private final List participants; + private List participants; + private 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 e) { - return participants.add(e); + public boolean addParticipants(Golfer g) { + boolean added = participants.add(g); + if(added) totalStrokes.putIfAbsent(g, 0); + return added; } - public boolean removeParticipants(Golfer o) { - return participants.remove(o); + public boolean removeParticipants(Golfer g) { + totalStrokes.remove(g); + return participants.remove(g); } public Golfer[] getParticipants() { return participants.toArray(Golfer[]::new); } + + @Override + public void recordPlay(Golfer player, Match match, Round round){ + if(!isPlayable() || !participants.contains(player)) return; + int strokes = round.getTotalSwings(); + totalStrokes.merge(player, strokes, Integer::sum); + match.addRound(round); + } + + @Override + public void finalizeLeague(){ + markCompleted(); + participants.sort(Comparator.comparingInt(totalStrokes::get)); + } + + public Map getTotalStrokes(){ + return Collections.unmodifiableMap(totalStrokes); + } } -- cgit v1.2.3