/** * Player.java * * Version: * $Id$ * * Revisions: * $Log$ */ /** * This class represents a baseball player in Major League Baseball. For * each player, we record the year the player won the home run champion * title, the name and team of the player, and the number of home runs the * player hit during the season. * * @author Robert Ghilduta */ public class Player implements Comparable { private String winyear; private String playername; private String playerteam; private int hr; /** * Create a new player. Initialize the name, the team, the year, and the * number of home runs of the player. * * @param winyear the year when the player won the title * @param playername the name of the player (e.g., "Alex Rodriguez") * @param playerteam the team of the player (e.g., "NewYork Yankees") * @param hr the number of home runs */ public Player (String winyear, String playername, String playerteam, int hr) { this.winyear = winyear; this.playername = playername; this.playerteam = playerteam; this.hr = hr; } /** * Get the year when the player won the title * * @return the winning year */ public String getYear () { return winyear; } /** * Get the name of this player * * @return the name of this player */ public String getName () { return playername; } /** * Get the team of this player * * @return the team of this player */ public String getTeam () { return playerteam; } /** * Get the number of home runs * * @return the number of home runs */ public int getHR () { return hr; } /** * Compares two players primarily for sorting * * @param other the player to be compared * @returns comparision between two players */ public int compareTo (Player other) { int stat = playername.compareTo(other.getName()); if (stat < 0) { return -1; } else if (stat > 0) { return 1; } stat = playerteam.compareTo(other.getTeam()); if (stat < 0) { return -1; } else if (stat > 0) { return 1; } return 0; } /** * Compare this player to the specified player. The result is true if * and only if the argument is not null and is a Player object that has * the same name and team as this object. * * @param other the object to compare this Player against * @return true if the Players are equal; false otherwise */ public boolean equals(Object other) { if (other instanceof Player) { Player p = (Player) other; return (compareTo(p) == 0); } return false; } /** * Get the hash code of this object. The hash code is defined as the sum * of the hash code of name and the hash code of team. */ public int hashCode () { return (playerteam.hashCode() + playername.hashCode()); } /** * Print the name, the team, the number of home runs, and the year of * this player. The format is "(team-name)'s (player-name) hit (# of * home runs) home runs in (year)." * * @return a string representation of this player */ public String toString () { return playerteam + "'s " + playername + " hit " + hr + " home runs in " + winyear; } }