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 --- .../java/design/model/holeplay/HolePlayContext.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'src/main/java/design/model/holeplay') diff --git a/src/main/java/design/model/holeplay/HolePlayContext.java b/src/main/java/design/model/holeplay/HolePlayContext.java index 7a5d8ef..16201b6 100644 --- a/src/main/java/design/model/holeplay/HolePlayContext.java +++ b/src/main/java/design/model/holeplay/HolePlayContext.java @@ -6,7 +6,10 @@ import java.util.ArrayList; import design.model.*; import design.persistence.PersonalDatabase; -public class HolePlayContext { +import design.model.undo.Memento; +import design.model.undo.Originator; + +public class HolePlayContext implements Originator { private final Golfer golfer; private final Round round; private final PersonalDatabase pdb; @@ -109,4 +112,16 @@ public class HolePlayContext { void setStrokes(int s) { this.strokes = s; } + + @Override + public Memento createMemento() { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'createMemento'"); + } + + @Override + public void restore(Memento memento) { + // TODO Auto-generated method stub + throw new UnsupportedOperationException("Unimplemented method 'restore'"); + } } \ No newline at end of file -- cgit v1.2.3 From a29285b3aaf19becc74ef5b2755052aed8782a57 Mon Sep 17 00:00:00 2001 From: Michael Lizzio Date: Mon, 10 Nov 2025 14:25:30 -0500 Subject: Added HolePlayMemento with sanpshots and restore --- .../design/model/holeplay/HolePlayContext.java | 54 ++++++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) (limited to 'src/main/java/design/model/holeplay') 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(); + 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(); + 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 -- cgit v1.2.3