summaryrefslogtreecommitdiff
path: root/src/main/java/design/model
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/design/model')
-rw-r--r--src/main/java/design/model/Golfer.java10
-rw-r--r--src/main/java/design/model/Hole.java3
-rw-r--r--src/main/java/design/model/Play.java25
-rw-r--r--src/main/java/design/model/Round.java17
-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
9 files changed, 266 insertions, 8 deletions
diff --git a/src/main/java/design/model/Golfer.java b/src/main/java/design/model/Golfer.java
index 04ad8bc..19d6ac6 100644
--- a/src/main/java/design/model/Golfer.java
+++ b/src/main/java/design/model/Golfer.java
@@ -11,6 +11,7 @@ public class Golfer {
private String fullName;
private final List<Course> courses;
private final List<Round> rounds;
+ private final List<Club> clubs = new ArrayList<>(); // Keep track of golfer's clubs
@JsonCreator
private Golfer(String username, int passwordHash, String fullName, List<Course> courses, List<Round> rounds) {
@@ -72,4 +73,13 @@ public class Golfer {
public void addRound(Round round) {
rounds.add(round);
}
+
+ // Helpers dealing with clubs
+ public void addClub(Club c) {
+ clubs.add(c);
+ }
+
+ public boolean hasClub(Club c) {
+ return clubs.contains(c);
+ }
}
diff --git a/src/main/java/design/model/Hole.java b/src/main/java/design/model/Hole.java
index bc9fdb9..23dc783 100644
--- a/src/main/java/design/model/Hole.java
+++ b/src/main/java/design/model/Hole.java
@@ -3,6 +3,7 @@ package design.model;
public class Hole {
private final int number;
private final int par;
+ // TODO: Maybe add a yardage interger var
public Hole(int number, int par) {
this.number = number;
@@ -16,4 +17,6 @@ public class Hole {
public int getPar() {
return par;
}
+
+ // TODO: Maybe add a get yardage method
}
diff --git a/src/main/java/design/model/Play.java b/src/main/java/design/model/Play.java
index ca6dac2..2e083b9 100644
--- a/src/main/java/design/model/Play.java
+++ b/src/main/java/design/model/Play.java
@@ -6,22 +6,22 @@ import java.util.ArrayList;
import java.util.List;
public class Play {
- private final int distance;
+ private int holeNumber;
private final List<Swing> swings;
@JsonCreator
- private Play(int distance, List<Swing> swings) {
- this.distance = distance;
- this.swings = swings;
+ public Play(int holeNumber, List<Swing> swings) {
+ this.holeNumber = holeNumber;
+ this.swings = (swings != null) ? swings : new ArrayList<>();
}
- public Play(int distance) {
+ public Play(int holeNumber) {
+ this.holeNumber = holeNumber;
this.swings = new ArrayList<>();
- this.distance = distance;
}
- public int getDistance() {
- return distance;
+ public int getHoleNumber() {
+ return holeNumber;
}
public void addSwing(Swing swing) {
@@ -35,4 +35,13 @@ public class Play {
public int getSwingCount() {
return swings.size();
}
+
+ // Returns the sum of all swing distances
+ public int getDistance() {
+ int total = 0;
+ for (Swing s : swings) {
+ total += s.getDistance();
+ }
+ return total;
+ }
}
diff --git a/src/main/java/design/model/Round.java b/src/main/java/design/model/Round.java
index 048a21a..bc4914e 100644
--- a/src/main/java/design/model/Round.java
+++ b/src/main/java/design/model/Round.java
@@ -11,6 +11,7 @@ public class Round {
private final LocalDateTime dateTime;
private final Hole startingHole;
private final List<Play> plays;
+ private int currentHoleIndex;
@JsonCreator
private Round(Course course, LocalDateTime dateTime, Hole startingHole, List<Play> plays) {
@@ -18,6 +19,9 @@ public class Round {
this.dateTime = dateTime;
this.startingHole = startingHole;
this.plays = plays;
+ // Allows the golfer to start anywhere on the course. Helps HolePalyContext be
+ // simpler.
+ this.currentHoleIndex = Math.max(0, startingHole.getNumber() - 1);
}
public Round(Course course, LocalDateTime dateTime, Hole startingHole) {
@@ -25,6 +29,9 @@ public class Round {
this.dateTime = dateTime;
this.startingHole = startingHole;
plays = new ArrayList<>();
+ // Allows the golfer to start anywhere on the course. Helps HolePalyContext be
+ // simpler.
+ this.currentHoleIndex = Math.max(0, startingHole.getNumber() - 1);
}
public int getTotalSwings() {
@@ -52,4 +59,14 @@ public class Round {
public void addPlay(Play play) {
plays.add(play);
}
+
+ // Current hole
+ public Hole getCurrentHole() {
+ return course.getHoles().get(currentHoleIndex);
+ }
+
+ // Handles wraparound too
+ public void nextHole() {
+ currentHoleIndex = (currentHoleIndex + 1) % course.getHoleCount();
+ }
}
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";
+ }
+}