fixed main menu JOptionPane

This commit is contained in:
Cool Guy
2026-05-07 20:31:25 +00:00
parent 9ecc766a51
commit 764b9cd2a0

View File

@@ -1,4 +1,3 @@
/** /**
* HockeyStats.java * HockeyStats.java
* *
@@ -25,8 +24,7 @@ public class HockeyStats {
*/ */
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
while (true) { while (true) {
if (showMenu()) if (showMenu()) break;
break;
} }
s.close(); s.close();
} }
@@ -78,7 +76,8 @@ public class HockeyStats {
* @throws Exception If input errors occur. * @throws Exception If input errors occur.
*/ */
public static boolean showMenu() throws Exception { public static boolean showMenu() throws Exception {
String menu = """ String menu =
"""
<----------------------------> <---------------------------->
Hockey Stats Manager Menu Hockey Stats Manager Menu
@@ -94,8 +93,18 @@ public class HockeyStats {
"""; """;
int n; int n;
try { 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) { } catch (Exception e) {
if (e instanceof NumberFormatException) {
return true;
}
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!"); JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
return false; return false;
} }
@@ -109,14 +118,12 @@ public class HockeyStats {
break; break;
case 3: case 3:
while (true) { while (true) {
if (showDataMenu()) if (showDataMenu()) break;
break;
} }
break; break;
case 4: case 4:
while (true) { while (true) {
if (showStatsMenu()) if (showStatsMenu()) break;
break;
} }
break; break;
case 5: case 5:
@@ -137,7 +144,8 @@ public class HockeyStats {
* @throws Exception If input errors occur. * @throws Exception If input errors occur.
*/ */
public static boolean showDataMenu() throws Exception { public static boolean showDataMenu() throws Exception {
String menu = """ String menu =
"""
<----------------------------> <---------------------------->
Hockey Data Editor Menu Hockey Data Editor Menu
@@ -167,21 +175,21 @@ public class HockeyStats {
JTextField fieldNewGamesWon = new JTextField(); JTextField fieldNewGamesWon = new JTextField();
JTextField fieldNewGamesLost = new JTextField(); JTextField fieldNewGamesLost = new JTextField();
Object[] newMessage = { Object[] newMessage = {
"Player Name:", "Player Name:",
fieldNewName, fieldNewName,
"Player Goals Scored:", "Player Goals Scored:",
fieldNewGoals, fieldNewGoals,
"Player Assists:", "Player Assists:",
fieldNewAssists, fieldNewAssists,
"Games Won:", "Games Won:",
fieldNewGamesWon, fieldNewGamesWon,
"Games Lost:", "Games Lost:",
fieldNewGamesLost fieldNewGamesLost
}; };
int newOption = JOptionPane.showConfirmDialog( int newOption =
null, newMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION); JOptionPane.showConfirmDialog(
if (newOption != JOptionPane.OK_OPTION) null, newMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION);
return false; if (newOption != JOptionPane.OK_OPTION) return false;
String newName = fieldNewName.getText(); String newName = fieldNewName.getText();
int newGoals = Integer.parseInt(fieldNewGoals.getText()); int newGoals = Integer.parseInt(fieldNewGoals.getText());
@@ -192,7 +200,8 @@ public class HockeyStats {
new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost)); new HockeyPlayer(newName, newGoals, newAssists, newGamesWon, newGamesLost));
break; break;
case 2: 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( JOptionPane.showMessageDialog(
null, null,
players.removePlayer(rmName) players.removePlayer(rmName)
@@ -205,19 +214,19 @@ public class HockeyStats {
JTextField fieldAddAssists = new JTextField(); JTextField fieldAddAssists = new JTextField();
JTextField fieldAddGameWon = new JTextField(); JTextField fieldAddGameWon = new JTextField();
Object[] addMessage = { Object[] addMessage = {
"Player Name:", "Player Name:",
fieldAddName, fieldAddName,
"Player Goals Scored:", "Player Goals Scored:",
fieldAddGoals, fieldAddGoals,
"Player Assists:", "Player Assists:",
fieldAddAssists, fieldAddAssists,
"Game Won? (yes/no)", "Game Won? (yes/no)",
fieldAddGameWon fieldAddGameWon
}; };
int addOption = JOptionPane.showConfirmDialog( int addOption =
null, addMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION); JOptionPane.showConfirmDialog(
if (addOption != JOptionPane.OK_OPTION) null, addMessage, "Enter The Following Stats:", JOptionPane.OK_CANCEL_OPTION);
return false; if (addOption != JOptionPane.OK_OPTION) return false;
String addName = fieldAddName.getText(); String addName = fieldAddName.getText();
int addGoals = Integer.parseInt(fieldAddGoals.getText()); int addGoals = Integer.parseInt(fieldAddGoals.getText());
int addAssists = Integer.parseInt(fieldAddAssists.getText()); int addAssists = Integer.parseInt(fieldAddAssists.getText());
@@ -244,13 +253,13 @@ public class HockeyStats {
} }
/** /**
* Displays the viewer menu for printing, finding, and sorting player * Displays the viewer menu for printing, finding, and sorting player statistics.
* statistics.
* *
* @return true if the user chooses to return to the main menu, false otherwise. * @return true if the user chooses to return to the main menu, false otherwise.
*/ */
public static boolean showStatsMenu() { public static boolean showStatsMenu() {
String menu = """ String menu =
"""
<----------------------------> <---------------------------->
Hockey Stats Viewer Hockey Stats Viewer
@@ -279,7 +288,8 @@ public class HockeyStats {
JOptionPane.showMessageDialog(null, players.toString()); JOptionPane.showMessageDialog(null, players.toString());
break; break;
case 2: 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); HockeyPlayer p = players.findPlayer(findName);
JOptionPane.showMessageDialog(null, p == null ? "Player not found." : p.toString()); JOptionPane.showMessageDialog(null, p == null ? "Player not found." : p.toString());
break; break;
@@ -292,7 +302,8 @@ public class HockeyStats {
JOptionPane.showMessageDialog(null, "Sorted by points"); JOptionPane.showMessageDialog(null, "Sorted by points");
break; break;
case 5: 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); PlayerList pl = players.getPlayersGoalsAbove(minGoals);
JOptionPane.showMessageDialog(null, pl.toString()); JOptionPane.showMessageDialog(null, pl.toString());
break; break;