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

import design.controller.userinput.Menu;
import design.controller.userinput.MenuOption;
import design.model.Golfer;
import design.model.League;
import design.model.ScrambleLeague;
import design.model.StrokeLeague;
import design.persistence.LeagueDatabase;
import design.runtime.Session;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class SelectLeague extends Menu {
    private final Golfer golfer = Session.getCurrentGolfer();

    @Override
    public String getTitle() {
        return "Select League";
    }

    @Override
    public List<MenuOption> getMenuOptions() {
        List<MenuOption> options = new ArrayList<>();
        for (League l : LeagueDatabase.instance().getLeagues()) {
            options.add(new MenuOption(l.getName(), () -> {
                if (l instanceof ScrambleLeague sl && sl.locateTeam(golfer) == null) {
                    Scanner sc = new Scanner(System.in);
                    System.out.print("You are not a member of a team for this league. Would you like to create one (Y/n): ");
                    if (sc.next().equals("n")) {
                        new MainMenu().present();
                    } else {
                        // create team
                    }
                } else {
                    new LeageMenu(l).present();
                }
            }));
        }
        options.add(new MenuOption("<create>", this::createLeagueWizard));
        return options;
    }

    private void createLeagueWizard() {
        try {
            Scanner scanner = new Scanner(System.in);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

            System.out.print("Enter league name: ");
            String name = scanner.nextLine();

            System.out.print("Enter registration date (yyyy-MM-dd): ");
            Date registrationDate = dateFormat.parse(scanner.nextLine());

            System.out.print("Enter start date (yyyy-MM-dd): ");
            Date startDate = dateFormat.parse(scanner.nextLine());

            System.out.print("Enter end date (yyyy-MM-dd): ");
            Date endDate = dateFormat.parse(scanner.nextLine());

            Golfer owner = Session.getCurrentGolfer();

            System.out.print("Enter format (stroke/scramble): ");
            String scramble = scanner.nextLine();

            League league;
            switch (scramble) {
                case "scramble" -> league = new ScrambleLeague(name, registrationDate, startDate, endDate, owner);
                case "stroke" -> league = new StrokeLeague(name, registrationDate, startDate, endDate, owner);
                default -> throw new RuntimeException();
            }
            LeagueDatabase.instance().addLeague(league);
        } catch (ParseException ex) {
            System.out.println("Invalid format");
        } catch (IOException ex) {
            System.out.println("Error writing to file");
        }
        this.present();
    }
}