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
|
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.model.Round;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import design.persistence.MasterDatabase;
import design.runtime.Session;
public class CourseSelectMenu extends Menu {
@Override
public String getTitle() {
return "course select";
}
@Override
public List<MenuOption> getMenuOptions() {
List<MenuOption> opts = new ArrayList<>();
// 0) back
opts.add(new MenuOption("back to main menu", () -> new MainMenu().present()));
// 1) go to CourseSearchMenu to search for a course and add it profile
opts.add(new MenuOption("Add Course to Profile", () -> new MainMenu().present()));
// 2) pick course from MY PROFILE only
opts.add(new MenuOption("pick course from my profile", () -> {
var golfer = Session.getCurrentGolfer();
Course[] mine = (golfer == null) ? new Course[0] : golfer.getCourses();
// Checks user has courses of not informs them to add one to there profile
if (mine == null || mine.length == 0) {
System.out.println("You don't have any courses saved to your profile yet.");
System.out.println("Use the 'Add Course to Profile' menu first.");
this.present();
return;
}
// Select a course
Scanner sc = new Scanner(System.in);
// Prints all courses
System.out.println("-- MY COURSES --");
for (int i = 0; i < mine.length; i++) {
Course c = mine[i];
System.out.printf("%d: %s (%s) | Holes: %d | Total Par: %d | Difficulty: %.1f%n",
i + 1, c.getName(), c.getLocation(), c.getHoleCount(), c.getTotalPar(),
c.getDifficultyRating());
}
// Select course by number
Course selected = null;
while (selected == null) {
System.out.print("Select course # (1.." + mine.length + "): ");
String line = sc.nextLine().trim();
int idx = Integer.parseInt(line);
if (idx < 1 || idx > mine.length) {
System.out.println("Out of range. Try again.");
continue;
}
selected = mine[idx - 1];
}
// Gets starting hole on course
int startHoleNum = readStartingHole(sc, selected.getHoleCount());
// Starts round and sends user to HolePlayMenu
startRound(selected, startHoleNum);
}));
return opts;
}
private int readStartingHole(Scanner sc, int holeCount) {
// Asks for a hole number until a valid number is selected
while (true) {
System.out.print("Starting hole (1.." + holeCount + ", blank=1): ");
String s = sc.nextLine().trim();
if (s.isEmpty())
return 1;
int start = Integer.parseInt(s);
if (start < 1 || start > holeCount) {
System.out.println("Starting hole must be between 1 and " + holeCount + ".");
continue;
}
return start;
}
}
private void startRound(Course c, int startHoleNum) {
Hole startHole = c.getHoles().get(startHoleNum - 1);
Round r = new Round(c, LocalDateTime.now(), startHole);
Session.getCurrentGolfer().addRound(r);
new HolePlayMenu(r).present();
}
}
|