-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClub.java
331 lines (314 loc) · 10.7 KB
/
Club.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import java.util.ArrayList;
import java.util.Iterator;
import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.Collections;
/**
* Store details of club memberships.
*
* @author n-c0de-r
* @version 22-10-04
*/
public class Club
{
// Assignment 1
ArrayList<Membership> memberships;
/**
* Constructor for objects of class Club
*/
public Club()
{
// Assignment 1
memberships = new ArrayList<Membership>();
// Helper method, fills club with members for testing
addTestMembers();
}
/**
* Add a new member to the club's list of members.
* @param member The member object to be added.
*/
public void join(Membership member)
{
// Assignment 6
String searchName = member.getName();
Membership found = search(searchName);
if (found != null) {
System.out.println(searchName + " is already a member!");
return; // Breaks out of the function immediately.
}
memberships.add(member);
}
/**
* Returns the number of members in this club.
* @return The number of members (Membership objects) in
* the club.
*/
public int numberOfMembers()
{
return memberships.size(); // Assignment 2
}
/**
* Assignment 4
* Returns a Member who joined in a specific month and year.
* @params month Month to look up.
* @params year Year to look up.
* @return Persons who joined at that time.
*/
public int joinedInMonthOfYear(int month, int year) {
int result = 0;
for (Membership member : memberships) {
if (member.getMonth() == month) {
if (member.getYear() == year) {
result++;
}
}
}
return result;
}
/**
* Assignment 5
* Returns a list of Members who joined in a given month & year.
* @params month Month to look up.
* @params year Year to look up.
* @return List of persons who joined at that time.
*/
public ArrayList listJoinedInMonthOfYear(int month, int year) {
ArrayList<Membership> result = new ArrayList<Membership>();
for (Membership member : memberships) {
if (member.getMonth() == month) {
if (member.getYear() == year) {
result.add(member);
}
}
}
// Sort by name in the end. For the bored
Collections.sort(result, new NameComparator());
return result;
}
/**
* Assignment 6
* Returns a Members with the given name.
* @params name Name of Member to look up.
* @return Membership with the name.
*/
public Membership search(String name) {
name = name.toLowerCase(); // Makes comparison easier here
for (Membership member : memberships) {
String memberName = member.getName().toLowerCase();
if (memberName.equals(name)) {
return member;
}
}
return null;
}
/**
* Assignment 8, for the bored;
* Removes members who joined in a given month & year.
* Returns a list of the removed Members.
* @params month Month to look up.
* @params year Year to look up.
* @return List of persons who were removed.
*/
public ArrayList removeAllOfMonthAndYear(int month, int year) {
ArrayList<Membership> result = new ArrayList<Membership>();
// To remove from lists safely, an iterator is best to use
Iterator<Membership> it = memberships.iterator();
Membership checkMember;
while (it.hasNext()) {
checkMember = it.next();
if (checkMember.getMonth() == month) {
if (checkMember.getYear() == year) {
result.add(checkMember);
it.remove();
}
}
}
return result;
}
/**
* Assignment 9; for the bored
* Returns a list sorted year, then month, and finally name.
* @return Sorted list.
*/
public ArrayList sortList() {
ArrayList<Membership> result;
// Copies the original list first
result = new ArrayList<Membership>(memberships);
// Sort fully by year first, we will this further
Collections.sort(result, new YearComparator());
Collections.sort(result, new YearMonthNameComparator());
return result;
}
/**
* Assignment 10
* Returns a list of Members who have birthday in a given month.
* @params month Month to look up.
* @return List of persons who have birthday at that time.
*/
public ArrayList listBirthdaysInMonth(int month) {
ArrayList<Membership> result = new ArrayList<Membership>();
for (Membership member : memberships) {
if (member.getBdayMonth() == month) {
result.add(member);
}
}
return result;
}
/**
* Assignment 10; for the bored
* Returns a list of Members who have round birthdays.
* @return List of persons who have birthday at that time.
*/
public ArrayList listRoundBirthdays() {
ArrayList<Membership> result = new ArrayList<Membership>();
// Gets the current system's year
int thisYear = LocalDateTime.now().getYear();
for (Membership member : memberships) {
int difference = thisYear - member.getBdayYear();
if (difference % 5 == 0 || difference % 10 == 10) {
result.add(member);
}
}
return result;
}
/**
* Assignment 10; for the bored
* Returns a list of Members who have prime birthdays.
* @return List of persons who have birthday at that time.
*/
public ArrayList listPrimeBirthdays() {
ArrayList<Membership> result = new ArrayList<Membership>();
int thisYear = LocalDateTime.now().getYear();
for (Membership member : memberships) {
int difference = thisYear - member.getBdayYear();
if (isPrime(difference)) {
result.add(member);
}
}
return result;
}
/**
* Assignment 10; for the bored
* Returns a list of Members who have computing birthdays.
* @return List of persons who have birthday at that time.
*/
public ArrayList listComputingBirthdays() {
ArrayList<Membership> result = new ArrayList<Membership>();
int thisYear = LocalDateTime.now().getYear();
for (Membership member : memberships) {
int difference = thisYear - member.getBdayYear();
// No idea what "cumputing" numbers are
}
return result;
}
/**
* Assignment 3;
* Extended for Assigment 10;
* Adds some Members for testing.
*/
private void addTestMembers() {
join(new Membership("Ryu", 2, 2017, 5, 1985));
join(new Membership("Rei", 3, 2007, 7, 1987));
join(new Membership("Teepo", 5, 2009, 10, 1990));
join(new Membership("Garr", 7, 2011, 12, 1992));
join(new Membership("Nina", 9, 2013, 1, 1994));
join(new Membership("Momo", 11, 2015, 4, 1997));
join(new Membership("Honey", 3, 2017, 9, 2000));
join(new Membership("Deis", 3, 2019, 3, 1999));
join(new Membership("Myria", 1, 2017, 3, 1999));
join(new Membership("Ladon", 2, 2017, 12, 1000));
join(new Membership("Alice", 2, 2017, 12, 1000));
}
/**
* Assignment 10; Helper Method
* Checks, if an given number is a prime.
* @param a The number to be checked.
* @return true if it' is a prime.
*/
private boolean isPrime(int num) {
// 2 is the only even prime...
if (num == 2)
{
return true;
}
//... all other evens are not primes.
if (num % 2 == 0)
{
return false;
}
// Calculate the limit once, saves cpu time!
double limit = Math.sqrt(num);
// Check until the square root of the number
// after that the factors flip anyway.
// Check only every second number (odds), left ones.
for (int i = 3; i <= limit; i+=2) {
// if this is true, it's not a prime
if (num % i == 0) {
return false;
}
}
return true; // if all are checked
}
// Inner classes of Comparator to compare and sort objects
class NameComparator implements Comparator<Membership>{
@Override
public int compare(Membership m1, Membership m2) {
String name1 = m1.getName();
String name2 = m2.getName();
return name1.compareTo(name2);
}
}
class MonthComparator implements Comparator<Membership>{
@Override
public int compare(Membership m1, Membership m2) {
Integer month1 = m1.getMonth(); // Autoboxing
Integer month2 = m2.getMonth();
return month1.compareTo(month2);
}
}
class YearComparator implements Comparator<Membership>{
@Override
public int compare(Membership m1, Membership m2) {
Integer year1 = m1.getYear(); // Autoboxing
Integer year2 = m2.getYear();
return year1.compareTo(year2);
}
}
// Assignment 9, for the bored
class YearMonthNameComparator implements Comparator<Membership>{
@Override
public int compare(Membership m1, Membership m2) {
int result;
// Sort by year fist
Integer year1 = m1.getYear(); // Autoboxing
Integer year2 = m2.getYear();
int years = year1.compareTo(year2);
if ((result = years) != 0) {
return result;
}
// If years are same, sort by months
Integer month1 = m1.getMonth(); // Autoboxing
Integer month2 = m2.getMonth();
int months = month1.compareTo(month2);
if ((result = months) != 0) {
return result;
}
// If months are same, sort the names finally
String name1 = m1.getName();
String name2 = m2.getName();
int names = name1.compareTo(name2);
if ((result = names) != 0) {
return result;
}
return result;
}
/*
* With Lambda expressions something like this could
* be also possible. It's just an example. Not tested!
* Comparator<Employee> COMPARE_BY_NAME =
* Comparator.comparing(Membership::getYear)
* .thenComparing(Membership::getMonth)
* .thenComparing(Membership::getName);
*/
}
}