/** * PlayerList.java * *

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; public class PlayerList { ArrayList pList; /** Constructs an empty PlayerList. */ public PlayerList() { pList = new ArrayList<>(); } /** Removes all players from the list. */ public void clear() { pList.clear(); } /** * Adds a HockeyPlayer to the list. * * @param p The HockeyPlayer object to add. */ public void addPlayer(HockeyPlayer 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) { HockeyPlayer p = findPlayer(name); 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() { String s = ""; for (HockeyPlayer p : pList) { s += p.toString() + "\n"; } 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() { String s = pList.size() + "\n"; for (HockeyPlayer p : pList) { s += p.getData(); } 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) { HockeyPlayer newPlayer = new HockeyPlayer(name); for (HockeyPlayer p : pList) { if (p.equals(newPlayer)) return p; } 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) { PlayerList newList = new PlayerList(); for (HockeyPlayer p : pList) { if (p.getGoals() > num) { newList.addPlayer(p); } } return newList; } /** Sorts the players in the list alphabetically by name using a Bubble Sort algorithm. */ public void sortByNames() { for (int i = 0; i < pList.size(); i++) { boolean swapped = false; for (int j = 0; j < pList.size() - 1 - i; j++) { if (pList.get(j).compareTo(pList.get(j + 1)) > 0) { HockeyPlayer temp = pList.get(j); pList.set(j, pList.get(j + 1)); pList.set(j + 1, temp); swapped = true; } } if (!swapped) break; } } /** Sorts the players in the list by total points using a Selection Sort algorithm. */ public void sortByPoints() { for (int i = 0; i < pList.size(); i++) { int minInd = i; for (int j = i + 1; j < pList.size(); j++) { if (pList.get(minInd).getPoints() > pList.get(j).getPoints()) { minInd = j; } } HockeyPlayer temp = pList.get(minInd); pList.set(minInd, pList.get(i)); pList.set(i, temp); } } /** Sorts the players in the list by total goals using an Insertion Sort algorithm. */ public void sortByGoals() { int n = pList.size(); for (int i = 0; i < n; i++) { HockeyPlayer key = pList.get(i); int j = i - 1; while (j >= 0 && pList.get(j).getGoals() < key.getGoals()) { pList.set(j + 1, pList.get(j)); j--; } pList.set(j + 1, key); } } /** * 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) { HockeyPlayer p = findPlayer(name); if (p == null) return null; else { p.addGame(goals, assists, gameWon); } return p; } }