diff options
Diffstat (limited to 'src/main/java/design')
| -rw-r--r-- | src/main/java/design/model/Golfer.java | 21 | ||||
| -rw-r--r-- | src/main/java/design/model/League.java | 23 | ||||
| -rw-r--r-- | src/main/java/design/model/ScrambleLeague.java | 45 | ||||
| -rw-r--r-- | src/main/java/design/model/StrokeLeague.java | 39 | ||||
| -rw-r--r-- | src/main/java/design/model/Team.java | 31 |
5 files changed, 146 insertions, 13 deletions
diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 960568f..2e59987 100644 --- a/src/main/java/design/model/Golfer.java +++ b/src/main/java/design/model/Golfer.java @@ -16,10 +16,11 @@ public class Golfer { 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<Invite> invites) { + List<Club> clubs, List<Invite> invites, Team joinedTeam) { this.username = username; this.passwordHash = passwordHash; this.fullName = fullName; @@ -28,6 +29,7 @@ public class Golfer { this.clubs = clubs; this.invites = invites != null ? invites : new ArrayList<>(); this.nextClubId = this.clubs.stream().mapToInt(Club::getId).max().orElse(0) + 1; + this.joinedTeam = joinedTeam; } public Golfer(String fullName, String username, String password) { @@ -39,6 +41,7 @@ public class Golfer { this.clubs = new ArrayList<>(); this.invites = new ArrayList<>(); this.nextClubId = 1; + this.joinedTeam = null; } public String getUsername() { @@ -127,4 +130,20 @@ public class Golfer { public Invite[] getInvites() { return invites.toArray(Invite[]::new); } + + 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 6dab033..1f4c9cb 100644 --- a/src/main/java/design/model/League.java +++ b/src/main/java/design/model/League.java @@ -14,6 +14,7 @@ public abstract class League { private final Date endDate; private final Golfer owner; private List<Match> schedule; + private boolean completed; @JsonCreator protected League(int id, String name, Date registrationDate, Date startDate, Date endDate, Golfer owner, List<Match> schedule) { @@ -24,6 +25,7 @@ public abstract class League { this.endDate = endDate; this.owner = owner; this.schedule = schedule; + this.completed = false; } public League(String name, Date registrationDate, Date startDate, Date endDate, Golfer owner) { @@ -34,6 +36,7 @@ public abstract class League { this.endDate = endDate; this.owner = owner; this.schedule = new ArrayList<>(); + this.completed = false; } public int getId() { @@ -64,9 +67,12 @@ public abstract class League { return schedule; } + 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); @@ -76,4 +82,17 @@ public abstract class League { assert this.id == -1; this.id = id; } + + 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 92e7099..55f9893 100644 --- a/src/main/java/design/model/ScrambleLeague.java +++ b/src/main/java/design/model/ScrambleLeague.java @@ -3,32 +3,67 @@ 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 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() { return participants.toArray(Team[]::new); } + + @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 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<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() { 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<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); + } } |
