summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/AddClubMenu.java
blob: ad22eb3802a0b9745f0cc18c9c2a00200b8b8725 (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
package design.controller.userinput.menus;

import design.controller.userinput.Menu;
import design.controller.userinput.MenuOption;
import design.model.Club;
import design.model.Golfer;
import design.persistence.PersonalDatabase;
import design.runtime.Session;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class AddClubMenu extends Menu {

    @Override
    public String getTitle() {
        return "add a club";
    }

    @Override
    public List<MenuOption> getMenuOptions() {
        List<MenuOption> opts = new ArrayList<>();

        opts.add(new MenuOption("create new club", () -> {
            Golfer g = Session.getCurrentGolfer();
            if (g == null) {
                System.out.println("No user loaded.");
                new UserSettings().present();
                return;
            }

            Scanner sc = new Scanner(System.in);

            System.out.print("Manufacturer: ");
            String manufacture = sc.nextLine().trim();

            System.out.print("Nickname: ");
            String nickname = sc.nextLine().trim();

            // Pick type
            Club.ClubType[] types = Club.ClubType.values();
            System.out.println("Club type:");
            for (int i = 0; i < types.length; i++) {
                System.out.printf("%d: %s%n", i + 1, types[i]);
            }

            Club.ClubType type = null;
            while (type == null) {
                System.out.print("Select (1.." + types.length + "): ");
                String line = sc.nextLine().trim();
                int idx = Integer.parseInt(line);
                if (idx < 1 || idx > types.length) {
                    System.out.println("Out of range. Try again.");
                    continue;
                }
                type = types[idx - 1];
            }

            g.addClub(manufacture, nickname, type);

            new UserSettings().present();
        }));

        opts.add(new MenuOption("cancel", () -> new UserSettings().present()));
        return opts;
    }
}