As a developer, we sometimes need to write code for checking overlapping. We check overlapping for two objects having one start and end point (Overlapping between two appointments, overlapping between two courses, programs etc).
For example lets take a Course as object, So Course has a Start Date and End Date.
Course (StartDate, EndDate).
Now we have given two courses, “courseOne” and “courseTwo”, now we need to check whether they are overlapping to each other or not?
We have For courseOne
- courseOne.startDate
- courseOne.endDate
For courseTwo
- courseTwo.startDate
- courseTwo.endDate
Analysis of Possible Overlapping
Let’s start some analysis on possible overlapping between courseOne and courseTwo, there are four possible use cases when courseOne and courseTwo can overlap each other.
1.) Complete Overlap : When courseOne completely covers the courseTwo i.e. courseOne starts before courseTwo and ends after courseTwo. So in coding we can write this condition as.
1 |
boolean isCompleteOverlap = courseOne.startDate <= courseTwo.startDate && courseOne.endDate >= courseTwo.endDate; |
2.) Under Overlap : When courseTwo completely covers the courseOne i.e. courseTwo starts before courseOne and ends after courseOne. We can simply say its vice-versa of complete overlap. In coding we can write this as.
1 |
boolean isUnderOverLap = courseOne.startDate >= courseTwo.startDate && courseOne.endDate <= courseTwo.endDate; |
3.) End Overlap : When ending of courseOne overlap with starting of courseTwo i.e. Before end of courseOne, courseTwo starts. We can write this check in code as.
1 |
boolean isEndOverLap = courseOne.startDate <= courseTwo.startDate && courseOne.endDate >= courseTwo.startDate; |
4.) Start Overlap : When starting of courseOne is overlapped with ending of courseTwo i.e. courseOne starts before courseTwo ends. In coding we can write this conditions as.
1 |
boolean isStartOverlap = courseOne.startDate <= courseTwo.endDate && courseOne.endDate >= courseTwo.endDate; |
Pseudo Code
Finally you can write a complete method to check overlapping between two object as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public boolean isOvelapping(Course courseOne, Course courseTwo){ boolean isOverLapping = false; // Checking overlapping boolean isCompleteOverlap = courseOne.startDate <= courseTwo.startDate && courseOne.endDate >= courseTwo.endDate; boolean isUnderOverLap = courseOne.startDate >= courseTwo.startDate && courseOne.endDate <= courseTwo.endDate; boolean isStartOverlap = courseOne.startDate <= courseTwo.endDate && courseOne.endDate >= courseTwo.endDate; boolean isEndOverLap = courseOne.startDate <= courseTwo.startDate && courseOne.endDate >= courseTwo.startDate; // If any overlapping use case is true , method will return true. isOverLapping = isUnderOverLap || isCompleteOverlap || isStartOverlap || isEndOverLap; // Above code is two make it more readable and understandable, to optimize this use all of them in one If condition. i.e. /*if((courseOne.startDate <= courseTwo.startDate && courseOne.endDate >= courseTwo.endDate) || (courseOne.startDate >= courseTwo.startDate && courseOne.endDate <= courseTwo.endDate) || (courseOne.startDate <= courseTwo.endDate && courseOne.endDate >= courseTwo.endDate) || (courseOne.startDate <= courseTwo.startDate && courseOne.endDate >= courseTwo.startDate)){ isOverLapping = true; }*/ return isOverLapping; } |
This code can be changed as per your requirement, just grab this pseudo code and be a faster/smarter coder.
Happy Coding 🙂
This really answered my problem, thank you!