summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-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/HolePlayContext.java56
-rw-r--r--src/main/java/design/model/holeplay/SetupState.java9
6 files changed, 94 insertions, 26 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/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
diff --git a/src/main/java/design/model/holeplay/SetupState.java b/src/main/java/design/model/holeplay/SetupState.java
index 2715e7c..d49ea0e 100644
--- a/src/main/java/design/model/holeplay/SetupState.java
+++ b/src/main/java/design/model/holeplay/SetupState.java
@@ -3,15 +3,16 @@ package design.model.holeplay;
import design.model.Hole;
public class SetupState implements HoleState {
+
@Override
public void enter(HolePlayContext ctx) {
- /* no-op */ }
+ ctx.clearInProgressPlay(); // Maybe we don't need this?
+ }
@Override
public void handleStart(HolePlayContext ctx) {
- // Pull current hole number & par from Round/Course
- Hole start = ctx.getRound().getStartingHole(); // or compute from current-hole tracking if you add it
- ctx.beginNewPlay(start.getNumber(), start.getPar());
+ Hole hole = ctx.getRound().getCurrentHole();
+ ctx.beginNewPlay(hole.getNumber());
ctx.setState(new PlayState());
}