How can I access struct fields

Hello Everyone! I am new to Julia and I am trying to create a function that checks if it is possible to insert a request in between 2 reservations. First I want to check the (Time) interval between the reservations, and if it is less than the length of the request, only then i can insert. I have the following;

using Dates

mutable struct Request
arrival::DateTime
departure::DateTime
numGuest::Number
end

mutable struct Reservation
checkIn::DateTime
checkOut::DateTime
extras::Number
end

function insertRequestInBetweenReservation(reservation1::Reservation, reservation2::Reservation, request::Request)
reservation_interval = Time(reservation2.checkIn - reservation1.checkOut).value
request_duration = Time(request.departure - request.arrival).value #get the duration of the request
if(request_duration > booking_interval) #check if the duration for the request can fit into the interval between the Reservations
return nothing
else
#some code
end
return reservation_interval
end

Is it possible to use reservations::Vector{Reservation} instead of reservation1 and 2? if so, how can I check the time interval in this way? I am thinking of something of the following…

function insertRequestInBetweenReservation(request::Request, reservations::Vector{Reservation})
left_hand_booking = 1 #the first reservation
right_hand_booking = left_hand_booking +1 #the next reservation and I want to check if it is possible to insert in between
reservation_interval = reservations[left_hand_booking]
end

Hi. A few things are unclear. What is an example of a request, a dinner reservation? What is an example of a reservation, a room reservation?
Why do you need to check the time interval between reservations, as a reservation has its own span check_out - check_in – probably I am misinterpreting some of your intent. It would be helpful if you were to clarify what is intended with words rather than code.

Something like this

function insertRequestInBetweenReservation(request::Request, reservations::Vector{Reservation}_
  left_hand_booking = 1 #the first reservation
  right_hand_booking = left_hand_booking + 1
  reservation_interval = Time(reservations[right_hand_booking].checkIn - reservations[left_hand_booking].checkOut).value
  request_duration = Time(request.departure - request.arrival).value #get the duration of the request
  if(request_duration > reservation_interval) #check if the duration for the request can fit into the interval between the Reservations
     nothing
  else
     insert!(reservations,left_hand_booking + 1, newreservation)
  end
  return reservation_interval
end

By the way, if you wrap your code between triple backticks it will be formatted nicely!

2 Likes

Sounds like a hotel reservation. So you can only fit a reservation between to preexisting ones if the duration of the new one fits between the checkout of the first reservation and the checkin of the next one, no?

I have never asked to reserve a second hotel room in the hotel where I stayed while staying there. I do not follow the intent. Part of the difficulty is that the request struct and the reservation struct seem much the same. Why both then?
@ali1 we are looking to be helpful … please provide a brief description of what you want to accommodate.

Not the original poster but if you are a hotel manager and someone asks for a reservation for Thursday, you will have to check your rooms. If you have a room that is reserved for Wednesday and Friday, then you can offer that one.

so this is a database query and insert sort of thing? just without the database.

@aramirezreyes Thank you for walking me through. I follow the intent.

Thanks, this helped a lot

The reservation struct holds a list of requests that were accepted. New users can send a request, and that is what the request struct is for (like @aramirezreyes explained, thanks). I understand it is much easier to check for overlapping (i.e if the request overlaps with existing reservation). But I have in mind a plan this scenario to EV car-sharing in which apart from the overlapping, I also have to check if the SoC (state of charge) will enough for the 3 reservation (left_hand_booking, new request that is inserted and right_hand_booking) without needing to recharge.

Have you worked with a relational database?
This is the sort of application which will tend to overexercise simple datastructures as the number of cars, drivers, passengers, passengers requesting pickup increase (let alone the calendar interdependencies).

We have support for the more common databases at JuliaDatabases. SQLite is reasonably nonthreatening – however jumping into relational tables if you have not any prior familiarity may be simple or may be not simple (as works for you, or not).

Otherwise, take a long look and study DataFrames.jl. You have many people on Discourse who would help you with questions as they arise, and there are tutorials here is one.

Thank you.