summaryrefslogtreecommitdiff
path: root/src/main/java/design
diff options
context:
space:
mode:
authorMichael Lizzio <mjl2396@rit.edu>2025-11-10 14:25:30 -0500
committerMichael Lizzio <mjl2396@rit.edu>2025-11-10 14:25:30 -0500
commita29285b3aaf19becc74ef5b2755052aed8782a57 (patch)
tree62c9677a0bb4ac48260e337ef35e6b9ed45cfb6e /src/main/java/design
parent873c9c81fab807127fb70e5abb88c8f96c9d8934 (diff)
downloaddesignproject-design-6-a29285b3aaf19becc74ef5b2755052aed8782a57.tar.gz
designproject-design-6-a29285b3aaf19becc74ef5b2755052aed8782a57.tar.bz2
designproject-design-6-a29285b3aaf19becc74ef5b2755052aed8782a57.zip
Added HolePlayMemento with sanpshots and restore
Diffstat (limited to '')
-rw-r--r--src/main/java/design/model/holeplay/HolePlayContext.java54
1 files changed, 49 insertions, 5 deletions
diff --git a/src/main/java/design/model/holeplay/HolePlayContext.java b/src/main/java/design/model/holeplay/HolePlayContext.java
index 16201b6..71fba32 100644
--- a/src/main/java/design/model/holeplay/HolePlayContext.java
+++ b/src/main/java/design/model/holeplay/HolePlayContext.java
@@ -113,15 +113,59 @@ public class HolePlayContext implements Originator {
this.strokes = s;
}
+ // Captures current State, current play (hole# + swings), and stroke counter
+ private static class HolePlayMemento implements Memento {
+ private final String stateName;
+ private final Play currentPlayCopy;
+ private final int strokesCopy;
+
+ HolePlayMemento(HolePlayContext ctx) {
+ this.stateName = ctx.state.name();
+
+ // Copy currentPlay if it exists
+ Play src = ctx.currentPlay;
+ if (src != null) {
+ var copySwings = new ArrayList<Swing>();
+ for (Swing s : src.getSwings()) {
+ copySwings.add(new Swing(s.getDistance(), s.getClubUsed()));
+ }
+ this.currentPlayCopy = new Play(src.getHoleNumber(), copySwings);
+ } else {
+ this.currentPlayCopy = null;
+ }
+
+ this.strokesCopy = ctx.strokes;
+ }
+ }
+
@Override
public Memento createMemento() {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("Unimplemented method 'createMemento'");
+ return new HolePlayMemento(this);
}
@Override
- public void restore(Memento memento) {
- // TODO Auto-generated method stub
- throw new UnsupportedOperationException("Unimplemented method 'restore'");
+ public void restore(Memento m) {
+ HolePlayMemento hm = (HolePlayMemento) m;
+
+ // restore strokes
+ this.strokes = hm.strokesCopy;
+
+ // restore currentPlay
+ if (hm.currentPlayCopy == null) {
+ this.currentPlay = null;
+ } else {
+ var copySwings = new ArrayList<Swing>();
+ for (Swing s : hm.currentPlayCopy.getSwings()) {
+ copySwings.add(new Swing(s.getDistance(), s.getClubUsed()));
+ }
+ this.currentPlay = new Play(hm.currentPlayCopy.getHoleNumber(), copySwings);
+ }
+
+ // restore state based on name
+ switch (hm.stateName) {
+ case "SetupState" -> this.state = new SetupState();
+ case "PlayState" -> this.state = new PlayState();
+ case "HoleCompleteState" -> this.state = new HoleCompleteState();
+ }
}
} \ No newline at end of file