diff options
| author | sowgro <tpoke.ferrari@gmail.com> | 2025-11-16 02:01:58 -0500 |
|---|---|---|
| committer | sowgro <tpoke.ferrari@gmail.com> | 2025-11-16 02:01:58 -0500 |
| commit | 969b8071dd799dfaed939e10bf77b8a1ad42f9dc (patch) | |
| tree | 214016dc742a155512fbb2f851cf589509f3b361 /src/main/java/design/model/Golfer.java | |
| parent | cf59d52cba70742f1d4098c38b4c7a798b3d89fa (diff) | |
| parent | 43530df067b1132b944e9619bdf60b72264829ec (diff) | |
| download | designproject-design-6-import-export.tar.gz designproject-design-6-import-export.tar.bz2 designproject-design-6-import-export.zip | |
Merge branch 'main' into import-exportimport-export
# Conflicts:
# data/personaldb.json
# src/main/java/design/controller/userinput/menus/MainMenu.java
# src/main/java/design/persistence/JSONLeagueDatabase.java
# src/main/java/design/persistence/JSONPersonalDatabase.java
# src/test/java/design/model/ClubTest.java
# src/test/java/design/model/GolferTest.java
Diffstat (limited to '')
| -rw-r--r-- | src/main/java/design/model/Golfer.java | 58 |
1 files changed, 46 insertions, 12 deletions
diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 1c4e669..84f7396 100644 --- a/src/main/java/design/model/Golfer.java +++ b/src/main/java/design/model/Golfer.java @@ -7,8 +7,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; @@ -16,20 +19,18 @@ 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; } @@ -40,7 +41,6 @@ public class Golfer { this.courses = new ArrayList<>(); this.rounds = new ArrayList<>(); this.clubs = new ArrayList<>(); - this.invites = new ArrayList<>(); this.nextClubId = 1; } @@ -93,8 +93,8 @@ public class Golfer { } public Club addClub(Club c) { - c.setId(nextClubId++); clubs.add(c); + c.setId(nextClubId++); return c; } @@ -119,15 +119,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; } } |
