summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/design/controller/userinput/menus/CourseSelectMenu.java')
-rw-r--r--src/main/java/design/controller/userinput/menus/CourseSelectMenu.java78
1 files changed, 63 insertions, 15 deletions
diff --git a/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java b/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java
index 881c393..af825e0 100644
--- a/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java
+++ b/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java
@@ -26,27 +26,75 @@ public class CourseSelectMenu extends Menu {
List<MenuOption> opts = new ArrayList<>();
// 0) back
- opts.add(new MenuOption("back to main menu", a -> new MainMenu().present()));
+ opts.add(new MenuOption("back to main menu", () -> new MainMenu().present()));
- // 1) pick course by number TODO: add error checking
- opts.add(new MenuOption("pick course by number (1..1000)", a -> {
- Scanner sc = new Scanner(System.in);
-
- System.out.print("Course number (1..1000): ");
- int n = Integer.parseInt(sc.nextLine().trim());
- Course c = MasterDatabase.INSTANCE.getCourse(n - 1);
+ // 1) go to CourseSearchMenu to search for a course and add it profile
+ opts.add(new MenuOption("Add Course to Profile", () -> new MainMenu().present()));
- System.out.print("Starting hole (1..18, blank=1): ");
- String s = sc.nextLine().trim();
- int start = s.isEmpty() ? 1 : Integer.parseInt(s);
+ // 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();
- Hole startHole = c.getHoles().get(start - 1);
+ // 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);
- Round r = new Round(c, LocalDateTime.now(), startHole);
- Session.getCurrentGolfer().addRound(r);
- new HolePlayMenu(r).present();
+ // 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();
+ }
}