351 lines
11 KiB
Java
351 lines
11 KiB
Java
/**
|
|
* HockeyStats.java
|
|
*
|
|
* <p>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.*;
|
|
import javax.swing.*;
|
|
|
|
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
|
|
}
|
|
JOptionPane.showMessageDialog(null, "Data loaded from " + fileName);
|
|
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();
|
|
JOptionPane.showMessageDialog(null, "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 {
|
|
String menu =
|
|
"""
|
|
<---------------------------->
|
|
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 {
|
|
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;
|
|
}
|
|
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:
|
|
JOptionPane.showMessageDialog(null, "Thank you!");
|
|
close = true;
|
|
break;
|
|
default:
|
|
JOptionPane.showMessageDialog(null, "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 {
|
|
String menu =
|
|
"""
|
|
<---------------------------->
|
|
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 {
|
|
String input = JOptionPane.showInputDialog(menu);
|
|
if (input == null) {
|
|
throw new NumberFormatException("Cancel or Exit clicked");
|
|
}
|
|
if (input == "") {
|
|
throw new IllegalArgumentException("No input");
|
|
}
|
|
k = 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;
|
|
}
|
|
try {
|
|
switch (k) {
|
|
case 1:
|
|
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:
|
|
String rmName =
|
|
JOptionPane.showInputDialog(null, "Enter the name of the player you want to remove:");
|
|
if (rmName == null) break;
|
|
JOptionPane.showMessageDialog(
|
|
null,
|
|
players.removePlayer(rmName)
|
|
? rmName + " was successfully removed."
|
|
: "Player not found.");
|
|
break;
|
|
case 3:
|
|
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) {
|
|
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:
|
|
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
|
break;
|
|
}
|
|
} catch (Exception e) {
|
|
JOptionPane.showMessageDialog(null, "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() {
|
|
String menu =
|
|
"""
|
|
<---------------------------->
|
|
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 {
|
|
String input = JOptionPane.showInputDialog(menu);
|
|
if (input == null) {
|
|
throw new NumberFormatException("Cancel or Exit clicked");
|
|
}
|
|
if (input == "") {
|
|
throw new IllegalArgumentException("No input");
|
|
}
|
|
k = 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;
|
|
}
|
|
try {
|
|
switch (k) {
|
|
case 1:
|
|
JOptionPane.showMessageDialog(null, players.toString());
|
|
break;
|
|
case 2:
|
|
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;
|
|
case 3:
|
|
players.sortByNames();
|
|
JOptionPane.showMessageDialog(null, "Sorted by names");
|
|
break;
|
|
case 4:
|
|
players.sortByPoints();
|
|
JOptionPane.showMessageDialog(null, "Sorted by points");
|
|
break;
|
|
case 5:
|
|
String minInput = JOptionPane.showInputDialog(null, "Enter the min goals to show:");
|
|
if (minInput == null) {
|
|
break;
|
|
}
|
|
if (minInput.equals("")) {
|
|
JOptionPane.showMessageDialog(null, "Enter a value");
|
|
break;
|
|
}
|
|
int minGoals = Integer.parseInt(minInput);
|
|
PlayerList pl = players.getPlayersGoalsAbove(minGoals);
|
|
JOptionPane.showMessageDialog(null, pl.toString());
|
|
break;
|
|
case 6:
|
|
statsExit = true;
|
|
break;
|
|
default:
|
|
JOptionPane.showMessageDialog(null, "Please choose one of the available menu options!");
|
|
break;
|
|
}
|
|
} catch (Exception e) {
|
|
JOptionPane.showMessageDialog(null, "Please enter the right kind of data");
|
|
}
|
|
return statsExit;
|
|
}
|
|
}
|