/*
* Created on Jun 29, 2004
*/
package baseball;
/**
* @author dickey
*/
public class Player implements Comparable {
/** First initial of the player's first name. */
char firstNameInitial;
/** Player's (full) last name. */
String lastName;
/** Abbreviation of the team on which the player players. */
String teamAbbrev;
/** Position (a code from the file for hitters,
* a constant for pitchers) */
private String position;
/** Create a new player object.
*
* @param fullName Player name as it comes from the input file.
* @param teamAbbrev Team abbreviation.
*/
public Player(String fullName, String teamAbbrev, String position) {
setPlayerName(fullName);
setTeamName(teamAbbrev);
this.position = position;
}
/** Separate the first initial and last name and save them
* in instance variables.
* The format of names on the input is
*
I LASTNAME
*
i.e., a single character initial, followed by one space
* (no period), and then the last name.
* @param fullName a string which has the first initial and the last name.
*/
private void setPlayerName(String fullName) {
this.firstNameInitial = fullName.charAt(0);
this.lastName = fullName.substring(2, fullName.length());
}
/** Save the team name, given its abbreviation. At present,
* the abbreviation itself is all that is saved. If desired, this
* method could convert the abbreviation to a fuller form of the name.
* @param teamAbbrev a string containing the abbreviated team name.
*/
private void setTeamName(String teamAbbrev) {
this.teamAbbrev = teamAbbrev;
}
public String getTeamName() {
return this.teamAbbrev;
}
/** Return a short string representation of the player.
*
*/
public String toString() {
return this.lastName + ", " + this.firstNameInitial + "." +
" " + this.teamAbbrev +
" (" + this.position + ")";
}
/** Compare players based on name (last name, first name in the usual
* way).
* @param otherPlayer an object which must be a non-null Player.
* @return the usual compareTo value: <0 if this object is less than
* (name comes before) the other object; >0 if this object is greater;
* 0 if the two are equal.
*/
public int compareTo(Object otherPlayer) {
String otherPlayerName = ((Player) otherPlayer).lastName;
int lastNameResult = this.lastName.compareTo(otherPlayerName);
if (lastNameResult != 0) {
return lastNameResult;
} else {
char otherPlayerInitial = ((Player) otherPlayer).firstNameInitial;
if (this.firstNameInitial < otherPlayerInitial) {
return -1;
} else {
if (this.firstNameInitial > otherPlayerInitial) {
return +1;
} else {
return 0; //names are fully the same
}
}
}
}
}