javadocs for hockeystats and playerlist
This commit is contained in:
@@ -1,3 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* 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.io.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -7,14 +15,25 @@ public class HockeyStats {
|
|||||||
static StringTokenizer st;
|
static StringTokenizer st;
|
||||||
static Scanner s = new Scanner(System.in);
|
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 {
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
if (showMenu()) break;
|
if (showMenu()) break;
|
||||||
}
|
}
|
||||||
s.close();
|
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 {
|
public static void loadData(String fileName) throws Exception {
|
||||||
players.clear();
|
players.clear();
|
||||||
File f = new File(fileName);
|
File f = new File(fileName);
|
||||||
@@ -34,6 +53,12 @@ public class HockeyStats {
|
|||||||
br.close();
|
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 {
|
public static void saveData(String fileName) throws Exception {
|
||||||
FileWriter fw = new FileWriter(fileName);
|
FileWriter fw = new FileWriter(fileName);
|
||||||
String save = players.createSaveData();
|
String save = players.createSaveData();
|
||||||
@@ -42,6 +67,12 @@ public class HockeyStats {
|
|||||||
System.out.println("Data saved to " + fileName);
|
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 {
|
public static boolean showMenu() throws Exception {
|
||||||
System.out.print(
|
System.out.print(
|
||||||
"""
|
"""
|
||||||
@@ -95,6 +126,12 @@ public class HockeyStats {
|
|||||||
return close;
|
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 {
|
public static boolean showDataMenu() throws Exception {
|
||||||
System.out.print(
|
System.out.print(
|
||||||
"""
|
"""
|
||||||
@@ -176,6 +213,11 @@ public class HockeyStats {
|
|||||||
return dataExit;
|
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() {
|
public static boolean showStatsMenu() {
|
||||||
System.out.print(
|
System.out.print(
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,25 +1,51 @@
|
|||||||
|
/**
|
||||||
|
* PlayerList.java
|
||||||
|
*
|
||||||
|
* <p>Manages a collection of HockeyPlayer objects. Provides functionality for adding, removing,
|
||||||
|
* searching, and sorting players, as well as generating data for storage.
|
||||||
|
*
|
||||||
|
* @author Cody Trainer
|
||||||
|
*/
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class PlayerList {
|
public class PlayerList {
|
||||||
ArrayList<HockeyPlayer> pList;
|
ArrayList<HockeyPlayer> pList;
|
||||||
|
|
||||||
|
/** Constructs an empty PlayerList. */
|
||||||
public PlayerList() {
|
public PlayerList() {
|
||||||
pList = new ArrayList<>();
|
pList = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes all players from the list. */
|
||||||
public void clear() {
|
public void clear() {
|
||||||
pList.clear();
|
pList.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a HockeyPlayer to the list.
|
||||||
|
*
|
||||||
|
* @param p The HockeyPlayer object to add.
|
||||||
|
*/
|
||||||
public void addPlayer(HockeyPlayer p) {
|
public void addPlayer(HockeyPlayer p) {
|
||||||
pList.add(p);
|
pList.add(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes a player from the list by their name.
|
||||||
|
*
|
||||||
|
* @param name The name of the player to remove.
|
||||||
|
* @return true if the player was found and removed, false otherwise.
|
||||||
|
*/
|
||||||
public boolean removePlayer(String name) {
|
public boolean removePlayer(String name) {
|
||||||
HockeyPlayer p = findPlayer(name);
|
HockeyPlayer p = findPlayer(name);
|
||||||
return pList.remove(p);
|
return pList.remove(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a concatenated string of all players' detailed statistics.
|
||||||
|
*
|
||||||
|
* @return A multi-line string containing all player stats.
|
||||||
|
*/
|
||||||
public String toString() {
|
public String toString() {
|
||||||
String s = "";
|
String s = "";
|
||||||
for (HockeyPlayer p : pList) {
|
for (HockeyPlayer p : pList) {
|
||||||
@@ -28,6 +54,12 @@ public class PlayerList {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a raw data string formatted for file output. The format includes the list size followed
|
||||||
|
* by individual player data.
|
||||||
|
*
|
||||||
|
* @return A string representation of the list for saving to a file.
|
||||||
|
*/
|
||||||
public String createSaveData() {
|
public String createSaveData() {
|
||||||
String s = pList.size() + "\n";
|
String s = pList.size() + "\n";
|
||||||
for (HockeyPlayer p : pList) {
|
for (HockeyPlayer p : pList) {
|
||||||
@@ -36,6 +68,12 @@ public class PlayerList {
|
|||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches for a player in the list by name.
|
||||||
|
*
|
||||||
|
* @param name The name of the player to find.
|
||||||
|
* @return The HockeyPlayer object if found, null otherwise.
|
||||||
|
*/
|
||||||
public HockeyPlayer findPlayer(String name) {
|
public HockeyPlayer findPlayer(String name) {
|
||||||
HockeyPlayer newPlayer = new HockeyPlayer(name);
|
HockeyPlayer newPlayer = new HockeyPlayer(name);
|
||||||
for (HockeyPlayer p : pList) {
|
for (HockeyPlayer p : pList) {
|
||||||
@@ -44,6 +82,12 @@ public class PlayerList {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filters the list to find players who have scored more than a specific number of goals.
|
||||||
|
*
|
||||||
|
* @param num The goal threshold.
|
||||||
|
* @return A new PlayerList containing only players above the threshold.
|
||||||
|
*/
|
||||||
public PlayerList getPlayersGoalsAbove(int num) {
|
public PlayerList getPlayersGoalsAbove(int num) {
|
||||||
PlayerList newList = new PlayerList();
|
PlayerList newList = new PlayerList();
|
||||||
for (HockeyPlayer p : pList) {
|
for (HockeyPlayer p : pList) {
|
||||||
@@ -54,6 +98,7 @@ public class PlayerList {
|
|||||||
return newList;
|
return newList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Sorts the players in the list alphabetically by name using a Bubble Sort algorithm. */
|
||||||
public void sortByNames() {
|
public void sortByNames() {
|
||||||
for (int i = 0; i < pList.size(); i++) {
|
for (int i = 0; i < pList.size(); i++) {
|
||||||
boolean swapped = false;
|
boolean swapped = false;
|
||||||
@@ -69,8 +114,7 @@ public class PlayerList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (pList.get(i).getPoints() > pList.get(j).getPoints()) {
|
/** Sorts the players in the list by total points using a Selection Sort algorithm. */
|
||||||
|
|
||||||
public void sortByPoints() {
|
public void sortByPoints() {
|
||||||
for (int i = 0; i < pList.size(); i++) {
|
for (int i = 0; i < pList.size(); i++) {
|
||||||
int minInd = i;
|
int minInd = i;
|
||||||
@@ -85,6 +129,15 @@ public class PlayerList {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locates a player and updates their stats with results from a single game.
|
||||||
|
*
|
||||||
|
* @param name The name of the player.
|
||||||
|
* @param goals Goals scored in the game.
|
||||||
|
* @param assists Assists made in the game.
|
||||||
|
* @param gameWon Whether the game was won.
|
||||||
|
* @return The updated HockeyPlayer object, or null if the player doesn't exist.
|
||||||
|
*/
|
||||||
public HockeyPlayer addGameToPlayer(String name, int goals, int assists, boolean gameWon) {
|
public HockeyPlayer addGameToPlayer(String name, int goals, int assists, boolean gameWon) {
|
||||||
HockeyPlayer p = findPlayer(name);
|
HockeyPlayer p = findPlayer(name);
|
||||||
if (p == null) return null;
|
if (p == null) return null;
|
||||||
|
|||||||
Reference in New Issue
Block a user