/** * HockeyStats.java * *
The main driver class for the Hockey Stats Manager. Handles user input, file I/O operations, * and menu navigation. * * @author Cody Trainer */ import java.io.*; import java.util.*; public class HockeyStats { static PlayerList players = new PlayerList(); static BufferedReader br; static StringTokenizer st; static Scanner s = new Scanner(System.in); /** * Runs the primary menu loop. * * @param args Command line arguments (not used). * @throws Exception If file reading or input errors occur. */ public static void main(String[] args) throws Exception { while (true) { if (showMenu()) break; } s.close(); } /** * Loads player data from a specified text file. * * @param fileName The name of the file to load from. * @throws Exception If the file is not found or is improperly formatted. */ public static void loadData(String fileName) throws Exception { players.clear(); File f = new File(fileName); br = new BufferedReader(new FileReader(f)); st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); for (int i = 0; i < n; i++) { st = new StringTokenizer(br.readLine()); players.addPlayer( new HockeyPlayer( st.nextToken() + " " + st.nextToken(), // first + last name Integer.parseInt(st.nextToken()), // goals Integer.parseInt(st.nextToken()), // assists Integer.parseInt(st.nextToken()), // gamesWon Integer.parseInt(st.nextToken()))); // gamesLost } br.close(); } /** * Saves current player list data to a specified text file. * * @param fileName The name of the file to save to. * @throws Exception If an I/O error occurs. */ public static void saveData(String fileName) throws Exception { FileWriter fw = new FileWriter(fileName); String save = players.createSaveData(); fw.write(save); fw.close(); System.out.println("Data saved to " + fileName); } /** * Displays the main menu and routes user to various sub-menus or actions. * * @return true if the user chooses to quit, false otherwise. * @throws Exception If input errors occur. */ public static boolean showMenu() throws Exception { System.out.print( """ <----------------------------> Hockey Stats Manager Menu Select an option: (1) Load Data from Files (2) Save Data (3) Open Data Editor (4) Open Stats View Menu (5) Quit <----------------------------> """); int n; try { n = s.nextInt(); s.nextLine(); } catch (Exception e) { System.out.println("Please choose one of the available menu options!"); return false; } boolean close = false; switch (n) { case 1: loadData("data.txt"); break; case 2: saveData("save.txt"); break; case 3: while (true) { if (showDataMenu()) break; } break; case 4: while (true) { if (showStatsMenu()) break; } break; case 5: System.out.println("Thank you!"); close = true; break; default: System.out.println("Please choose one of the available menu options!"); break; } return close; } /** * Displays the editor menu for adding, removing, or updating player data. * * @return true if the user chooses to return to the main menu, false otherwise. * @throws Exception If input errors occur. */ public static boolean showDataMenu() throws Exception { System.out.print( """ <----------------------------> Hockey Data Editor Menu Select an option: (1) Add Player (2) Remove Player (3) Add Game to Player (4) Return to main menu <----------------------------> """); boolean dataExit = false; int k; try { k = s.nextInt(); s.nextLine(); } catch (Exception e) { System.out.println("Please choose one of the available menu options!"); return false; } try { switch (k) { case 1: System.out.println("Enter The Following Stats:"); System.out.println("\nPlayer Name:"); String newName = s.nextLine(); System.out.println("Player Goals Scored:"); int newGoals = s.nextInt(); System.out.println("Player Assists:"); int newAssists = s.nextInt(); System.out.println("Games Won:"); int newGamesWon = s.nextInt(); System.out.println("Games Lost:"); int newGamesLost = s.nextInt(); s.nextLine(); players.addPlayer( new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost)); break; case 2: System.out.println("Enter the name of the player you want to remove:"); String rmName = s.nextLine(); System.out.println( players.removePlayer(rmName) ? rmName + " was successfully removed." : "Player not found."); break; case 3: System.out.println("Enter The Following Stats:"); System.out.println("\nPlayer Name:"); String addName = s.nextLine(); System.out.println("Player Goals Scored:"); int addGoals = s.nextInt(); System.out.println("Player Assists:"); int addAssists = s.nextInt(); s.nextLine(); System.out.println("Games Won? (yes/no)"); boolean addGameWon = s.nextLine().equalsIgnoreCase("yes"); HockeyPlayer found = players.findPlayer(addName); if (found == null) { System.out.println("Player not found."); } else { found.addGame(addGoals, addAssists, addGameWon); } break; case 4: dataExit = true; break; default: System.out.println("Please choose one of the available menu options!"); break; } } catch (Exception e) { System.out.println("Please enter the right kind of data"); } return dataExit; } /** * Displays the viewer menu for printing, finding, and sorting player statistics. * * @return true if the user chooses to return to the main menu, false otherwise. */ public static boolean showStatsMenu() { System.out.print( """ <----------------------------> Hockey Stats Viewer Select an option: (1) Print Data (2) Find Player (3) Sort By Name (4) Sort By Points (5) Show by Goals > num (6) Return to Main Menu <----------------------------> """); boolean statsExit = false; int k; try { k = s.nextInt(); s.nextLine(); } catch (Exception e) { System.out.println("Please choose one of the available menu options!"); return false; } try { switch (k) { case 1: System.out.println(players.toString()); break; case 2: System.out.println("Enter the name of the player you want to find:"); String findName = s.nextLine(); HockeyPlayer p = players.findPlayer(findName); System.out.println(p == null ? "Player not found." : p.toString()); break; case 3: players.sortByNames(); System.out.println("Sorted by names"); break; case 4: players.sortByPoints(); System.out.println("Sorted by points"); break; case 5: System.out.println("Enter the min goals to show:"); int minGoals = s.nextInt(); s.nextLine(); PlayerList pl = players.getPlayersGoalsAbove(minGoals); System.out.println(pl.toString()); break; case 6: statsExit = true; break; default: System.out.println("Please choose one of the available menu options!"); break; } } catch (Exception e) { System.out.println("k = " + k); System.out.println(e); System.out.println("Please enter the right kind of data"); } return statsExit; } }