diff options
| author | Jacob Shimp <jrs9538@g.rit.edu> | 2025-10-05 14:35:04 -0400 |
|---|---|---|
| committer | Jacob Shimp <jrs9538@g.rit.edu> | 2025-10-05 14:35:04 -0400 |
| commit | ea6a4afbe65ff6e26b568345efbb564a0a86abc9 (patch) | |
| tree | c815add1a75fed21f129845bc170b12cdfdca0cf /src/main/java/design/model/holeplay/HolePlayContext.java | |
| parent | b4919667c3455d8b517df6a72b7d37e0734d9155 (diff) | |
| parent | c5cbe4ffd56b7c96d85dc1676387993c6c15af7b (diff) | |
| download | designproject-design-6-ea6a4afbe65ff6e26b568345efbb564a0a86abc9.tar.gz designproject-design-6-ea6a4afbe65ff6e26b568345efbb564a0a86abc9.tar.bz2 designproject-design-6-ea6a4afbe65ff6e26b568345efbb564a0a86abc9.zip | |
merged from main
Diffstat (limited to 'src/main/java/design/model/holeplay/HolePlayContext.java')
| -rw-r--r-- | src/main/java/design/model/holeplay/HolePlayContext.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/main/java/design/model/holeplay/HolePlayContext.java b/src/main/java/design/model/holeplay/HolePlayContext.java new file mode 100644 index 0000000..7a5d8ef --- /dev/null +++ b/src/main/java/design/model/holeplay/HolePlayContext.java @@ -0,0 +1,112 @@ +package design.model.holeplay; + +import java.io.IOException; +import java.util.ArrayList; + +import design.model.*; +import design.persistence.PersonalDatabase; + +public class HolePlayContext { + private final Golfer golfer; + private final Round round; + private final PersonalDatabase pdb; + + private HoleState state; + private Play currentPlay; + private int strokes; + + public HolePlayContext(Golfer golfer, Round round, PersonalDatabase pdb) { + this.golfer = golfer; + this.round = round; + this.pdb = pdb; + setState(new SetupState()); + } + + public void setState(HoleState next) { + this.state = next; + next.enter(this); + } + + // API the PTUI/Commands will call: + public void startHole() { + state.handleStart(this); + } + + public void recordShot(Club club, Integer distanceYds) { + state.handleShot(this, club, distanceYds); + } + + public void holeOut() { + state.handleHoleOut(this); + } + + void beginNewPlay(int holeNumber) { + this.currentPlay = new Play(holeNumber); + this.strokes = 0; + } + + void addSwing(Club club, Integer distanceYds) { + // Checks golfer owns club maybe not necessary + // if (golfer.hasClub(club)) { + // throw new IllegalArgumentException("Club not in golfer's bag"); + // } + int d = (distanceYds == null ? 0 : distanceYds); + this.currentPlay.addSwing(new Swing(d, club)); + this.strokes++; + + } + + void finalizePlayAndPersist() { + round.addPlay(currentPlay); + round.nextHole(); + currentPlay = null; + strokes = 0; + // Not sure if try catch is necessary + try { + pdb.updateGolfer(golfer); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + public void clearInProgressPlay() { + this.setCurrentPlay(null); + this.setStrokes(0); + } + + public void endRoundNow() { + try { + pdb.updateGolfer(golfer); // persist whatever’s done so far + } catch (IOException e) { + e.printStackTrace(); + } + setState(new SetupState()); + } + + // getters you’ll want: + public Golfer getGolfer() { + return golfer; + } + + public Round getRound() { + return round; + } + + public Play getCurrentPlay() { + return currentPlay; + } + + public int getStrokes() { + return strokes; + } + + // setters used internally + void setCurrentPlay(Play p) { + this.currentPlay = p; + } + + void setStrokes(int s) { + this.strokes = s; + } +}
\ No newline at end of file |
