javadocs for hockeystats and playerlist
This commit is contained in:
@@ -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;
|
||||
|
||||
public class PlayerList {
|
||||
ArrayList<HockeyPlayer> 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) {
|
||||
@@ -28,6 +54,12 @@ public class PlayerList {
|
||||
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) {
|
||||
@@ -36,6 +68,12 @@ public class PlayerList {
|
||||
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) {
|
||||
@@ -44,6 +82,12 @@ public class PlayerList {
|
||||
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) {
|
||||
@@ -54,6 +98,7 @@ public class PlayerList {
|
||||
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;
|
||||
@@ -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() {
|
||||
for (int i = 0; i < pList.size(); 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) {
|
||||
HockeyPlayer p = findPlayer(name);
|
||||
if (p == null) return null;
|
||||
|
||||
Reference in New Issue
Block a user