1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
package design.controller.userinput.menus;
import design.controller.userinput.Menu;
import design.controller.userinput.MenuOption;
import design.model.Course;
import design.model.Golfer;
import design.model.Hole;
import design.runtime.Session;
import design.model.Club;
import design.model.Round;
import design.model.holeplay.HolePlayContext;
import design.persistence.PersonalDatabase;
import design.model.undo.UndoManager;
import design.controller.userinput.UndoActions;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class HolePlayMenu extends Menu {
private final Round round;
private final HolePlayContext ctx;
private final Golfer golfer = Session.getCurrentGolfer();
public HolePlayMenu() {
this.round = createRound();
this.ctx = new HolePlayContext(this.golfer, round, PersonalDatabase.instance());
}
public Round getRound() {
return round;
}
@Override
public String getTitle() {
return String.format("hole play - %s | hole %d", round.getCourse().getName(),
round.getCurrentHole().getNumber());
}
@Override
public List<MenuOption> getMenuOptions() {
List<MenuOption> opts = new ArrayList<>();
// End round (always shown) 1
opts.add(new MenuOption("end round", () -> {
UndoManager.instance().capture(golfer, "End round on " + round.getCourse().getName());
ctx.endRoundNow();
System.out.println("Round ended.");
new MainMenu().present();
}));
// 2
opts.add(new MenuOption("undo", () -> {
UndoActions.undoWithSave();
this.present();
}));
// 3
opts.add(new MenuOption("redo", () -> {
UndoActions.redoWithSave();
this.present();
}));
boolean inSetup = (ctx.getCurrentPlay() == null);
if (inSetup) {
// 4) Start hole
opts.add(new MenuOption("start hole", () -> {
ctx.startHole();
System.out.println("Started hole " + round.getCurrentHole().getNumber() + ".");
this.present();
}));
} else {
// 4) Take a shot
opts.add(new MenuOption("take a shot", () -> {
var selector = new SelectClub();
selector.present();
Club club = selector.getResult();
Scanner sc = new Scanner(System.in);
// Get shot distance (defaults to 0 of not stated)
System.out.print("Distance (yds, blank=0): ");
String ds = sc.nextLine().trim();
int dist = 0;
if (!ds.isEmpty()) {
dist = Integer.parseInt(ds);
}
UndoManager.instance().capture(ctx, "Shot with " + club.getNickname() + " for " + dist + " yds");
// Records shot
ctx.recordShot(club, dist);
System.out.println("Shot recorded: " + dist + " yds with " + club.getNickname());
this.present();
}));
// 5) Hole out
opts.add(new MenuOption("hole out", () -> {
// Precedes to next hole
int prev = round.getCurrentHole().getNumber();
UndoManager.instance().capture(golfer, "Hole out on " + prev + " at " + round.getCourse().getName());
ctx.holeOut();
System.out.println("Holed out on " + prev + ". Next: " + round.getCurrentHole().getNumber());
this.present();
}));
}
return opts;
}
private static Round createRound() {
var golfer = Session.getCurrentGolfer();
var selector = new SelectCourse();
selector.present();
Course course = selector.getResult();
Scanner sc = new Scanner(System.in);
// Gets starting hole on course
int startHoleNum = 1;
int holeCount = course.getHoleCount();
// Asks for a hole number until a valid number is selected
System.out.print("Starting hole (1.." + holeCount + ", blank=1): ");
String s = sc.nextLine().trim();
if (!s.isEmpty()) {
int start;
try {
start = Integer.parseInt(s);
} catch (NumberFormatException ex) {
System.out.println("Input must be a number");
return createRound();
}
if (start < 1 || start > holeCount) {
System.out.println("Starting hole must be between 1 and " + holeCount + ".");
return createRound();
}
startHoleNum = start;
}
// Starts round and sends user to HolePlayMenu
Hole startHole = course.getHoles().get(startHoleNum - 1);
Round r = new Round(course, Session.getDateTime(), startHole);
// Undo puts golfer back to before round existed.
UndoManager.instance().capture(golfer, "Start round on " + course.getName() + " (hole " + startHoleNum + ")");
golfer.addRound(r);
return r;
}
}
|