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/holeplay | |
| 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/holeplay/HolePlayContext.java | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/main/java/design/model/holeplay/HolePlayContext.java b/src/main/java/design/model/holeplay/HolePlayContext.java index 7a5d8ef..71fba32 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,60 @@ public class HolePlayContext { void setStrokes(int s) { 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() { + return new HolePlayMemento(this); + } + + @Override + 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 |
