From 4c485841954cb566d82b2e14a0cddf347a3d8e18 Mon Sep 17 00:00:00 2001 From: Michael Lizzio Date: Sun, 5 Oct 2025 03:37:44 -0400 Subject: Created holepaly in model --- src/main/java/design/model/holeplay/HolePlayContext.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/main/java/design/model/holeplay/HolePlayContext.java (limited to 'src/main/java/design/model/holeplay/HolePlayContext.java') 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..e69de29 -- cgit v1.2.3 From fc8584b1defdbfeaf80ad3e0c7bdb9f53cf2929c Mon Sep 17 00:00:00 2001 From: Michael Lizzio Date: Sun, 5 Oct 2025 03:57:37 -0400 Subject: Completed first take of holeplay logic and added empty test files --- .../design/model/holeplay/HolePlayContext.java | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) (limited to 'src/main/java/design/model/holeplay/HolePlayContext.java') diff --git a/src/main/java/design/model/holeplay/HolePlayContext.java b/src/main/java/design/model/holeplay/HolePlayContext.java index e69de29..7445073 100644 --- a/src/main/java/design/model/holeplay/HolePlayContext.java +++ b/src/main/java/design/model/holeplay/HolePlayContext.java @@ -0,0 +1,84 @@ +package design.model.holeplay; + +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; + private int distanceRemainingYds; + + 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); + } + + // package-private helpers the states use: + void beginNewPlay(int holeNumber, int par) { + /* create Play, reset counters */ } + + void addSwing(Club club, Integer distanceYds) { + /* add Swing to currentPlay; strokes++; distanceRemainingYds -= distanceYds */ } + + void finalizePlayAndPersist() { + /* round.addPlay(currentPlay); currentPlay=null; pdb.updateGolfer(golfer); */ } + + // getters you’ll want: + public Golfer getGolfer() { + return golfer; + } + + public Round getRound() { + return round; + } + + public Play getCurrentPlay() { + return currentPlay; + } + + public int getStrokes() { + return strokes; + } + + public int getDistanceRemainingYds() { + return distanceRemainingYds; + } + + // setters used internally + void setCurrentPlay(Play p) { + this.currentPlay = p; + } + + void setStrokes(int s) { + this.strokes = s; + } + + void setDistanceRemainingYds(int d) { + this.distanceRemainingYds = d; + } +} \ No newline at end of file -- cgit v1.2.3 From 40470b1788c28f1c95bba7339dd9819b0e9b95cc Mon Sep 17 00:00:00 2001 From: Michael Lizzio Date: Sun, 5 Oct 2025 05:29:38 -0400 Subject: Updated logic and completed holeplay subsystem --- .../design/model/holeplay/HolePlayContext.java | 56 ++++++++++++++++------ 1 file changed, 42 insertions(+), 14 deletions(-) (limited to 'src/main/java/design/model/holeplay/HolePlayContext.java') diff --git a/src/main/java/design/model/holeplay/HolePlayContext.java b/src/main/java/design/model/holeplay/HolePlayContext.java index 7445073..7a5d8ef 100644 --- a/src/main/java/design/model/holeplay/HolePlayContext.java +++ b/src/main/java/design/model/holeplay/HolePlayContext.java @@ -1,5 +1,8 @@ package design.model.holeplay; +import java.io.IOException; +import java.util.ArrayList; + import design.model.*; import design.persistence.PersonalDatabase; @@ -11,7 +14,6 @@ public class HolePlayContext { private HoleState state; private Play currentPlay; private int strokes; - private int distanceRemainingYds; public HolePlayContext(Golfer golfer, Round round, PersonalDatabase pdb) { this.golfer = golfer; @@ -38,15 +40,49 @@ public class HolePlayContext { state.handleHoleOut(this); } - // package-private helpers the states use: - void beginNewPlay(int holeNumber, int par) { - /* create Play, reset counters */ } + void beginNewPlay(int holeNumber) { + this.currentPlay = new Play(holeNumber); + this.strokes = 0; + } void addSwing(Club club, Integer distanceYds) { - /* add Swing to currentPlay; strokes++; distanceRemainingYds -= 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); currentPlay=null; pdb.updateGolfer(golfer); */ } + 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() { @@ -65,10 +101,6 @@ public class HolePlayContext { return strokes; } - public int getDistanceRemainingYds() { - return distanceRemainingYds; - } - // setters used internally void setCurrentPlay(Play p) { this.currentPlay = p; @@ -77,8 +109,4 @@ public class HolePlayContext { void setStrokes(int s) { this.strokes = s; } - - void setDistanceRemainingYds(int d) { - this.distanceRemainingYds = d; - } } \ No newline at end of file -- cgit v1.2.3