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 { Golfer golfer = Session.getCurrentGolfer(); @Override public String getTitle() { return "statistics menu"; } @Override public List getMenuOptions() { return List.of( new MenuOption("return to main menu", () -> new MainMenu().present()), 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(); }), 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(); }), 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(); }), 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(); }), 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(); }) ); } // 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()); Statistics yearStats = new YearlyStats(baseStats, year); return yearStats; } 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; } }