-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStatsArray.java
277 lines (231 loc) · 4.61 KB
/
StatsArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import java.util.*;
import java.io.File;
public class StatsArray
{
Stats[] sa;
int n;
final int maxRecords = 1000;
/*
* @return stats array from readFile();
*/
public StatsArray()
{
sa = new Stats[maxRecords];
readFile();
}
/*
* @return file at root directory called data.txt
*/
public void readFile()
{
try {
Scanner sc = new Scanner(new File("data.txt"));
int i = 0;
while (sc.hasNext())
{
String s = sc.nextLine();
String[] sd = s.split(",");
sa[i] = new Stats(sd[0], sd[1], sd[2], Double.parseDouble(sd[3]),Double.parseDouble(sd[4]),Double.parseDouble(sd[5]),Double.parseDouble(sd[6]));
i++;
}
n = i;
sc.close();
} catch (Exception e) {
System.out.println("Exception with StatsArray readFile");
}
}
/*
* @return total minutes for all teams in data set
*/
public double calcTotalMinutes()
{
double total = 0;
for(int i=0; i<n; i++)
{
total += sa[i].getMinutes();
}
return total;
}
/*
* @return total points for all teams in data set
*/
public double calcTotalPoints()
{
double total = 0;
for(int i=0; i<n; i++)
{
total += sa[i].getPoints();
}
return total;
}
/*
* @return total rebounds for all teams in data set
*/
public double calcTotalRebounds()
{
double total = 0;
for(int i=0; i<n; i++)
{
total += sa[i].getRebounds();
}
return total;
}
/*
* @return total assists for all teams in data set
*/
public double calcTotalAssists()
{
double total = 0;
for(int i=0; i<n; i++)
{
total += sa[i].getAssists();
}
return total;
}
/*
* @param Stats s1
* @param Stats s2
* @return comparison of params
*/
class SortByName implements Comparator<Stats>
{
public int compare(Stats s1, Stats s2)
{
return s1.getName().compareTo(s2.getName());
}
}
/*
* @return array of all players sorted by name alphabetically
*/
public void sortByName()
{
Arrays.sort(sa, 0, n, new SortByName());
}
/*
* @param Stats s1
* @param Stats s2
* @return comparison of params
*/
class SortByTeam implements Comparator<Stats>
{
public int compare(Stats s1, Stats s2)
{
return s1.getTeam().compareTo(s2.getTeam());
}
}
/*
* @return array of all players sorted by team alphabetically
*/
public void sortByTeam()
{
Arrays.sort(sa, 0, n, new SortByTeam());
}
/*
* @param Stats s1
* @param Stats s2
* @return comparison of params
*/
class SortByMinutes implements Comparator<Stats>
{
public int compare(Stats s1, Stats s2)
{
if (s1.getMinutes() > s2.getMinutes() ) return -1;
else if (s1.getMinutes() < s2.getMinutes() ) return 1;
else return 0;
}
}
/*
* @return array of all players sorted by MPG alphabetically
*/
public void sortByMinutes()
{
Arrays.sort(sa, 0, n, new SortByMinutes());
}
/*
* @param Stats s1
* @param Stats s2
* @return comparison of params
*/
class SortByPoints implements Comparator<Stats>
{
public int compare(Stats s1, Stats s2)
{
if (s1.getPoints() > s2.getPoints() ) return -1;
else if (s1.getPoints() < s2.getPoints() ) return 1;
else return 0;
}
}
/*
* @return array of all players sorted by PPG alphabetically
*/
public void sortByPoints()
{
Arrays.sort(sa, 0, n, new SortByPoints());
}
/*
* @param Stats s1
* @param Stats s2
* @return comparison of params
*/
class SortByRebounds implements Comparator<Stats>
{
public int compare(Stats s1, Stats s2)
{
if (s1.getRebounds() > s2.getRebounds() ) return -1;
else if (s1.getRebounds() < s2.getRebounds() ) return 1;
else return 0;
}
}
/*
* @return array of all players sorted by RPG alphabetically
*/
public void sortByRebounds()
{
Arrays.sort(sa, 0, n, new SortByRebounds());
}
/*
* @param Stats s1
* @param Stats s2
* @return comparison of params
*/
class SortByAssists implements Comparator<Stats>
{
public int compare(Stats s1, Stats s2)
{
if (s1.getAssists() > s2.getAssists() ) return -1;
else if (s1.getAssists() < s2.getAssists() ) return 1;
else return 0;
}
}
/*
* @return array of all players sorted by APG alphabetically
*/
public void sortByAssists()
{
Arrays.sort(sa, 0, n, new SortByAssists());
}
/*
* @param fname
* @param lname
* @return records for player with the correct fname and lname
*/
public String listPlayer(String fname, String lname)
{
String s = "";
for (int i=0; i<n; i++)
if (fname.equalsIgnoreCase(sa[i].getFirst()) &&
lname.equalsIgnoreCase(sa[i].getLast()))
s += sa[i] + "\n";
return s;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
String s = "";
for (int i=0; i<n; i++)
s += sa[i] + "\n";
return s;
}
}