summaryrefslogtreecommitdiff
path: root/src/main/java/design/model/holeplay
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/design/model/holeplay')
-rw-r--r--src/main/java/design/model/holeplay/HolePlayContext.java56
-rw-r--r--src/main/java/design/model/holeplay/SetupState.java9
2 files changed, 47 insertions, 18 deletions
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());
}