From 873c9c81fab807127fb70e5abb88c8f96c9d8934 Mon Sep 17 00:00:00 2001 From: Michael Lizzio Date: Mon, 10 Nov 2025 14:19:24 -0500 Subject: Added GolferMememto, updated Golfer --- src/main/java/design/model/Golfer.java | 51 +++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'src/main/java/design/model/Golfer.java') diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 48daae8..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; @@ -112,4 +115,50 @@ public class Golfer { public void removeClub(Club c) { clubs.remove(c); } + + // 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 courses; + private final List rounds; + private final List 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; + } + } + + @Override + public Memento createMemento() { + return new GolferMemento(this); + } + + @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; + } } -- cgit v1.2.3 From 43530df067b1132b944e9619bdf60b72264829ec Mon Sep 17 00:00:00 2001 From: sowgro Date: Thu, 13 Nov 2025 08:22:39 -0500 Subject: fix constructor --- src/main/java/design/model/Golfer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/main/java/design/model/Golfer.java') diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java index 0f4c914..870b460 100644 --- a/src/main/java/design/model/Golfer.java +++ b/src/main/java/design/model/Golfer.java @@ -89,9 +89,9 @@ public class Golfer implements Originator { rounds.add(round); } - public Club addClub(String manufacture, String nickname, Club.ClubType type) { - Club c = new Club(nextClubId++, manufacture, nickname, type); + public Club addClub(Club c) { clubs.add(c); + c.setId(nextClubId++); return c; } -- cgit v1.2.3