0

I've run into a bit of a tricky issue. It'll probably be easy for most of you but I'm just not seeing it.

There are 3 parking spaces available: A, B, and C. All 3 can be booked at the same time, but no more than those 3 bookings can happen at once or we'll run out of spaces!

My current problem can be viewed in my masterpiece of a drawing here: https://i.stack.imgur.com/x31fG.png (This represents the scheduler object I'm using, with times labelled on the left).

That represents a scheduler control with "bookings" for parking spaces. Here is some pseudocode of what I'm currently doing:

function GetFreeSpace(Reservation Res)
    boolean intersects;

    for each Reservation R in ListOfReservationsInSpaceA
        if (R and Reservation are not the same)
            if (Res and R intersect)
                intersects = true;
                exit for;
            end if
        end if
    next

    if (intersects = false)
        ListOfReservationsInSpaceA.Add(Res);
        Return "A"
    end if

    intersects = false

    // do the same for spots B and C

    // if all spots are taken, return "none"
end function

But with my code, if Booking 1 is between 8AM and 5PM and takes spot A, Booking 2 takes spot B between 10AM and 8PM, and booking 3 takes spot C between 6PM and 10PM (once booking 1 is done), a booking 4 that SHOULD work between 8AM and 10PM cannot be made because all spots are taken.

I hope this was relatively clear.

Thanks for any help! The language I'm using is VB.NET but I'd be able to translate from C, C++, C#, Java, etc.

Dan
  • 1,163
  • 3
  • 14
  • 28
  • At the moment I'm thinking that when it detects an intersection with a booking in space C, it'll try and re-evaluate the space for that booking and see if it can be re-assigned. This is pretty hacky though and will be vastly inefficient and will take a lot of work, so I'd rather avoid that solution. – Dan Mar 04 '16 at 06:45
  • 1
    1. It would be helpful to see the real code not just pseudo-code. Especially `if (Res and R intersect)` 2. I guess the problem is that you check each space (A,B,C) seperately. The intersection check have to be amongst all of them 3. There are some similiar QA on SO, e.g. [here](http://stackoverflow.com/a/7325171/2882256). It´s not an exact duplicate but it should also work for 3 time ranges. – Alex B. Mar 04 '16 at 07:40
  • You should include the booking date to make sure that there will be no overlapping schedules – F0r3v3r-A-N00b Mar 04 '16 at 08:19
  • So you're saying that the rejected reservation (from 8 AM to 10 PM) should be accepted because spot C is available between 8 AM and 6 PM, and spot A is available from 6 PM to 10 PM? Nice idea, but somebody's car will have to move. You'll have to change your logic quite a bit to accommodate that. You could have a situation where a car has to be moved every hour. Or whatever is your minimum granularity. – Jim Mischel Mar 04 '16 at 21:33
  • @JimMischel Fortunately for me, the spaces are only logical. When the concierge makes the bookings they send the visitor to any space that's available regardless of what the software says. My solution is below. – Dan Mar 05 '16 at 04:43

1 Answers1

0

So I found out that my original algorithm works 100% of the time, until a reservation is cancelled in which case the parking spaces would not be refreshed. I'm mostly sure that the issue I'm facing here only happens during booking cancellation, so I've added a "refresh" function to the cancellation code. Things seem to be working. I'll have to get my testers to determine that for sure.

Anyway, thanks for the help everyone.

Dan
  • 1,163
  • 3
  • 14
  • 28