summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/main/java/design/controller/userinput/Menu.java2
-rw-r--r--src/main/java/design/controller/userinput/menus/SelectLeague.java2
-rw-r--r--src/main/java/design/model/Golfer.java24
-rw-r--r--src/main/java/design/model/League.java29
-rw-r--r--src/main/java/design/model/ScrambleLeague.java50
-rw-r--r--src/main/java/design/model/StrokeLeague.java42
-rw-r--r--src/main/java/design/model/Team.java31
7 files changed, 164 insertions, 16 deletions
diff --git a/src/main/java/design/controller/userinput/Menu.java b/src/main/java/design/controller/userinput/Menu.java
index ab633e1..01e80a9 100644
--- a/src/main/java/design/controller/userinput/Menu.java
+++ b/src/main/java/design/controller/userinput/Menu.java
@@ -23,7 +23,7 @@ public abstract class Menu {
try {
int i = Integer.parseInt(line) - 1;
menuOptions.get(i).onAction();
- } catch (IndexOutOfBoundsException ex) {
+ } catch (IndexOutOfBoundsException | NumberFormatException ex) {
System.err.printf("Invalid option \"%s\"\n", line);
present();
}
diff --git a/src/main/java/design/controller/userinput/menus/SelectLeague.java b/src/main/java/design/controller/userinput/menus/SelectLeague.java
index 08f6c91..e1675e9 100644
--- a/src/main/java/design/controller/userinput/menus/SelectLeague.java
+++ b/src/main/java/design/controller/userinput/menus/SelectLeague.java
@@ -26,7 +26,7 @@ public class SelectLeague extends Menu {
public List<MenuOption> getMenuOptions() {
List<MenuOption> options = new ArrayList<>();
for (League l : LeagueDatabase.instance().getLeagues()) {
- options.add(new MenuOption(l.getName(), () -> {
+ options.add(new MenuOption(l.toString(), () -> {
if (l instanceof ScrambleLeague sl && sl.locateTeam(golfer) == null) {
Scanner sc = new Scanner(System.in);
System.out.print("You are not a member of a team for this league. Would you like to create one (Y/n): ");
diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java
index 84f7396..4fb521e 100644
--- a/src/main/java/design/model/Golfer.java
+++ b/src/main/java/design/model/Golfer.java
@@ -19,12 +19,14 @@ 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;
@@ -32,6 +34,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) {
@@ -42,6 +46,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() {
@@ -164,4 +170,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 881e25f..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);
@@ -87,4 +93,23 @@ public abstract class League {
assert this.id == -1;
this.id = id;
}
+
+ public abstract String getType();
+
+ @Override
+ 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 d372264..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() {
@@ -43,4 +53,32 @@ public class ScrambleLeague extends League {
}
return null;
}
+
+ @Override
+ 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 2b787b5..d051fd0 100644
--- a/src/main/java/design/model/StrokeLeague.java
+++ b/src/main/java/design/model/StrokeLeague.java
@@ -4,33 +4,65 @@ 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() {
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 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);
+ }
}