Reputation: 2064
I need to store time slots for restaurant table reservation and then see if there is any collision...
For example - Total tables - 4
1) 9 - 11 , 3 tables
2) 9 - 10 , 1 tables (Need to do search if any table left
with constraint to above booking)
How do i store the time slots and tables and compare with others...
what data structure should i use...
If i use HashMap what can be the key and value ,
I have designed all other classes and methods but cant able to find a way to solve time slot collision problem
collision example -
total - 4 tables
1) 9-10 , 3 tables
2) 9-11 , 1 table
3) 9-12 , 2 tables (collision , table not available)
Upvotes: 0
Views: 3399
Reputation: 11
More examples :)
I had the same problem.
I create time slots with interface Slot and using it in the application
As example:
Schedule schedule = new Schedule(myduration, unit);
schedule.add(date1, slot1);
schedule.add(date2, slot2);
schedule.get(date1, slot1);
schedule.indexOf(date1, slot1);
schedule.remove(date1, slot1);
schedule.set(date1, index, slot1);
More details in github https://github.com/illineya/schedule
Upvotes: 1
Reputation: 796
You could simplify the problem, by slicing the available time in blocks of 15 minutes (or whatever other block size is suitable for you). For restaurant reservations I bet that 15 minute blocks are ok.
Then you could have a simple int[] which stores the number of booked tables for each time slot.
Example: Your restaurant opens from 9 a.m. to 9 p.m., so 12 hours with 4 time slots each. So you need an int[] with 48 slots. Now, when you get a reservation for 3 tables 9 to 11 o'clock, then you increase the first 8 slots (means 9 to 11 o'clock) by 3. The second booking would increase the first 4 slots by 1. If a reservation would increase one of your slots over the available table limit, you know that you need to reject it.
final int MAX_TABLES = 4;
final int OPENING= 9;
final int CLOSING= 21;
final int SLOTS= 4;
int[] booking = new int[(CLOSING - OPENING) * SLOTS];
public void main() {
// no tables booked
Arrays.fill(booking,0);
doBooking(3, 0, 8);
doBooking(1, 4, 8);
doBooking(1, 4, 12);
}
public void doBooking(int tables, int startSlot, int endSlot) {
for (int slot= startSlot, slot < endSlot, slot++) {
if (booking[slot] + tables > MAX_TABLES) {
throw new Exception("no free table at slot "+slot);
}
}
for (int slot= startSlot, slot < endSlot, slot++) {
booking[slot] += tables;
}
}
This should give you the idea. There is still stuff to do, e.g. proper exception handling, conversion from time to slots, etc. Note also, that this may be improper java code, as I didn't test it nor did I write it in a GUI.
Upvotes: 3
Reputation: 668
You can create a TimeSlot objects and compare each start and endtimes.
public class TimeSlot {
private int startTime;
private int endTime;
public TimeSlot(int start, int end){
this.startTime = start;
this.endTime = end;
}
}
Upvotes: 0
Reputation: 3031
For time ranges(make it timestamp or 15 min intervals) use Guava Range - it has nice tools to work with ranges (collisions).
Start with open interval from now to future - all tables are available. New reservation -> check whether there is a free table for the whole interval. If there is break original interval to three -> before + this + after
Repeat...
Upvotes: 0