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 getMenuOptions() { List 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(); } }