The contract of a.compareTo(b)
is to return a value greater than 0 if a
is "larger" than b
, a value smaller than 0 if it's "smaller" or 0 if they are equal. Using 1 and -1 is purely an implementation detail, and you should not rely on it. I.e., your should change your code to:
public boolean isBookable(Booking wishedBooking) {
boolean bookable = false;
if ((wishedBooking.begin.compareTo(begin) <= 0
&& wishedBooking.end.compareTo(begin) <= 0)
|| (wishedBooking.begin.compareTo(end) >= 0
&& wishedBooking.end.compareTo(end) >= 0)) {
bookable = true;
}
return bookable;
}
Note that since you're only returning a boolean
, the if
can be removed, and you can just return its condition:
public boolean isBookable(Booking wishedBooking) {
return ((wishedBooking.begin.compareTo(begin) <= 0
&& wishedBooking.end.compareTo(begin) <= 0)
|| (wishedBooking.begin.compareTo(end) >= 0
&& wishedBooking.end.compareTo(end) >= 0));
}