diff options
Diffstat (limited to 'src/main/java/design/model/Golfer.java')
| -rw-r--r-- | src/main/java/design/model/Golfer.java | 57 |
1 files changed, 47 insertions, 10 deletions
diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 2e59987..3b4770d 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; @@ -27,8 +30,8 @@ public class Golfer { 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; + this.invites = invites; this.joinedTeam = joinedTeam; } @@ -39,8 +42,8 @@ public class Golfer { this.courses = new ArrayList<>(); this.rounds = new ArrayList<>(); this.clubs = new ArrayList<>(); - this.invites = new ArrayList<>(); this.nextClubId = 1; + this.invites = new ArrayList<>(); this.joinedTeam = null; } @@ -93,8 +96,8 @@ public class Golfer { } public Club addClub(Club c) { - c.setId(nextClubId++); clubs.add(c); + c.setId(nextClubId++); return c; } @@ -119,16 +122,50 @@ 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; } public boolean joinTeam(Team team){ |
