diff options
Diffstat (limited to 'src/main/java/design/controller/userinput/menus/HolePlayMenu.java')
| -rw-r--r-- | src/main/java/design/controller/userinput/menus/HolePlayMenu.java | 74 |
1 files changed, 42 insertions, 32 deletions
diff --git a/src/main/java/design/controller/userinput/menus/HolePlayMenu.java b/src/main/java/design/controller/userinput/menus/HolePlayMenu.java index 14e5345..c824341 100644 --- a/src/main/java/design/controller/userinput/menus/HolePlayMenu.java +++ b/src/main/java/design/controller/userinput/menus/HolePlayMenu.java @@ -2,6 +2,8 @@ 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; @@ -17,8 +19,8 @@ public class HolePlayMenu extends Menu { private final Round round; private final HolePlayContext ctx; - public HolePlayMenu(Round round) { - this.round = round; + public HolePlayMenu() { + this.round = createRound(); this.ctx = new HolePlayContext(Session.getCurrentGolfer(), round, PersonalDatabase.INSTANCE); } @@ -44,38 +46,11 @@ public class HolePlayMenu extends Menu { } else { // 0) Take a shot opts.add(new MenuOption("take a shot", () -> { - // loads golfers clubs - var golfer = Session.getCurrentGolfer(); - Club[] clubs = (golfer == null) ? new Club[0] : golfer.getClubs(); - - if (clubs.length == 0) { - System.out.println("You don't have any clubs yet. Add one first."); - new AddClubMenu().present(); - this.present(); - return; - } - - // list clubs - System.out.println("-- YOUR CLUBS --"); - for (int i = 0; i < clubs.length; i++) { - Club c = clubs[i]; - System.out.printf("%d: #%d %s - %s (%s)%n", - i + 1, c.getId(), c.getNickname(), c.getManufacture(), c.getClubType()); - } + var selector = new SelectClub(); + selector.present(); + Club club = selector.getResult(); - // user selects one of their clubs Scanner sc = new Scanner(System.in); - Club club = null; - while (club == null) { - System.out.print("Select club # (1.." + clubs.length + "): "); - String line = sc.nextLine().trim(); - int idx = Integer.parseInt(line); - if (idx < 1 || idx > clubs.length) { - System.out.println("Out of range. Try again."); - continue; - } - club = clubs[idx - 1]; - } // Get shot distance (defaults to 0 of not stated) System.out.print("Distance (yds, blank=0): "); @@ -111,4 +86,39 @@ public class HolePlayMenu extends Menu { 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, Session.getDateTime(), startHole); + golfer.addRound(r); + return r; + } } |
