0

I have an app to write in Java to find the available booking slot in a day. what I have a is a json file which contain the list of already booked slot and the open/close time of the booking.

For example (just assume the numbers below are given as exampe),

{
    "bookingopen" : 112,
    "bookingclose" : 213,
    "list_of_booking" : {
          "start" : "114",
          "end" : "119"
        },
        {
          "start" : "136",
          "end" : "180",
        },
        {
          "start" : "140",
          "end" : "156"
        }
    }
}

what I want to get is the remaining spot:

(112..114), (119..136) and (180..213)

I have tried using

IntStream but it's not working.

I have also wrote a set of if condition:

for(int i = 1; i < mListOfBooking.size()-1; i++){

            if(mListOfBooking.get(i-1).getArriveTime() > venue.getOpenTime() && (i-1 == 0)) {
                mNewList.add(new Person(0, "No visitors", venue.getOpenTime(), mListOfBooking.get(i - 1).getArriveTime()));
            } else if (mListOfBooking.get(i-1).getLeaveTime() < mListOfBooking.get(i).getArriveTime()){
                mNewList.add(new Person(0, "No visitors", mListOfBooking.get(i-1).getLeaveTime(), mListOfBooking.get(i).getArriveTime()));
            } else if((i == mListOfBooking.size()-1) && (mListOfBooking.get(i).getLeaveTime() < venue.getCloseTime())){
                mNewList.add(new Person(0, "No visitors", mListOfBooking.get(i).getLeaveTime(), venue.getCloseTime()));
            }
                mNewList.add(new Person(mListOfBooking.get(i - 1).getId(),
                        mListOfBooking.get(i - 1).getArriveTime(),
                        mListOfBooking.get(i - 1).getLeaveTime()));

}

method getclose, getopen, getLeaveTime, getArrive a just method to extarct information from the JSON

this logic is not good and probably not "smart" enough because it misses some cases and I can't have an if in any edge case.

Any idea on a good algorithm to extract the available slot

Seb
  • 2,929
  • 4
  • 30
  • 73
  • Are you working with dates & times? If so, important to note as there are specific libraries for handling date-time values. – Basil Bourque Dec 02 '17 at 16:29
  • @BasilBourque it's timestamp which means just consider it as "Long". I handling the human-readable state in another lib. – Seb Dec 02 '17 at 16:41
  • Well, if you *don’t* consider it as a "Long", and instead consider it as a date-time object, you would have the [*java.time*](https://docs.oracle.com/javase/9/docs/api/java/time/package-summary.html) and [*ThreeTen-Extra*](http://www.threeten.org/threeten-extra/) classes at your disposal, including `Period`, `Duration`, `Interval`, and `LocalDateRange`. As-is, your Question is too vague to fully assist you. – Basil Bourque Dec 02 '17 at 20:48

1 Answers1

0

This is a interval arithmetic task.

You can check on Ranges, from the Guava libraries. As suggested on this post:

Date interval sum and subtraction in Java

Or use some more specific library like interval.sourceforge.net

Aleh Maksimovich
  • 2,622
  • 8
  • 19