This document explains the logic used in the following code snippet
from the MyCalendar
class:
if (start < date.get(1) && end > date.get(0)) {
return false; // There's an overlap
}
The purpose of this logic is to check whether a new booking overlaps
with an existing booking in the calendar. If the new booking overlaps
with an existing booking, the function will return false
,
indicating that the booking cannot be made.
Imagine a timeline where each event has a start and an end time. The
new event is represented by start
and end
,
while an existing event is represented by date.get(0)
(start) and date.get(1)
(end).
New: |-----|
Existing: |-----|
end <= date.get(0)
New: |-----|
Existing: |-----|
start >= date.get(1)
New: |-------|
Existing: |------|
start < date.get(1)
and
end > date.get(0)
New: |---|
Existing: |-------|
start < date.get(1)
and
end > date.get(0)
New: |-----------|
Existing: |-----|
start < date.get(1)
and
end > date.get(0)
start < date.get(1)
: This checks if
the new event starts before the existing event ends.end > date.get(0)
: This checks if
the new event ends after the existing event starts.If both conditions are true, there is an overlap,
and the function returns false
to indicate that the booking
cannot be made.