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
|
package design.controller.userinput.menus;
import design.controller.userinput.Menu;
import design.controller.userinput.MenuOption;
import design.model.Course;
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 java.time.LocalDateTime;
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;
public HolePlayMenu() {
this.round = createRound();
this.ctx = new HolePlayContext(Session.getCurrentGolfer(), round, PersonalDatabase.INSTANCE);
}
@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<>();
boolean inSetup = (ctx.getCurrentPlay() == null);
if (inSetup) {
// 0) Start hole
opts.add(new MenuOption("start hole", () -> {
ctx.startHole();
System.out.println("Started hole " + round.getCurrentHole().getNumber() + ".");
this.present();
}));
} else {
// 0) 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);
}
// Records shot
ctx.recordShot(club, dist);
System.out.println("Shot recorded: " + dist + " yds with " + club.getNickname());
this.present();
}));
// 1) Hole out
opts.add(new MenuOption("hole out", () -> {
// Precedes to next hole
int prev = round.getCurrentHole().getNumber();
ctx.holeOut();
System.out.println("Holed out on " + prev + ". Next: " + round.getCurrentHole().getNumber());
this.present();
}));
}
// End round (always shown)
opts.add(new MenuOption("end round", () -> {
ctx.endRoundNow();
System.out.println("Round ended.");
new MainMenu().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, LocalDateTime.now(), startHole);
golfer.addRound(r);
return r;
}
}
|