summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/HolePlayMenu.java
blob: e12bdfad720139a3b854b2ae57bece8df1cdbe24 (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
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", () -> {
                // Gets club
                Scanner sc = new Scanner(System.in);
                System.out.print("Club nickname: ");
                String nick = sc.nextLine().trim();
                // Defaulted to Driver for now
                // TODO update logic
                Club club = new Club("Generic", nick, Club.ClubType.DRIVER);

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