我需要存储餐厅订位的时间段,然后看看有没有碰撞...
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)
我如何存储时间段和表格并与其他人进行比较...
我应该使用什么数据结构...
如果我使用 HashMap 什么可以是键和值,
我已经设计了所有其他类和方法,但无法找到解决时隙冲突问题的方法
collision example -
total - 4 tables
1) 9-10 , 3 tables
2) 9-11 , 1 table
3) 9-12 , 2 tables (collision , table not available)
最佳答案
您可以通过将可用时间分成 15 分钟的 block (或适合您的任何其他 block 大小)来简化问题。对于餐厅预订,我敢打赌 15 分钟的时间段没问题。
然后你可以有一个简单的 int[] 来存储每个时间段的预订表数。
例子: 您的餐厅从上午 9 点营业到晚上 9 点,因此营业时间为 12 小时,每个时段有 4 个时段。所以你需要一个有 48 个槽的 int[]。现在,当您预订 9 点到 11 点的 3 个 table 时,您将前 8 个空位(即 9 点到 11 点)增加 3。第二次预订会将前 4 个空位增加 1。如果预订会使您的其中一个空位增加到超过可用赌 table 限制,您知道您需要拒绝它。
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;
}
}
这应该会给您灵感。还有事情要做,例如正确的异常处理、从时间到槽的转换等。另请注意,这可能是不正确的 Java 代码,因为我没有测试它,也没有在 GUI 中编写它。
关于java - 表示和比较时隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27046368/