A small attended car park uses a class ParkingLot to manage its operation. The lot has a fixed total number of spaces, set when the lot opens for the day. The class must support recording cars entering and leaving the lot, reporting capacity information, and reporting how many cars have been admitted across the entire shift.
A ParkingLot object keeps track of three pieces of information:
- the total number of spaces in the lot, set at construction and never changed afterward,
- the current number of cars parked in the lot,
- the total number of cars that have ever been admitted into the lot during the shift (a car that enters and later leaves still counts here).
Write the complete ParkingLot class. Your class must include the instance variables described above and the behaviors specified below. All instance variables must be private.
Constructor
public ParkingLot(int spaces) — Constructs a new lot with the given number of total spaces. The number of cars currently parked and the total ever admitted are both 0. You may assume spaces > 0.
Method: enter
public boolean enter() — Attempts to admit one car. If the lot is full (the current count equals the total number of spaces), no instance variable changes and the method returns false. Otherwise the current count increases by 1, the total ever admitted increases by 1, and the method returns true.
Method: leave
public boolean leave() — Attempts to release one car. If the lot is empty (the current count is 0), no instance variable changes and the method returns false. Otherwise the current count decreases by 1 and the method returns true. The total ever admitted is never decreased by leave.
Method: available
public int available() — Returns the number of spaces still free in the lot, computed from the total number of spaces and the current count.
Method: served
public int served() — Returns the total number of cars that have been admitted during the shift.
Method: occupancy
public double occupancy() — Returns the fraction of the lot currently occupied, expressed as a value in the range 0.0 to 1.0. The fraction is computed as the current number of cars divided by the total number of spaces. Because spaces > 0 by construction, no division-by-zero check is required.
The table below shows a possible sequence of calls on a newly constructed ParkingLot named lot. Use this sequence to guide the design and behavior of your class.
| Statements and Expressions |
Value Returned (blank if no value) |
Comment |
ParkingLot lot = new ParkingLot(3); |
|
Creates a new lot with 3 spaces, all empty, no one served yet. |
lot.available() |
3 |
All spaces are free. |
lot.enter() |
true |
1 car parked, served count becomes 1. |
lot.enter() |
true |
2 cars parked, served count becomes 2. |
lot.enter() |
true |
3 cars parked, served count becomes 3; lot is now full. |
lot.enter() |
false |
Lot is full; rejection does not affect any field. |
lot.available() |
0 |
No spaces remain. |
lot.occupancy() |
1.0 |
Three of three spaces are occupied. |
lot.leave() |
true |
2 cars parked; served count is unchanged. |
lot.leave() |
true |
1 car parked. |
lot.leave() |
true |
0 cars parked; lot is now empty. |
lot.leave() |
false |
Lot is already empty; rejection does not affect any field. |
lot.served() |
3 |
Total ever admitted across the shift is preserved. |
lot.occupancy() |
0.0 |
Zero of three spaces are occupied. |
Write the complete ParkingLot class, including instance variables, constructor, and all methods described above.