Friday, March 2, 2007

The code made me feel proud of my logical skill

Being a software engineer, I have written many codes. There is nothing special in that. Even when I came up with a solution for some tricky problems, I felt happy. Also such things gave me satisfaction in my work life....

But this one stands first among all those gems.

Just to let you understand the senario, for which I came up with the solution, I will explain that first.

This is regarding a small application, that can be used for making reservations. For example Room booking systems, Car booking systems Etc.
The requirement was to avoid the duplicate bookings. Users shouldn't be allowed to book same room for the same time frame. means once if a room or car is booked for some time, it should not be allowed for others to book that same room/car for the same duration. And overnight booking was allowed.

Booking form was as below.




Try to find solution outline for this.
----------------------------------------------------------------------------------
After struggling a quite, finally I came up with a decent solution.....
And that gave me the most happiness ever I experienced in my 2 years software work life.
----------------------------------------------------------------------------------
Normally for this type of situations, if reservations are restricted to only one day , solution would be as below. Will be quite simple.

1. Search for the existing reservations, with the same car name/number or room name/number. (Some thing like unique key).
2. Get the collection of all those reservations from above list, which matches our reservation date.
3. Check weather any of them have reservation time, which may deny us booking the room we want.
4. If yes then prompt a message like room is not allowed.
5. Else allow booking.

But now in our case over night reservations were allowed, where reservation may extend to 2 - 3 days. This made the problem bit tricky, as it is not that easy to follow the solution explained above.

Ex: If a resrvation already exists for 18/01/2007 then our booking is from 16/01/2007 to 19/01/2007 for the same room, it is not that easy to check weather reservation is not possible for us or not.

If we keep on comparing dates and times separately, we will end up with more than 12 if conditions, that too may not work properly for all senarios. After finding this fact I started thinking some alternative way, where I can simplify whole thing in much efficient simpler way. That leades to the master piece of code, I ever written in my work life....
I decided to combine date and time as one entity, as follows.

'Set the new request start date and end date time
Set StartDateTime =New notesdatetime( docRequest.DepartureDateDT(0) & " " & docRequest.DepartureTimeDT(0) )
Set EndDateTime = New notesdatetime( docRequest.ReturnDateDT(0) & " " & docRequest.ReturnTimeDT(0) )


This made my life much easier as it simplified the things.

'Get the collection of reservations that may be in conflict with the new reservation I am going to create now.
Set colDuplicateCandidates = LookupView.GetAllDocumentsByKey( key, False)

Logic was

Suppose, x's reservation is picked as a duplicate candidate then

if My start date is greater than x's end date then directly allow
else if My end date is less than x's start date then allow
else
don't allow

Actual code goes here..
************************************************************************
'checking whether the selecteted cars are vacant or reserved.
If colDuplicateCandidates.count > 0 Then

Set docDuplicateRequest = colDuplicateCandidates.GetFirstDocument
While Not docDuplicateRequest Is Nothing

' make sure we are not comparing to our own document. (While editting existing documents this case will arise).
If (Strcompare(docRequest.NoteID, docDuplicateRequest .NoteID) <> 0) Then

'Set the start date and end date time of the duplicate candidate
Set DupStartDateTime = New notesdatetime( docDuplicateRequest .DeptDateDT(0) & " " & docDuplicateRequest .DeptTimeDT(0) )
Set DupEndDateTime = New notesdatetime( docDuplicateRequest .ReturnDateDT(0) & " " & docDuplicateRequest .ReturnTimeDT(0) )

'Directly skip the duplicate candidate as end time is less than our start time. Else go on and chck other conditions
If StartDateTime.TimeDifference(DupEndDateTime) < 0 Then
If DupStartDateTime.TimeDifference(EndDateTime) < 0 Then

strAvail="False"

'Exit Sub 'Don't waste time by checking other documents. any way it is not available
Goto Result
End If
End If
End If
Set docDuplicateRequest = colDuplicateCandidates.GetNextDocument(docDuplicateRequest)
Wend
End If
********************************************************************************