summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/ManageClubs.java
blob: 56d645f7e629ea33f289611bcb960a5126672015 (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
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.List;
import java.util.Scanner;

public class ManageClubs extends Menu {
    Golfer golfer = Session.getCurrentGolfer();

    @Override
    public String getTitle() {
        return "manage clubs";
    }

    @Override
    public List<MenuOption> getMenuOptions() {
        List<MenuOption> opts = new java.util.ArrayList<>();
        opts.add(new MenuOption("return to main menu", () -> new MainMenu().present()));

        opts.add(new MenuOption("list clubs", () -> {
            for (Club club : golfer.getClubs()) {
                System.out.printf("- %s\n", club);
            }
            this.present();
        }));

        opts.add(new MenuOption("remove club...", () -> {
            new Menu() {
                @Override
                public String getTitle() {
                    return "remove club";
                }

                @Override
                public List<MenuOption> getMenuOptions() {
                    List<MenuOption> list = new ArrayList<>();
                    for (Club c : golfer.getClubs()) {
                        MenuOption menuOption = new MenuOption(c.toString(), () -> {
                            golfer.removeClub(c);
                            this.present();
                        });
                        list.add(menuOption);
                    }
                    return list;
                }
            }.present();
        }));

        opts.add(new MenuOption("add club...", () -> {
            if (golfer == 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];
            }

            golfer.addClub(manufacture, nickname, type);

            // Add club to JSON
            try {
                PersonalDatabase.INSTANCE.updateGolfer(golfer);
                System.out.println("Club added and saved.");
            } catch (IOException e) {
                throw new RuntimeException("Failed to save club", e);
            }
            new UserSettings().present();
        }));// Pick type
// Add club to JSON
        return opts;
    }
}