diff options
Diffstat (limited to '')
| -rw-r--r-- | src/main/java/design/model/Golfer.java | 60 |
1 files changed, 47 insertions, 13 deletions
diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 960568f..0f4c914 100644 --- a/src/main/java/design/model/Golfer.java +++ b/src/main/java/design/model/Golfer.java @@ -6,8 +6,11 @@ import com.fasterxml.jackson.annotation.JsonPropertyOrder; import java.util.ArrayList; import java.util.List; +import design.model.undo.Memento; +import design.model.undo.Originator; + @JsonPropertyOrder({ "clubs", "nextClubId" }) -public class Golfer { +public class Golfer implements Originator { private String username; private int passwordHash; private String fullName; @@ -15,18 +18,16 @@ public class Golfer { private final List<Round> rounds; private final List<Club> clubs; // Keep track of golfer's clubs private int nextClubId; - private final List<Invite> invites; @JsonCreator private Golfer(String username, int passwordHash, String fullName, List<Course> courses, List<Round> rounds, - List<Club> clubs, List<Invite> invites) { + List<Club> clubs) { this.username = username; this.passwordHash = passwordHash; this.fullName = fullName; this.courses = courses; this.rounds = rounds; this.clubs = clubs; - this.invites = invites != null ? invites : new ArrayList<>(); this.nextClubId = this.clubs.stream().mapToInt(Club::getId).max().orElse(0) + 1; } @@ -37,7 +38,6 @@ public class Golfer { this.courses = new ArrayList<>(); this.rounds = new ArrayList<>(); this.clubs = new ArrayList<>(); - this.invites = new ArrayList<>(); this.nextClubId = 1; } @@ -89,8 +89,8 @@ public class Golfer { rounds.add(round); } - public Club addClub(Club c) { - c.setId(nextClubId++); + public Club addClub(String manufacture, String nickname, Club.ClubType type) { + Club c = new Club(nextClubId++, manufacture, nickname, type); clubs.add(c); return c; } @@ -116,15 +116,49 @@ public class Golfer { clubs.remove(c); } - public boolean addInvite(Invite invite) { - return invites.add(invite); + // Takes a snapshot of the golfer + private static class GolferMemento implements Memento { + private final String username; + private final int passwordHash; + private final String fullName; + private final List<Course> courses; + private final List<Round> rounds; + private final List<Club> clubs; + private final int nextClubId; + + GolferMemento(Golfer g) { + this.username = g.username; + this.passwordHash = g.passwordHash; + this.fullName = g.fullName; + this.courses = new ArrayList<>(g.courses); + this.rounds = new ArrayList<>(g.rounds); + this.clubs = new ArrayList<>(g.clubs); + this.nextClubId = g.nextClubId; + } } - public boolean removeInvite(Invite o) { - return invites.remove(o); + @Override + public Memento createMemento() { + return new GolferMemento(this); } - public Invite[] getInvites() { - return invites.toArray(Invite[]::new); + @Override + public void restore(Memento memento) { + GolferMemento gm = (GolferMemento) memento; + + this.username = gm.username; + this.passwordHash = gm.passwordHash; + this.fullName = gm.fullName; + + this.courses.clear(); + this.courses.addAll(gm.courses); + + this.rounds.clear(); + this.rounds.addAll(gm.rounds); + + this.clubs.clear(); + this.clubs.addAll(gm.clubs); + + this.nextClubId = gm.nextClubId; } } |
