/
EventDB.java
301 lines (272 loc) · 8.93 KB
/
EventDB.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
///////////////////////////////////////////////////////////////////////////////
// ALL STUDENTS COMPLETE THESE SECTIONS
// Main Class File: EventDB.java
// File: EventDB
// Semester: CS367 Spring 2013
//
// Author: Nicholas Stamas, nstamas@wisc.edu
// CS Login: stamas
// Lecturer's Name: Jim Skrentny
// Lab Section: -
//
// PAIR PROGRAMMERS COMPLETE THIS SECTION
// Pair Partner: Calvin Hareng, hareng@wisc.edu
// CS Login: hareng
// Lecturer's Name: Jim Skrentny
// Lab Section: -
//
// STUDENTS WHO GET HELP FROM ANYONE OTHER THAN THEIR PARTNER
// Credits: (list anyone who helped you write your program)
//////////////////////////// 80 columns wide //////////////////////////////////
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/** EventDB
* This class is the data base for storing athletes and events. It includes
* several public methods for accessing, adding, and removing data from the
* database. It also stores objects of type Event in an arrayList to keep
* track of the events that are in the database.
*
* @author Calvin Hareng and Nick Stamas
*/
public class EventDB{
private ArrayList<Event> events;
public EventDB (){
events = new ArrayList<Event>();
}
/**
* addEvent(String t)
* This method creates an iterator that iterates through the events. The name
* of the event that is passed in as a parameter, compared with each event
* already created. If it is already in the database, the code breaks from the
* while loop and does not add the event. If it goes through the entire
* database without finding a match, the event will be added to the DB.
*
* @param t The event name that is compared with all of the events already
* created.
*/
public void addEvent(String t){
boolean check = true;
Iterator<Event> itr = events.iterator();
while (itr.hasNext()){
if (itr.next().getType().equalsIgnoreCase(t)){
check = false;
break;
}
}
if(check){
Event newEvent = new Event(t);
events.add(newEvent);
}
}
/**
* addAthlete(String n, String t)
* This method creates an iterator, itr, and iterates through each event to
* match the event passed into the parameter, t. If t matches an event,
* it will check to make sure the athlete is not already in that event, and
* then add that athlete to the event. If the event does not match, or if the
* athlete is already in that event, the athlete will not be added
*
* @param t The event name that is compared with the events already created
* @param n The athlete name that is compared with the athletes already added
*
*/
public void addAthlete(String n, String t){
Iterator<Event> itr1 = events.iterator();
Event currentEvent;
boolean eventExists = false;
boolean contains = false;
while(itr1.hasNext()){
if(itr1.next().getType().equalsIgnoreCase(t)){
eventExists = true;
}
}
if(!eventExists){throw new java.lang.IllegalArgumentException("Error " +
"event not found");}
itr1 = events.iterator();
while (itr1.hasNext()){
currentEvent = itr1.next();
if (currentEvent.getType().equalsIgnoreCase(t)){
Iterator<String> itr2 = currentEvent.getRoster().iterator();
while(itr2.hasNext()){
String currAthlete = itr2.next();
if(currAthlete.equalsIgnoreCase(n)){
contains = true;
break;
}
}
if(!contains){
currentEvent.getRoster().add(n);
}
}
}
}
/**
* removeEvent(String t)
* This method creates an iterator, itr, and iterates through each event to
* match the event passed into the parameter, t. If t matches an event,
* the method will remove the event, t, from the database, and it will return
* true. If the event, t, does not match a current event, it will return false.
*
* @param t The event name that is compared with the events already created
* @return true if the event is removed and false if the event, t, is not
* in the database and therefore not removed.
*/
public boolean removeEvent(String t){
Iterator<Event> itr = events.iterator();
Event currentEvent;
while (itr.hasNext()){
currentEvent = itr.next();
if (currentEvent.getType().equalsIgnoreCase(t)){
events.remove(currentEvent);
return true;
}
} return false;
}
/**
* containsEvent(String t)
* This method creates an iterator, itr, and iterates through each event to
* match the event passed into the parameter, t. If t matches an event,
* the method will return true, otherwise it will return false.
*
* @param t The event name that is compared with the events already created
* @return true if the event, t, is in the database otherwise false
*/
public boolean containsEvent(String t){
Iterator<Event> itr = events.iterator();
while (itr.hasNext()){
if (itr.next().getType().equalsIgnoreCase(t)){
return true;
}
}return false;
}
/**
* containsAthlete(String n)
* This method goes through each event and checks to see if any of the events
* contain the specified athlete, n. If it does contain the athlete, it
* returns true, else it returns false.
*
* @param n The athlete name that the method is searching for.
* @return true if the athlete is in the database, otherwise false.
*/
public boolean containsAthlete(String n){
for (int i = 0; i < events.size(); i++){
Iterator<String> itr2 = events.get(i).getRoster().iterator();
while (itr2.hasNext()){
String currAthlete = itr2.next();
if(currAthlete.equalsIgnoreCase(n)){
return true;
}
}
}return false;
}
/**
* isRegistered(String n, String t)
* Returns true if the athlete, n, is registered in the event, t, otherwise
* the method returns false.
*
* @param n The athlete name that the method is searching for
* @param t The event name that the method is searching for
* @return true if the athlete, n, is registered in the event, t.
*/
public boolean isRegistered(String n, String t){
Iterator<Event> itr = events.iterator();
Event currentEvent;
while (itr.hasNext()){
currentEvent = itr.next();
if (currentEvent.getType().equalsIgnoreCase(t)){
Iterator<String> itr2 = currentEvent.getRoster().iterator();
while(itr2.hasNext()){
String currAthlete = itr2.next();
if(currAthlete.equalsIgnoreCase(n)){
return true;
}
}
}
}
return false;
}
/**
* getRoster(String t)
* Retrieves the athlete roster from the event, t, and returns it. If the
* event is not in the database, it returns null.
*
* @param t The event name that the method is searching for
* @return currentEvent.getRoster(), the roster, if the event is found, else it
* returns null if the event is empty or not in the database.
*/
public List<String> getRoster(String t){
Iterator<Event> itr = events.iterator();
Event currentEvent;
while (itr.hasNext()){
currentEvent = itr.next();
// checking to make sure event is register
if (currentEvent.getType().equalsIgnoreCase(t)){
return currentEvent.getRoster();
}
}
return null;
}
/**
* getEvents(String n)
* Returns the list of events that the athlete, n, is in. If the athlete, n,
* does not exist, or they are not registered in any events, the method
* returns null.
*
* @param n The athlete that the method is checking for
* @return nEventList, the list of events that the athlete is in, or returns
* null if the athlete is not in any events or if the athlete doesn't exist.
*/
public List<String> getEvents(String n){
List<String> nEventList = new ArrayList<String>();
for (int i = 0; i < events.size(); i++){
if (isRegistered(n, events.get(i).getType())){
nEventList.add(events.get(i).getType());
}
}
if(!nEventList.isEmpty()){
return nEventList;
}else {return null;}
}
/**
* iterator()
* This method returns an iterator, itr, which iterates through a list of
* objects of type Event.
*
* @return itr, which is an iterator for objects of type Event
*/
public Iterator<Event> iterator() {
Iterator<Event> itr = events.iterator();
return itr;
}
/**
* size()
* This method returns the number of events that are stored in the database
*
* @return events.size(), which is the # of events in the DB
*/
public int size(){
return events.size();
}
/**
* removeAthlete(String n)
* This method deletes an athlete from the database.
*
* @return isRemoved is true if the athlete is successfully removed from the
* database and is false if the athlete is not in the database.
*/
boolean removeAthlete(String n){
boolean isRemoved = false;
for (int i = 0; i < events.size(); i++){
if (isRegistered(n, events.get(i).getType())){
for(int e = 0; e < events.get(i).getRoster().size(); e++){
if(events.get(i).getRoster().get(e).equalsIgnoreCase(n)){
events.get(i).getRoster().remove(e);
}
}
isRemoved = true;
}
}
return isRemoved;
}
}