lajdlksadlmla
[rmh3093.git] / lab6 / RCS / Player.java,v
blob0367cf033c4cd505d51dc594e6f4fa92a9542242
1 head    1.1;
2 access;
3 symbols;
4 locks
5         rmh3093:1.1; strict;
6 comment @# @;
9 1.1
10 date    2008.04.21.15.00.06;    author rmh3093; state Exp;
11 branches;
12 next    ;
15 desc
16 @create player class
20 1.1
21 log
22 @Initial revision
24 text
25 @/*
26  * Player.java
27  *
28  * Version:
29  *     $Id$
30  *
31  * Revisions:
32  *     $Log$
33  */
35 /**
36  * This class represents a baseball player in Major League Baseball. For each 
37  * player, we record the year the player won the home run champion title, the 
38  * name and team of the player, and the number of home runs the player hit 
39  * during the season.
40  *  
41  * @@author Ryan Hope
42  */
43 public class Player implements Comparable<Player> {
45         private String winyear, playername, playerteam;
46         private int hr; 
48         public Player(String winyear, String playername, String playerteam,
49                         int hr) {
50                 this.winyear = winyear;
51                 this.playername = playername;
52                 this.playerteam = playerteam;
53                 this.hr = hr;
54         }
56         /**
57          * Get the year when the player won the title.
58          * 
59          * @@return the winning year.
60          */
61         public String getYear() {
62                 return winyear;
63         }
65         /**
66          * Get the name of this player.
67          * 
68          * @@return the name of this player.
69          */
70         public String getName() {
71                 return playername;
72         }
74         /**
75          * Get the team of this player.
76          * 
77          * @@return the team of this player.
78          */
79         public String getTeam() {
80                 return playerteam;
81         }
83         /**
84          * Get the number of home runs.
85          * 
86          * @@return the number of home runs.
87          */
88         public int getHR() {
89                 return hr;
90         }
92         /**
93          * Compares two players primarily for sorting.
94          *
95          * @@param other the Player to be compared 
96          * @@return returns a value less than 0 if this player's name is 
97          * lexicographically less than the other player's name; returns a value 
98          * greater than 0 if this player's name is lexicographically greater than 
99          * the other player's name; if the two players have the same name, compare 
100          * their teams; that is, <0, 0, and >0 as the team of this player is less 
101          * than, equal to, or greater than the team of the other player 
102          * lexicographically.
103          */
104         public int compareTo(Player other) {
105                 int ret = 0;
106                 if (!(playername.equals(other.getName()))) {
107                         char[] thisplayer = playername.toCharArray();
108                         char[] otherplayer = other.getName().toCharArray();
109                         int len1 = thisplayer.length;
110                         int len2 = otherplayer.length;
111                         int len;
112                         if (len1<len2) {
113                                 len = len2;
114                         } else {
115                                 len = len1;
116                         }
117                         for (int i=0; i<len; i++) {
118                                 if (thisplayer[i] > otherplayer[i]) {
119                                         ret = 1;
120                                         break;
121                                 } else if (thisplayer[i] < otherplayer[i]) {
122                                         ret = -1;
123                                         break;
124                                 }
125                         }
126                 } else {
127                         char[] thisteam = playerteam.toCharArray();
128                         char[] otherteam = other.getTeam().toCharArray();
129                         int len1 = thisteam.length;
130                         int len2 = otherteam.length;
131                         int len;
132                         if (len1<len2) {
133                                 len = len2;
134                         } else {
135                                 len = len1;
136                         }
137                         for (int i=0; i<len; i++) {
138                                 if (thisteam[i] > otherteam[i]) {
139                                         ret = 1;
140                                         break;
141                                 } else if (thisteam[i] < otherteam[i]) {
142                                         ret = -1;
143                                         break;
144                                 }
145                         }
146                 }
147                 return ret;
148         }
150         /**
151          * Compare this player to the specified player. The result is true if and 
152          * only if the argument is not null and is a Player object that has the 
153          * same name and team as this object.
154          *  
155          * @@param other the object to compare this Player against
156          * @@return true if the Player are equal; false otherwise.
157          */
158         public boolean equals(Object other) {
159                 boolean ret = false;
160                 if (other != null) {
161                         Player o = (Player)other;
162                         if (playername.equals(o.getName())) {
163                                 if (playerteam.equals(o.getTeam())) {
164                                         ret = true;
165                                 }
166                         }
167                 }
168                 return ret;
169         }
171         /**
172          * Get the hash code of this object. The hash code is defined as the sum 
173          * of the hash code of name and the hash code of team. 
174          * 
175          * @@return the sum of the name and team hash code's.
176          */
177         public int hashCode() {
178                 return playername.hashCode() + playerteam.hashCode();
179         }
181         /**
182          * Makes a String with the name, the team, the number of home runs, and the 
183          * year of this player. The format is "(team-name)'s (player-name) hit 
184          * (# of home runs) home runs in (year)."
185          *  
186          * @@return a string representation of this player.
187          */
188         public String toString() {
189                 return playerteam + "'s " + playername + " hit " + hr + 
190                 " home runs in " + winyear;  
191         }
192