From 764b9cd2a0bb89db294d399980c126298726638f Mon Sep 17 00:00:00 2001 From: Cool Guy <4052244-CoolGuy27@users.noreply.replit.com> Date: Thu, 7 May 2026 20:31:25 +0000 Subject: [PATCH] fixed main menu JOptionPane --- HockeyStats.java | 95 +++++++++++++++++++++++++++--------------------- 1 file changed, 53 insertions(+), 42 deletions(-) diff --git a/HockeyStats.java b/HockeyStats.java index b6ca0ee..86cadf5 100644 --- a/HockeyStats.java +++ b/HockeyStats.java @@ -1,4 +1,3 @@ - /** * HockeyStats.java * @@ -25,8 +24,7 @@ public class HockeyStats { */ public static void main(String[] args) throws Exception { while (true) { - if (showMenu()) - break; + if (showMenu()) break; } s.close(); } @@ -78,7 +76,8 @@ public class HockeyStats { * @throws Exception If input errors occur. */ public static boolean showMenu() throws Exception { - String menu = """ + String menu = + """ <----------------------------> Hockey Stats Manager Menu @@ -94,8 +93,18 @@ public class HockeyStats { """; int n; try { - n = Integer.parseInt(JOptionPane.showInputDialog(menu)); + String input = JOptionPane.showInputDialog(menu); + if (input == null) { + throw new NumberFormatException("Cancel or Exit clicked"); + } + if (input == "") { + throw new IllegalArgumentException("No input"); + } + n = Integer.parseInt(input); } catch (Exception e) { + if (e instanceof NumberFormatException) { + return true; + } JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!"); return false; } @@ -109,14 +118,12 @@ public class HockeyStats { break; case 3: while (true) { - if (showDataMenu()) - break; + if (showDataMenu()) break; } break; case 4: while (true) { - if (showStatsMenu()) - break; + if (showStatsMenu()) break; } break; case 5: @@ -137,7 +144,8 @@ public class HockeyStats { * @throws Exception If input errors occur. */ public static boolean showDataMenu() throws Exception { - String menu = """ + String menu = + """ <----------------------------> Hockey Data Editor Menu @@ -167,21 +175,21 @@ public class HockeyStats { JTextField fieldNewGamesWon = new JTextField(); JTextField fieldNewGamesLost = new JTextField(); Object[] newMessage = { - "Player Name:", - fieldNewName, - "Player Goals Scored:", - fieldNewGoals, - "Player Assists:", - fieldNewAssists, - "Games Won:", - fieldNewGamesWon, - "Games Lost:", - fieldNewGamesLost + "Player Name:", + fieldNewName, + "Player Goals Scored:", + fieldNewGoals, + "Player Assists:", + fieldNewAssists, + "Games Won:", + fieldNewGamesWon, + "Games Lost:", + fieldNewGamesLost }; - int newOption = JOptionPane.showConfirmDialog( - null, newMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION); - if (newOption != JOptionPane.OK_OPTION) - return false; + int newOption = + JOptionPane.showConfirmDialog( + null, newMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION); + if (newOption != JOptionPane.OK_OPTION) return false; String newName = fieldNewName.getText(); int newGoals = Integer.parseInt(fieldNewGoals.getText()); @@ -192,7 +200,8 @@ public class HockeyStats { new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost)); break; case 2: - String rmName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:"); + String rmName = + JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:"); JOptionPane.showMessageDialog( null, players.removePlayer(rmName) @@ -205,19 +214,19 @@ public class HockeyStats { JTextField fieldAddAssists = new JTextField(); JTextField fieldAddGameWon = new JTextField(); Object[] addMessage = { - "Player Name:", - fieldAddName, - "Player Goals Scored:", - fieldAddGoals, - "Player Assists:", - fieldAddAssists, - "Game Won? (yes/no)", - fieldAddGameWon + "Player Name:", + fieldAddName, + "Player Goals Scored:", + fieldAddGoals, + "Player Assists:", + fieldAddAssists, + "Game Won? (yes/no)", + fieldAddGameWon }; - int addOption = JOptionPane.showConfirmDialog( - null, addMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION); - if (addOption != JOptionPane.OK_OPTION) - return false; + int addOption = + JOptionPane.showConfirmDialog( + null, addMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION); + if (addOption != JOptionPane.OK_OPTION) return false; String addName = fieldAddName.getText(); int addGoals = Integer.parseInt(fieldAddGoals.getText()); int addAssists = Integer.parseInt(fieldAddAssists.getText()); @@ -244,13 +253,13 @@ public class HockeyStats { } /** - * Displays the viewer menu for printing, finding, and sorting player - * statistics. + * 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() { - String menu = """ + String menu = + """ <----------------------------> Hockey Stats Viewer @@ -279,7 +288,8 @@ public class HockeyStats { JOptionPane.showMessageDialog(null, players.toString()); break; case 2: - String findName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to find:"); + String findName = + JOptionPane.showInputDialog(null, "Enter the name of the player you want to find:"); HockeyPlayer p = players.findPlayer(findName); JOptionPane.showMessageDialog(null, p == null ? "Player not found." : p.toString()); break; @@ -292,7 +302,8 @@ public class HockeyStats { JOptionPane.showMessageDialog(null, "Sorted by points"); break; case 5: - int minGoals = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the min goals to show:")); + int minGoals = + Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the min goals to show:")); PlayerList pl = players.getPlayersGoalsAbove(minGoals); JOptionPane.showMessageDialog(null, pl.toString()); break;