Files
csa-final-project/HockeyPlayer.java
2026-05-05 20:28:30 +00:00

179 lines
4.2 KiB
Java

/**
* HockeyPlayer.java
*
* <p>Represents a hockey player and tracks their performance statistics such as goals, assists, and
* win/loss records.
*
* @author Cody Trainer
*/
public class HockeyPlayer {
private String name;
private int goals, assists, gamesWon, gamesLost;
/**
* Constructs a new HockeyPlayer with full statistics.
*
* @param n The player's name.
* @param g Number of goals scored.
* @param a Number of assists made.
* @param gw Number of games won.
* @param gl Number of games lost.
*/
public HockeyPlayer(String n, int g, int a, int gw, int gl) {
name = n;
goals = g;
assists = a;
gamesWon = gw;
gamesLost = gl;
}
/**
* Constructs a new HockeyPlayer with a name and zeroed statistics.
*
* @param name The player's name.
*/
public HockeyPlayer(String name) {
this.name = name;
}
/**
* getName gets the player's name
*
* @return The player's name.
*/
public String getName() {
return name;
}
/**
* getGoals gets the player's total goals scored
*
* @return Total goals scored.
*/
public int getGoals() {
return goals;
}
/**
* getAssists gets the player's total assists recorded
*
* @return Total assists recorded.
*/
public int getAssists() {
return assists;
}
/**
* getGamesWon gets the player's total games won
*
* @return Total games won.
*/
public int getGamesWon() {
return gamesWon;
}
/**
* getGamesLost gets the player's total games lost
*
* @return Total games lost.
*/
public int getGamesLost() {
return gamesLost;
}
/**
* Calculates total points (goals + assists).
*
* @return The sum of goals and assists.
*/
public int getPoints() {
return goals + assists;
}
/**
* Calculates total games played.
*
* @return The sum of games won and games lost.
*/
public int getGamesPlayed() {
return gamesWon + gamesLost;
}
/**
* Calculates the average points earned per game.
*
* @return Points divided by games played as a double.
*/
public double getPointsPerGame() {
return (double) getPoints() / (double) getGamesPlayed();
}
/**
* Returns a formatted string containing the player's full statistics.
*
* @return A multi-line summary of the player.
*/
public String toString() {
String s =
String.format(
"Name: %s%nGoals: %d\tAssists: %d\tPoints: %d\tPoints Per Game: %f%nGames Won: %d"
+ "\tGames Lost: %d\tGames Played: %d%n",
name,
goals,
assists,
getPoints(),
getPointsPerGame(),
gamesWon,
gamesLost,
getGamesPlayed());
return s;
}
/**
* Returns the raw data of the player in a single line format.
*
* @return A string formatted as "name goals assists wins losses".
*/
public String getData() {
return String.format("%s %d %d %d %d%n", name, goals, assists, gamesWon, gamesLost);
}
/**
* Compares this player to another object for equality based on name.
*
* @param o The object to compare.
* @return true if the names are equal, false otherwise.
*/
public boolean equals(Object o) {
if (!(o instanceof HockeyPlayer)) return false;
HockeyPlayer p = (HockeyPlayer) o;
return this.name.equals(p.name);
}
/**
* Compares this player to another for sorting purposes based on name.
*
* @param o The object to compare.
* @return A negative integer, zero, or a positive integer as this name is less than, equal to, or
* greater than the specified player's name.
*/
public int compareTo(Object o) {
HockeyPlayer p = (HockeyPlayer) o;
return this.name.compareTo(p.name);
}
/**
* Updates the player's career statistics by adding a new game's results.
*
* @param goals Number of goals scored in the game.
* @param assists Number of assists made in the game.
* @param gameWon True if the game resulted in a win, false if a loss.
*/
public void addGame(int goals, int assists, boolean gameWon) {
this.goals += goals;
this.assists += assists;
if (gameWon) gamesWon++;
else gamesLost++;
}
}