Use arrays. They can have as many values as you like, so you don't have a limit of 3, although you could enforce it still. And while you're at it, you can use a class as well. Just for the fun of it. ;)
$mrA = new User("GolezTrol");
$mrB = new User("user1715417");
try {
$booking = new Booking();
$booking->bookSeat($mrA, 'A7');
$booking->bookSeat($mrA, 'A8');
// $booking->bookSeat($mrB, 'A8'); // Double booking
$booking->bookSeat($mrB, 'A9');
$booking->bookSeat($mrA, 'B7');
// $booking->bookSeat($mrA, 'B8'); // Only 3 seats allowed per user
$booking->bookSeat($mrB, 'B9');
// $booking->bookSeat($mrB, 'C7'); // No Seat Left
var_dump($booking->bookedSeats); // Output Booking
} catch ( Exception $e ) {
echo $e->getMessage(), PHP_EOL;
}
Output
array
'GolezTrol' =>
array
0 => string 'A7' (length=2)
1 => string 'A8' (length=2)
2 => string 'B7' (length=2)
'user1715417' =>
array
0 => string 'A9' (length=2)
1 => string 'B9' (length=2)
Class Used
class User {
public $id;
function __construct($id) {
$this->id = $id;
}
function __toString() {
return $this->id;
}
}
class Booking {
public $bookedSeats = array();
public $seatUsed = array();
private $maxSeat = 5;
private $maxBooking = 3;
public function bookSeat(User $user, $seat) {
if (count($this->seatUsed) >= $this->maxSeat) {
throw new Exception('No Seat Left');
}
if (in_array($seat, $this->seatUsed)) {
throw new Exception('Double booking');
}
if (array_key_exists($user->id, $this->bookedSeats)) {
if (count($this->bookedSeats[$user->id]) >= $this->maxBooking) {
throw new Exception('Only 3 seats allowed');
}
$this->bookedSeats[$user->id][] = $seat;
$this->seatUsed[] = $seat;
} else {
$this->bookedSeats[$user->id] = array();
$this->bookedSeats[$user->id][] = $seat;
$this->seatUsed[] = $seat;
}
}
}
You can see how easy it would be to put the limit at 5 or 8 seats, without having to modify anything but a number, while your current check is already complex and will become twice as complex if you grow from 3 to 5.