summaryrefslogtreecommitdiff
path: root/src/main/java/design/controller/userinput/menus/StatisticsMenu.java
blob: 9ce0bd0d4ad8d9104085a38aeac2207831ebd522 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package design.controller.userinput.menus;

import design.controller.userinput.Menu;
import design.controller.userinput.MenuOption;
import design.model.Course;
import design.model.Golfer;
import design.model.Hole;
import design.model.Play;
import design.model.Round;
import design.model.statistics.CourseStats;
import design.model.statistics.HoleStats;
import design.model.statistics.LifetimeStats;
import design.model.statistics.RoundStats;
import design.model.statistics.Statistics;
import design.model.statistics.YearlyStats;
import design.runtime.Session;

import java.util.List;
import java.util.Scanner;

public class StatisticsMenu extends Menu {
    private final Golfer golfer = Session.getCurrentGolfer();
    
    @Override
    public String getTitle() {
        return "statistics menu";
    }

    @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("view lifetime statistics", () -> {
            Statistics stats = new LifetimeStats(golfer);

            System.out.printf("Total swings: %d\n", stats.get_score());
            System.out.printf("Total distance: %.1f\n", stats.get_distance());
            this.present();
        }));

        opts.add(new MenuOption("view yearly statistics", () -> {
            Statistics baseStats = new LifetimeStats(golfer);
            Statistics yearStats = getUserYear(baseStats);

            System.out.printf("Total swings: %d\n", yearStats.get_score());
            System.out.printf("Total distance: %.1f\n", yearStats.get_distance());
            this.present();
        }));

        opts.add(new MenuOption("view course statistics", () -> {
            Statistics baseStats = new LifetimeStats(golfer);
            Statistics yearStats = getUserYear(baseStats);
            Statistics courseStats = getUserCourse(yearStats);

            System.out.printf("Total swings: %d\n", courseStats.get_score());
            System.out.printf("Total distance: %.1f\n", courseStats.get_distance());
            this.present();
        }));

        opts.add(new MenuOption("view round statistics", () -> {
            Statistics baseStats = new LifetimeStats(golfer);
            Statistics yearStats = getUserYear(baseStats);
            Statistics courseStats = getUserCourse(yearStats);
            Statistics roundStats = getUserRound(courseStats);

            System.out.printf("Total swings: %d\n", roundStats.get_score());
            System.out.printf("Total distance: %.1f\n", roundStats.get_distance());
            this.present();
        }));

        opts.add(new MenuOption("view hole statistics", () -> {
            Statistics baseStats = new LifetimeStats(golfer);
            Statistics yearStats = getUserYear(baseStats);
            Statistics courseStats = getUserCourse(yearStats);
            Statistics roundStats = getUserRound(courseStats);
            Statistics holeStats = getUserHole(roundStats);

            System.out.printf("Total swings: %d\n", holeStats.get_score());
            System.out.printf("Total distance: %.1f\n", holeStats.get_distance());
            this.present();
        }));

        return opts;
    }

    // Helper classes to get user input and handle finding the data they requested (Course, Hole, Year, etc.)
    private Statistics getUserYear(Statistics baseStats){
        Scanner sc = new Scanner(System.in);

        System.out.println("Enter year to search: ");
        int year = Integer.parseInt(sc.nextLine());

        return new YearlyStats(baseStats, year);
    }

    private Statistics getUserCourse(Statistics baseStats){
        Scanner sc = new Scanner(System.in);
        Course[] courses = golfer.getCourses();
        for(int i = 0; i < courses.length; i++){
            System.out.println((i+1) + ".) Name: " + courses[i].getName() + ". Location: " + courses[i].getLocation());
        }
        System.out.println("Select the course number corresponding to the course. ");
        int selection = Integer.parseInt(sc.nextLine());
        return new CourseStats(baseStats, courses[selection - 1]);
    }

    private Statistics getUserRound(Statistics baseStats){
        Scanner sc = new Scanner(System.in);
        Round[] rounds = baseStats.getRounds();
        for(int i = 0; i < rounds.length; i++){
            System.out.println((i+1) + ".) Date: " + rounds[i].getDateTime().toLocalDate() + ". Time: " + rounds[i].getDateTime().toLocalTime() + ". Final Score: " + rounds[i].getTotalSwings() + ". Total Distance: " + rounds[i].getTotalDistance());
        }
        System.out.println("Select the round number: ");
        int selection = Integer.parseInt(sc.nextLine());
        return new RoundStats(baseStats, rounds[selection - 1]);
    }

    private Statistics getUserHole(Statistics baseStats){
        Scanner sc = new Scanner(System.in);
        Round round = baseStats.getRounds()[0]; // Only 1 round
        Play[] plays = round.getPlays();
        for(Play play : plays){
            System.out.println("Hole number: " + play.getHoleNumber());
        }
        System.out.println("Enter the hole number: ");
        int holeNum = Integer.parseInt(sc.nextLine());

        Course course = round.getCourse();
        for(Hole hole : course.getHoles()){
            if(hole.getNumber() == holeNum){
                return new HoleStats(baseStats, hole);
            }
        }
        return baseStats;
    }
}