forked from exercism/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeetup.java
More file actions
53 lines (47 loc) · 1.52 KB
/
Meetup.java
File metadata and controls
53 lines (47 loc) · 1.52 KB
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
import java.time.LocalDate;
import java.time.DayOfWeek;
public class Meetup {
private final LocalDate startOfMonth;
public Meetup(int monthOfYear, int year) {
startOfMonth = LocalDate.of(year, monthOfYear, 1);
}
LocalDate day(DayOfWeek dayOfWeek, MeetupSchedule schedule) {
LocalDate current = cycleToNext(dayOfWeek, startOfMonth);
switch (schedule) {
case FIRST:
break;
case SECOND:
current = current.plusWeeks(1);
break;
case THIRD:
current = current.plusWeeks(2);
break;
case FOURTH:
current = current.plusWeeks(3);
break;
case TEENTH:
while (current.getDayOfMonth() < 13) {
current = current.plusWeeks(1);
}
break;
case LAST:
current = cycleToPrev(dayOfWeek, startOfMonth.plusMonths(1).minusDays(1));
break;
default:
return null;
}
return current;
}
private LocalDate cycleToPrev(DayOfWeek dayOfWeek, LocalDate current) {
while (current.getDayOfWeek() != dayOfWeek) {
current = current.minusDays(1);
}
return current;
}
private LocalDate cycleToNext(DayOfWeek dayOfWeek, LocalDate current) {
while (current.getDayOfWeek() != dayOfWeek) {
current = current.plusDays(1);
}
return current;
}
}