diff options
Diffstat (limited to '')
| -rw-r--r-- | src/main/java/design/controller/userinput/menus/CourseSelectMenu.java | 76 |
1 files changed, 62 insertions, 14 deletions
diff --git a/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java b/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java index 8df9823..af825e0 100644 --- a/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java +++ b/src/main/java/design/controller/userinput/menus/CourseSelectMenu.java @@ -28,25 +28,73 @@ public class CourseSelectMenu extends Menu { // 0) back 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)", () -> { - 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(); + } } |
