summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/HolePlayMenu.java
blob: 14e53453ee672b637eeee64faffcecd7178911a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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<MenuOption> getMenuOptions() {
        List<MenuOption> 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.printf("%d: #%d %s - %s (%s)%n",
                            i + 1, c.getId(), c.getNickname(), c.getManufacture(), c.getClubType());
                }

                // 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;
    }
}