diff options
| author | Jacob Shimp <jrs9538@g.rit.edu> | 2025-11-13 08:51:31 -0500 |
|---|---|---|
| committer | Jacob Shimp <jrs9538@g.rit.edu> | 2025-11-13 08:51:31 -0500 |
| commit | f79ab927050250c4b7e63a4fbd37034d4eeae8cd (patch) | |
| tree | 2e86ca8a13453baff59c2ca01df58be8ee18226b | |
| parent | 0294bd3a3121d6166788594f7249bf293cb00896 (diff) | |
| parent | 4d1ac032652e857a6d6bd63cadad0727ddb3a345 (diff) | |
| download | designproject-design-6-f79ab927050250c4b7e63a4fbd37034d4eeae8cd.tar.gz designproject-design-6-f79ab927050250c4b7e63a4fbd37034d4eeae8cd.tar.bz2 designproject-design-6-f79ab927050250c4b7e63a4fbd37034d4eeae8cd.zip | |
merge league-play into league-model
Diffstat (limited to '')
| -rw-r--r-- | src/main/java/design/model/Golfer.java | 24 | ||||
| -rw-r--r-- | src/main/java/design/model/League.java | 22 | ||||
| -rw-r--r-- | src/main/java/design/model/ScrambleLeague.java | 45 | ||||
| -rw-r--r-- | src/main/java/design/model/StrokeLeague.java | 37 | ||||
| -rw-r--r-- | src/main/java/design/model/Team.java | 31 |
5 files changed, 145 insertions, 14 deletions
diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 870b460..3b4770d 100644 --- a/src/main/java/design/model/Golfer.java +++ b/src/main/java/design/model/Golfer.java @@ -18,10 +18,12 @@ public class Golfer implements Originator { private final List<Round> rounds; private final List<Club> clubs; // Keep track of golfer's clubs private int nextClubId; + private final List<Invite> invites; + private Team joinedTeam; @JsonCreator private Golfer(String username, int passwordHash, String fullName, List<Course> courses, List<Round> rounds, - List<Club> clubs) { + List<Club> clubs, List<Invite> invites, Team joinedTeam) { this.username = username; this.passwordHash = passwordHash; this.fullName = fullName; @@ -29,6 +31,8 @@ public class Golfer implements Originator { this.rounds = rounds; this.clubs = clubs; this.nextClubId = this.clubs.stream().mapToInt(Club::getId).max().orElse(0) + 1; + this.invites = invites; + this.joinedTeam = joinedTeam; } public Golfer(String fullName, String username, String password) { @@ -39,6 +43,8 @@ public class Golfer implements Originator { this.rounds = new ArrayList<>(); this.clubs = new ArrayList<>(); this.nextClubId = 1; + this.invites = new ArrayList<>(); + this.joinedTeam = null; } public String getUsername() { @@ -161,4 +167,20 @@ public class Golfer implements Originator { this.nextClubId = gm.nextClubId; } + + public boolean joinTeam(Team team){ + for(Invite invite : invites){ + if(invite.getTeam().equals(team)){ + this.joinedTeam = team; + team.addMember(this); + invites.remove(invite); + return true; + } + } + return false; + } + + public Team getTeam(){ + return joinedTeam; + } } diff --git a/src/main/java/design/model/League.java b/src/main/java/design/model/League.java index fd756f9..8793c86 100644 --- a/src/main/java/design/model/League.java +++ b/src/main/java/design/model/League.java @@ -25,6 +25,7 @@ public abstract class League { private final Date endDate; private final Golfer owner; private final List<Match> schedule; + private boolean completed; @JsonCreator protected League(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List<Match> schedule) { @@ -35,6 +36,7 @@ public abstract class League { this.endDate = endDate; this.owner = owner; this.schedule = schedule != null ? schedule : new ArrayList<>(); + this.completed = false; } public League(String name, Date registrationDate, Date startDate, Date endDate, Golfer owner) { @@ -45,6 +47,7 @@ public abstract class League { this.endDate = endDate; this.owner = owner; this.schedule = new ArrayList<>(); + this.completed = false; } public int getId() { @@ -75,9 +78,12 @@ public abstract class League { return schedule.toArray(Match[]::new); } + public boolean isCompleted() { + return completed; + } + public void addMatchToSchedule(Match match) { - Date date = match.getDateScheduled(); - if(date.after(endDate)){ + if(match.getDateScheduled().after(endDate)){ throw new IllegalArgumentException("Cannot create match after league has ended"); } schedule.add(match); @@ -94,4 +100,16 @@ public abstract class League { public String toString() { return String.format("%s - %s", name, getType()); } + public boolean isPlayable() { + Date now = new Date(); + return now.after(startDate) && now.before(endDate); + } + + public void markCompleted(){ + this.completed = true; + } + + public abstract void recordPlay(Golfer player, Match match, Round round); + + public abstract void finalizeLeague(); } diff --git a/src/main/java/design/model/ScrambleLeague.java b/src/main/java/design/model/ScrambleLeague.java index ad115d4..1fb8f1d 100644 --- a/src/main/java/design/model/ScrambleLeague.java +++ b/src/main/java/design/model/ScrambleLeague.java @@ -4,31 +4,41 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonTypeName; import java.util.ArrayList; -import java.util.Arrays; +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("scramble") public class ScrambleLeague extends League { - private final List<Team> participants; + private List<Team> participants; + private Map<Team, Integer> totalTeamScores; @JsonCreator private ScrambleLeague(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List<Team> participants, List<Match> schedule) { super(id, name, registrationDate, startDate, endDate, owner, schedule); this.participants = participants; + this.totalTeamScores = new HashMap<>(); + participants.forEach(t -> totalTeamScores.putIfAbsent(t, 0)); } public ScrambleLeague(String name, Date registrationDate, Date startDate, Date endDate, Golfer owner) { super(name, registrationDate, startDate, endDate, owner); this.participants = new ArrayList<>(); + this.totalTeamScores = new HashMap<>(); } - public boolean addParticipants(Team e) { - return participants.add(e); + public boolean addParticipants(Team t) { + boolean added = participants.add(t); + if(added) totalTeamScores.putIfAbsent(t, 0); + return added; } - public boolean removeParticipants(Team o) { - return participants.remove(o); + public boolean removeParticipants(Team t) { + totalTeamScores.remove(t); + return participants.remove(t); } public Team[] getParticipants() { @@ -48,4 +58,27 @@ public class ScrambleLeague extends League { public String getType() { return "scramble"; } + @Override + public void recordPlay(Golfer player, Match match, Round round){ + if(!isPlayable()) return; + Team team = player.getTeam(); + if (team == null) return; + int strokes = round.getTotalSwings(); + team.addMemberRound(player, strokes); + match.addRound(round); + } + + @Override + public void finalizeLeague(){ + markCompleted(); + for(Team team : participants){ + int score = team.computeTotalBestScore(); + totalTeamScores.put(team, score); + } + participants.sort(Comparator.comparingInt(totalTeamScores::get)); + } + + public Map<Team, Integer> getTotalTeamScores(){ + return Collections.unmodifiableMap(totalTeamScores); + } } diff --git a/src/main/java/design/model/StrokeLeague.java b/src/main/java/design/model/StrokeLeague.java index 508198e..d051fd0 100644 --- a/src/main/java/design/model/StrokeLeague.java +++ b/src/main/java/design/model/StrokeLeague.java @@ -4,30 +4,41 @@ 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<Golfer> participants; + private List<Golfer> participants; + private Map<Golfer, Integer> totalStrokes; @JsonCreator private StrokeLeague(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List<Golfer> participants, List<Match> 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() { @@ -38,4 +49,20 @@ public class StrokeLeague extends League { public String getType() { return "stroke"; } + 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<Golfer, Integer> getTotalStrokes(){ + return Collections.unmodifiableMap(totalStrokes); + } } diff --git a/src/main/java/design/model/Team.java b/src/main/java/design/model/Team.java index 53b276e..8f30468 100644 --- a/src/main/java/design/model/Team.java +++ b/src/main/java/design/model/Team.java @@ -1,17 +1,22 @@ package design.model; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Objects; public class Team { private String name; private final List<Golfer> members; + private final Map<Golfer, Integer> memberBestRounds; private final Golfer owner; public Team(String name, Golfer owner) { this.name = name; this.owner = owner; this.members = new ArrayList<>(); + this.memberBestRounds = new HashMap<>(); } public String getName() { @@ -27,14 +32,40 @@ public class Team { } public boolean addMember(Golfer golfer) { + if(members.size() >= 4){ + throw new IllegalArgumentException("Team size limit reached!"); + } return members.add(golfer); } public boolean removeMember(Golfer g) { + memberBestRounds.remove(g); return members.remove(g); } public Golfer getOwner() { return owner; } + + public void addMemberRound(Golfer g, int strokes){ + memberBestRounds.merge(g, strokes, Math::min); + } + + public int computeTotalBestScore(){ + if(memberBestRounds.isEmpty()) return 100; // Bad Score Penalty for not playing + return memberBestRounds.values().stream().min(Integer::compareTo).orElse(100); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + Team other = (Team) obj; + return Objects.equals(name, other.name) && Objects.equals(owner, other.owner); + } + + @Override + public int hashCode() { + return Objects.hash(name, owner); + } } |
