summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/holeplay
diff options
context:
space:
mode:
authorMichael Lizzio <142752852+Michael-Lizzio@users.noreply.github.com>2025-10-05 05:30:55 -0400
committerGitHub <noreply@github.com>2025-10-05 05:30:55 -0400
commitc5cbe4ffd56b7c96d85dc1676387993c6c15af7b (patch)
tree2ba839f3fc8fea03612e7f7051176b8d0a059a48 /src/main/java/design/model/holeplay
parent42fcc633ddc0e328561f984a6aa0b8593765dcf2 (diff)
parent40470b1788c28f1c95bba7339dd9819b0e9b95cc (diff)
downloaddesignproject-design-6-c5cbe4ffd56b7c96d85dc1676387993c6c15af7b.tar.gz
designproject-design-6-c5cbe4ffd56b7c96d85dc1676387993c6c15af7b.tar.bz2
designproject-design-6-c5cbe4ffd56b7c96d85dc1676387993c6c15af7b.zip
Merge pull request #2 from RIT-SWEN-262/lizzio-holePlaySubsystem
Lizzio hole play subsystem
Diffstat (limited to 'src/main/java/design/model/holeplay')
-rw-r--r--src/main/java/design/model/holeplay/HoleCompleteState.java28
-rw-r--r--src/main/java/design/model/holeplay/HolePlayContext.java112
-rw-r--r--src/main/java/design/model/holeplay/HoleState.java15
-rw-r--r--src/main/java/design/model/holeplay/PlayState.java31
-rw-r--r--src/main/java/design/model/holeplay/SetupState.java33
5 files changed, 219 insertions, 0 deletions
diff --git a/src/main/java/design/model/holeplay/HoleCompleteState.java b/src/main/java/design/model/holeplay/HoleCompleteState.java
new file mode 100644
index 0000000..958fe5f
--- /dev/null
+++ b/src/main/java/design/model/holeplay/HoleCompleteState.java
@@ -0,0 +1,28 @@
+package design.model.holeplay;
+
+public class HoleCompleteState implements HoleState {
+ @Override
+ public void enter(HolePlayContext ctx) {
+ /* compute summaries if needed */ }
+
+ @Override
+ public void handleStart(HolePlayContext ctx) {
+ // optional: treat this as “Next Hole”
+ ctx.setState(new SetupState());
+ }
+
+ @Override
+ public void handleShot(HolePlayContext ctx, design.model.Club club, Integer distanceYds) {
+ throw new IllegalStateException("Hole is complete; start next hole.");
+ }
+
+ @Override
+ public void handleHoleOut(HolePlayContext ctx) {
+ throw new IllegalStateException("Already holed out.");
+ }
+
+ @Override
+ public String name() {
+ return "HoleCompleteState";
+ }
+}
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
diff --git a/src/main/java/design/model/holeplay/HoleState.java b/src/main/java/design/model/holeplay/HoleState.java
new file mode 100644
index 0000000..ec0618f
--- /dev/null
+++ b/src/main/java/design/model/holeplay/HoleState.java
@@ -0,0 +1,15 @@
+package design.model.holeplay;
+
+import design.model.Club;
+
+public interface HoleState {
+ void enter(HolePlayContext ctx);
+
+ void handleStart(HolePlayContext ctx);
+
+ void handleShot(HolePlayContext ctx, Club club, Integer distanceYds); // distance may be null
+
+ void handleHoleOut(HolePlayContext ctx);
+
+ String name();
+} \ No newline at end of file
diff --git a/src/main/java/design/model/holeplay/PlayState.java b/src/main/java/design/model/holeplay/PlayState.java
new file mode 100644
index 0000000..e41af6b
--- /dev/null
+++ b/src/main/java/design/model/holeplay/PlayState.java
@@ -0,0 +1,31 @@
+package design.model.holeplay;
+
+import design.model.Club;
+
+public class PlayState implements HoleState {
+ @Override
+ public void enter(HolePlayContext ctx) {
+ /* no-op */ }
+
+ @Override
+ public void handleStart(HolePlayContext ctx) {
+ throw new IllegalStateException("Hole already started.");
+ }
+
+ @Override
+ public void handleShot(HolePlayContext ctx, Club club, Integer distanceYds) {
+ ctx.addSwing(club, distanceYds);
+ // remain in PlayState
+ }
+
+ @Override
+ public void handleHoleOut(HolePlayContext ctx) {
+ ctx.finalizePlayAndPersist();
+ ctx.setState(new HoleCompleteState());
+ }
+
+ @Override
+ public String name() {
+ return "PlayState";
+ }
+}
diff --git a/src/main/java/design/model/holeplay/SetupState.java b/src/main/java/design/model/holeplay/SetupState.java
new file mode 100644
index 0000000..d49ea0e
--- /dev/null
+++ b/src/main/java/design/model/holeplay/SetupState.java
@@ -0,0 +1,33 @@
+package design.model.holeplay;
+
+import design.model.Hole;
+
+public class SetupState implements HoleState {
+
+ @Override
+ public void enter(HolePlayContext ctx) {
+ ctx.clearInProgressPlay(); // Maybe we don't need this?
+ }
+
+ @Override
+ public void handleStart(HolePlayContext ctx) {
+ Hole hole = ctx.getRound().getCurrentHole();
+ ctx.beginNewPlay(hole.getNumber());
+ ctx.setState(new PlayState());
+ }
+
+ @Override
+ public void handleShot(HolePlayContext ctx, design.model.Club club, Integer distanceYds) {
+ throw new IllegalStateException("Cannot record a shot during setup.");
+ }
+
+ @Override
+ public void handleHoleOut(HolePlayContext ctx) {
+ throw new IllegalStateException("Cannot hole out during setup.");
+ }
+
+ @Override
+ public String name() {
+ return "SetupState";
+ }
+}