-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppointment.java
More file actions
77 lines (64 loc) · 2.38 KB
/
Copy pathAppointment.java
File metadata and controls
77 lines (64 loc) · 2.38 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import java.time.LocalDate;
public abstract class Appointment implements Comparable<Appointment> {
private LocalDate startDate;
private LocalDate endDate;
private String description;
public Appointment(LocalDate startDate, LocalDate endDate, String description) {
LocalDate today = LocalDate.now();
if (startDate.isBefore(today)) {
throw new IllegalArgumentException("Start date must be today or later.");
}
if (startDate.isAfter(endDate)) {
throw new IllegalArgumentException("Start date must be earlier than or equal to end date.");
}
this.startDate = startDate;
this.endDate = endDate;
this.description = description;
}
// helpful way to determine whether a date falls between startDate and endDate
protected boolean inBetween(LocalDate date) {
return (date.isEqual(startDate) || date.isEqual(endDate) ||
(date.isAfter(startDate) && date.isBefore(endDate)));
}
// implementing an abstract method for subclasses
public abstract boolean occursOn(LocalDate date);
// implementation of the compareTo function for sorting
@Override
public int compareTo(Appointment other) {
int result = this.startDate.compareTo(other.startDate);
if (result == 0) {
result = this.endDate.compareTo(other.endDate);
if (result == 0) {
result = this.description.compareTo(other.description);
}
}
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !(obj instanceof Appointment)) {
return false;
}
Appointment other = (Appointment) obj;
boolean sameStartDate = this.startDate.equals(other.startDate);
boolean sameEndDate = this.endDate.equals(other.endDate);
boolean sameDescription = this.description.equals(other.description);
return sameStartDate && sameEndDate && sameDescription;
}
@Override
public String toString() {
return description + " (" + startDate + " to " + endDate + ")";
}
public LocalDate getStartDate() {
return startDate;
}
public LocalDate getEndDate() {
return endDate;
}
public String getDescription() {
return description;
}
}