Create a class CapacityManager
to manage restaurant dining capacity.
You should provide a public constructor that allows the capacity of the restaurant to be set as an int
when the
CapacityManager
is created.
You don't need to provide either a getter or a setter for the capacity, which will not change.
Provide two methods: enter
and leave
.
Each accepts a count of guests (as an int
) that are either arriving (enter
) or departing (leave
) from the
restaurant.
Both should return the number of guests after the operation completes as an int
.
If the count is negative or zero, both methods should throw an IllegalArgumentException
.
enter
should also throw a IllegalStateException
if the new party would exceed the capacity of the restaurant.
This restaurant also refuses to admit parties larger than 8, and if one arrives enter
should throw a
PartyTooLarge
exception, which you can create without importing:
Note that this is a checked exception!
leave
should also throw an IllegalStateException
if a party leaves that is larger than 8, or if the leave
would cause the number of diners to fall below zero, since both represent mistakes in our enter
code.
In both enter
and leave
, if an exception is thrown, the number of diners should not be changed.
When you are finished, here is how your class should work:
Do not use assert
to solve this problem!
Now that we know how to throw
exceptions, we'll utilize that ability in many of the places that we used assert
previously.
You're challenge is to write tests for this problem described above.
Stuck? You may find these lessons helpful: