How can PHP developers ensure that the table reservation system accurately assigns tables based on the number of guests without overcomplicating the logic with algorithms like knapsack?
To ensure that the table reservation system accurately assigns tables based on the number of guests without overcomplicating the logic with algorithms like knapsack, PHP developers can simply iterate through the available tables and assign the guests to the first table that can accommodate them. This straightforward approach minimizes complexity while still effectively assigning tables based on guest count.
function assignTable($guestCount, $tables) {
foreach ($tables as $table) {
if ($table['capacity'] >= $guestCount) {
return $table['tableNumber'];
}
}
return "No available tables for $guestCount guests.";
}
// Example usage
$tables = [
['tableNumber' => 1, 'capacity' => 4],
['tableNumber' => 2, 'capacity' => 6],
['tableNumber' => 3, 'capacity' => 8]
];
$guestCount = 5;
$tableNumber = assignTable($guestCount, $tables);
echo "Table $tableNumber has been assigned for $guestCount guests.";