package design.controller.userinput.menus; import design.controller.userinput.Menu; import design.controller.userinput.MenuOption; import design.runtime.Session; import design.model.Club; import design.model.Round; import design.model.holeplay.HolePlayContext; import design.persistence.PersonalDatabase; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class HolePlayMenu extends Menu { private final Round round; private final HolePlayContext ctx; public HolePlayMenu(Round round) { this.round = round; this.ctx = new HolePlayContext(Session.getCurrentGolfer(), round, PersonalDatabase.INSTANCE); } @Override public String getTitle() { return String.format("hole play - %s | hole %d", round.getCourse().getName(), round.getCurrentHole().getNumber()); } @Override public List getMenuOptions() { List opts = new ArrayList<>(); boolean inSetup = (ctx.getCurrentPlay() == null); if (inSetup) { // 0) Start hole opts.add(new MenuOption("start hole", () -> { ctx.startHole(); System.out.println("Started hole " + round.getCurrentHole().getNumber() + "."); this.present(); })); } 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.println(c); } // 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): "); String ds = sc.nextLine().trim(); int dist = 0; if (!ds.isEmpty()) { dist = Integer.parseInt(ds); } // Records shot ctx.recordShot(club, dist); System.out.println("Shot recorded: " + dist + " yds with " + club.getNickname()); this.present(); })); // 1) Hole out opts.add(new MenuOption("hole out", () -> { // Precedes to next hole int prev = round.getCurrentHole().getNumber(); ctx.holeOut(); System.out.println("Holed out on " + prev + ". Next: " + round.getCurrentHole().getNumber()); this.present(); })); } // End round (always shown) opts.add(new MenuOption("end round", () -> { ctx.endRoundNow(); System.out.println("Round ended."); new MainMenu().present(); })); return opts; } }