Skip to content

Commit

Permalink
Fix bug in Restaurant Table Delegator. Was not using table index
Browse files Browse the repository at this point in the history
  • Loading branch information
bkuberek committed Aug 5, 2024
1 parent 540d6ce commit 8134c61
Showing 1 changed file with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class ReservationTableDelegator {

// Keep track of the last table.
// Assuming our query is right, the last item should be the largest table.
var lastSeen: RestaurantTableEntity? = null
for (table in availableTables) {
// Skip if the table supply is depleted
if (table.quantity == 0) {
Expand Down Expand Up @@ -76,6 +77,7 @@ class ReservationTableDelegator {
// We found our table, no need to look any further
break
}
lastSeen = table
}

// We've seen the whole list, and we didn't find a table.
Expand All @@ -87,6 +89,21 @@ class ReservationTableDelegator {
)
throw IllegalArgumentException("Not enough seats")
}

// If we do have enough capacity but the tables are not large enough, we split the group
if (standing > 0 && lastSeen != null && lastSeen.quantity > 0) {
// seat part of the group at the last largest table
val reservedTable = reservedTables[lastSeen.size]
if (reservedTable == null) {
val newTable = reservationTableEntity(randomUUID, lastSeen)
reservedTables[lastSeen.size] = newTable
reservation.tables.add(newTable)
} else {
reservedTable.quantity += 1
}
standing -= lastSeen.size
lastSeen.quantity -= 1
}
}

// This should not have happened since we throw an exception above, but just in case,
Expand Down

0 comments on commit 8134c61

Please sign in to comment.