changed I/O to JOptionPane

This commit is contained in:
CT
2026-05-06 23:44:37 -05:00
parent d7bc53caa8
commit 591bafdb77
3 changed files with 256 additions and 67 deletions

View File

@@ -1,3 +1,4 @@
/**
* HockeyStats.java
*
@@ -8,6 +9,7 @@
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
public class HockeyStats {
static PlayerList players = new PlayerList();
@@ -23,7 +25,8 @@ public class HockeyStats {
*/
public static void main(String[] args) throws Exception {
while (true) {
if (showMenu()) break;
if (showMenu())
break;
}
s.close();
}
@@ -50,6 +53,7 @@ public class HockeyStats {
Integer.parseInt(st.nextToken()), // gamesWon
Integer.parseInt(st.nextToken()))); // gamesLost
}
JOptionPane.showMessageDialog(null, "Data loaded from " + fileName);
br.close();
}
@@ -64,7 +68,7 @@ public class HockeyStats {
String save = players.createSaveData();
fw.write(save);
fw.close();
System.out.println("Data saved to " + fileName);
JOptionPane.showMessageDialog(null, "Data saved to " + fileName);
}
/**
@@ -74,8 +78,7 @@ public class HockeyStats {
* @throws Exception If input errors occur.
*/
public static boolean showMenu() throws Exception {
System.out.print(
"""
String menu = """
<---------------------------->
Hockey Stats Manager Menu
@@ -88,13 +91,12 @@ public class HockeyStats {
(5) Quit
<---------------------------->
""");
""";
int n;
try {
n = s.nextInt();
s.nextLine();
n = Integer.parseInt(JOptionPane.showInputDialog(menu));
} catch (Exception e) {
System.out.println("Please choose one of the available menu options!");
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
return false;
}
boolean close = false;
@@ -107,20 +109,22 @@ 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:
System.out.println("Thank you!");
JOptionPane.showMessageDialog(null, "Thank you!");
close = true;
break;
default:
System.out.println("Please choose one of the available menu options!");
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
break;
}
return close;
@@ -133,8 +137,7 @@ public class HockeyStats {
* @throws Exception If input errors occur.
*/
public static boolean showDataMenu() throws Exception {
System.out.print(
"""
String menu = """
<---------------------------->
Hockey Data Editor Menu
@@ -146,81 +149,108 @@ public class HockeyStats {
(4) Return to main menu
<---------------------------->
""");
""";
boolean dataExit = false;
int k;
try {
k = s.nextInt();
s.nextLine();
k = Integer.parseInt(JOptionPane.showInputDialog(menu));
} catch (Exception e) {
System.out.println("Please choose one of the available menu options!");
JOptionPane.showMessageDialog(null, "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();
JTextField fieldNewName = new JTextField();
JTextField fieldNewGoals = new JTextField();
JTextField fieldNewAssists = new JTextField();
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
};
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());
int newAssists = Integer.parseInt(fieldNewAssists.getText());
int newGamesWon = Integer.parseInt(fieldNewGamesWon.getText());
int newGamesLost = Integer.parseInt(fieldNewGamesLost.getText());
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(
String rmName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:");
JOptionPane.showMessageDialog(
null,
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");
JTextField fieldAddName = new JTextField();
JTextField fieldAddGoals = new JTextField();
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
};
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());
boolean addGameWon = Boolean.parseBoolean(fieldAddGameWon.getText());
HockeyPlayer found = players.findPlayer(addName);
if (found == null) {
System.out.println("Player not found.");
JOptionPane.showMessageDialog(null, "Player not found.");
} else {
found.addGame(addGoals, addAssists, addGameWon);
JOptionPane.showMessageDialog(null, "Game added to " + addName);
}
break;
case 4:
dataExit = true;
break;
default:
System.out.println("Please choose one of the available menu options!");
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
break;
}
} catch (Exception e) {
System.out.println("Please enter the right kind of data");
JOptionPane.showMessageDialog(null, "Please enter the right kind of data");
}
return dataExit;
}
/**
* 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() {
System.out.print(
"""
String menu = """
<---------------------------->
Hockey Stats Viewer
@@ -234,53 +264,47 @@ public class HockeyStats {
(6) Return to Main Menu
<---------------------------->
""");
""";
boolean statsExit = false;
int k;
try {
k = s.nextInt();
s.nextLine();
k = Integer.parseInt(JOptionPane.showInputDialog(menu));
} catch (Exception e) {
System.out.println("Please choose one of the available menu options!");
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
return false;
}
try {
switch (k) {
case 1:
System.out.println(players.toString());
JOptionPane.showMessageDialog(null, players.toString());
break;
case 2:
System.out.println("Enter the name of the player you want to find:");
String findName = s.nextLine();
String findName = JOptionPane.showInputDialog(null, "Enter the name of the player you want to find:");
HockeyPlayer p = players.findPlayer(findName);
System.out.println(p == null ? "Player not found." : p.toString());
JOptionPane.showMessageDialog(null, p == null ? "Player not found." : p.toString());
break;
case 3:
players.sortByNames();
System.out.println("Sorted by names");
JOptionPane.showMessageDialog(null, "Sorted by names");
break;
case 4:
players.sortByPoints();
System.out.println("Sorted by points");
JOptionPane.showMessageDialog(null, "Sorted by points");
break;
case 5:
System.out.println("Enter the min goals to show:");
int minGoals = s.nextInt();
s.nextLine();
int minGoals = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the min goals to show:"));
PlayerList pl = players.getPlayersGoalsAbove(minGoals);
System.out.println(pl.toString());
JOptionPane.showMessageDialog(null, pl.toString());
break;
case 6:
statsExit = true;
break;
default:
System.out.println("Please choose one of the available menu options!");
JOptionPane.showMessageDialog(null, "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");
JOptionPane.showMessageDialog(null, "Please enter the right kind of data");
}
return statsExit;
}