lajdlksadlmla
[rmh3093.git] / lab6 / Player.java
blobb3751bdd5a41bd728902273aea7618f2932368d4
1 /*
2 * Player.java
4 * Version:
5 * $Id: Player.java,v 1.1 2008/04/21 15:00:06 rmh3093 Exp rmh3093 $
7 * Revisions:
8 * $Log: Player.java,v $
9 * Revision 1.1 2008/04/21 15:00:06 rmh3093
10 * Initial revision
14 /**
15 * This class represents a baseball player in Major League Baseball. For each
16 * player, we record the year the player won the home run champion title, the
17 * name and team of the player, and the number of home runs the player hit
18 * during the season.
20 * @author Ryan Hope
22 public class Player implements Comparable<Player> {
24 private String winyear, playername, playerteam;
25 private int hr;
27 public Player(String winyear, String playername, String playerteam,
28 int hr) {
29 this.winyear = winyear;
30 this.playername = playername;
31 this.playerteam = playerteam;
32 this.hr = hr;
35 /**
36 * Get the year when the player won the title.
38 * @return the winning year.
40 public String getYear() {
41 return winyear;
44 /**
45 * Get the name of this player.
47 * @return the name of this player.
49 public String getName() {
50 return playername;
53 /**
54 * Get the team of this player.
56 * @return the team of this player.
58 public String getTeam() {
59 return playerteam;
62 /**
63 * Get the number of home runs.
65 * @return the number of home runs.
67 public int getHR() {
68 return hr;
71 /**
72 * Compares two players primarily for sorting.
74 * @param other the Player to be compared
75 * @return returns a value less than 0 if this player's name is
76 * lexicographically less than the other player's name; returns a value
77 * greater than 0 if this player's name is lexicographically greater than
78 * the other player's name; if the two players have the same name, compare
79 * their teams; that is, <0, 0, and >0 as the team of this player is less
80 * than, equal to, or greater than the team of the other player
81 * lexicographically.
83 public int compareTo(Player other) {
84 int ret = 0;
85 if (!(playername.equals(other.getName()))) {
86 char[] thisplayer = playername.toCharArray();
87 char[] otherplayer = other.getName().toCharArray();
88 int len1 = thisplayer.length;
89 int len2 = otherplayer.length;
90 int len;
91 if (len1<len2) {
92 len = len2;
93 } else {
94 len = len1;
96 for (int i=0; i<len; i++) {
97 if (thisplayer[i] > otherplayer[i]) {
98 ret = 1;
99 break;
100 } else if (thisplayer[i] < otherplayer[i]) {
101 ret = -1;
102 break;
105 } else {
106 char[] thisteam = playerteam.toCharArray();
107 char[] otherteam = other.getTeam().toCharArray();
108 int len1 = thisteam.length;
109 int len2 = otherteam.length;
110 int len;
111 if (len1<len2) {
112 len = len2;
113 } else {
114 len = len1;
116 for (int i=0; i<len; i++) {
117 if (thisteam[i] > otherteam[i]) {
118 ret = 1;
119 break;
120 } else if (thisteam[i] < otherteam[i]) {
121 ret = -1;
122 break;
126 return ret;
130 * Compare this player to the specified player. The result is true if and
131 * only if the argument is not null and is a Player object that has the
132 * same name and team as this object.
134 * @param other the object to compare this Player against
135 * @return true if the Player are equal; false otherwise.
137 public boolean equals(Object other) {
138 boolean ret = false;
139 if (other != null) {
140 Player o = (Player)other;
141 if (playername.equals(o.getName())) {
142 if (playerteam.equals(o.getTeam())) {
143 ret = true;
147 return ret;
151 * Get the hash code of this object. The hash code is defined as the sum
152 * of the hash code of name and the hash code of team.
154 * @return the sum of the name and team hash code's.
156 public int hashCode() {
157 return playername.hashCode() + playerteam.hashCode();
161 * Makes a String with the name, the team, the number of home runs, and the
162 * year of this player. The format is "(team-name)'s (player-name) hit
163 * (# of home runs) home runs in (year)."
165 * @return a string representation of this player.
167 public String toString() {
168 return playerteam + "'s " + playername + " hit " + hr +
169 " home runs in " + winyear;