HockeyPlayer javadocs

This commit is contained in:
CT
2026-05-05 20:18:28 +00:00
parent 870f4c6680
commit 4088203644

View File

@@ -1,7 +1,23 @@
/**
* HockeyPlayer.java
*
* Represents a hockey player and tracks their performance statistics
* such as goals, assists, and win/loss records.
*
* @author Cody Trainer
*/
public class HockeyPlayer { public class HockeyPlayer {
private String name; private String name;
private int goals, assists, gamesWon, gamesLost; 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) { public HockeyPlayer(String n, int g, int a, int gw, int gl) {
name = n; name = n;
goals = g; goals = g;
@@ -10,43 +26,80 @@ public class HockeyPlayer {
gamesLost = gl; gamesLost = gl;
} }
/**
* Constructs a new HockeyPlayer with a name and zeroed statistics.
* @param name The player's name.
*/
public HockeyPlayer(String name) { public HockeyPlayer(String name) {
this.name = name; this.name = name;
} }
/**
* getName gets the player's name
* @return The player's name.
*/
public String getName() { public String getName() {
return name; return name;
} }
/**
* getGoals gets the player's total goals scored
* @return Total goals scored.
*/
public int getGoals() { public int getGoals() {
return goals; return goals;
} }
/**
* getAssists gets the player's total assists recorded
* @return Total assists recorded.
*/
public int getAssists() { public int getAssists() {
return assists; return assists;
} }
/**
* getGamesWon gets the player's total games won
* @return Total games won.
*/
public int getGamesWon() { public int getGamesWon() {
return gamesWon; return gamesWon;
} }
/** @return Total games lost. */
public int getGamesLost() { public int getGamesLost() {
return gamesLost; return gamesLost;
} }
/**
* Calculates total points (goals + assists).
* @return The sum of goals and assists.
*/
public int getPoints() { public int getPoints() {
return goals + assists; return goals + assists;
} }
/**
* Calculates total games played.
* @return The sum of games won and games lost.
*/
public int getGamesPlayed() { public int getGamesPlayed() {
return gamesWon + gamesLost; return gamesWon + gamesLost;
} }
/**
* Calculates the average points earned per game.
* @return Points divided by games played as a double.
*/
public double getPointsPerGame() { public double getPointsPerGame() {
return (double) getPoints() / (double) getGamesPlayed(); return (double) getPoints() / (double) getGamesPlayed();
} }
public String toString() { /**
* Returns a formatted string containing the player's full statistics.
* @return A multi-line summary of the player.
*/
public String toString() {
String s = String s =
String.format( String.format(
"Name: %s%nGoals: %d\tAssists: %d\tPoints: %d\tPoints Per Game: %f%nGames Won: %d" "Name: %s%nGoals: %d\tAssists: %d\tPoints: %d\tPoints Per Game: %f%nGames Won: %d"
@@ -62,20 +115,42 @@ public class HockeyPlayer {
return s; 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() { public String getData() {
return String.format("%s %d %d %d %d%n", name, goals, assists, gamesWon, gamesLost); 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) { public boolean equals(Object o) {
if (!(o instanceof HockeyPlayer)) return false;
HockeyPlayer p = (HockeyPlayer) o; HockeyPlayer p = (HockeyPlayer) o;
return this.name.equals(p.name); 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) { public int compareTo(Object o) {
HockeyPlayer p = (HockeyPlayer) o; HockeyPlayer p = (HockeyPlayer) o;
return this.name.compareTo(p.name); 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) { public void addGame(int goals, int assists, boolean gameWon) {
this.goals += goals; this.goals += goals;
this.assists += assists; this.assists += assists;