This class is a simple enum class for different vehicle size.
public enum VehicleSize { Motorcycle, Large, Compact}
Abstract class that coulbe extended for different vehicles in the parking lot.
public abstract class Vehicle {
List<ParkingSpot> parkingSpots = new ArrayList<>();
String licensePlate;
VehicleSize sizeOfVehicle;
int spotsRequired;
// park the vehicle in a paricular spot.
public void parkInSpot(ParkingSpot parkingSpot) { parkingSpots.add(parkingSpot); }
//clear the spot
public void removeSpot() { ... }
// can fit in a spot ?
public boolean canFitInParkingSpot(ParkingSpot parkingSpot);
}
Extends the vehicle abstract class.
public Motorcycle extends Vehicle {
Motorcycle(){
spotsRequired=1;
sizeOfVehicle = VehicleSize.MotorCycle;
}
// can a motorcycle fit in the slot
public boolean canFitInParkingSpot(ParkingSpot parkingSpot) { ... }
}
Extends the vehicle abstract class.
public Car extends Vehicle {
Car(){
spotsRequired=1;
sizeOfVehicle = VehicleSize.Compact;
}
// can a car fit in the slot
public boolean canFitInParkingSpot(ParkingSpot parkingSpot) { ... }
}
Extends the vehicle abstract class.
public Bus extends Vehicle {
Bus(){
spotsRequired=5;
sizeOfVehicle = VehicleSize.Large;
}
// can a bus fit in the slot
public boolean canFitInParkingSpot(ParkingSpot parkingSpot) { ... }
}
The ParkingSlot is used to keep a track of all slot information.
class ParkingSpot {
private Level level;
private int row;
private int spot;
VehicleSize vehicleSize;
private Vehicle vehicle;
ParkingSpot(Level level, int spot, int row, Vehicle vehicle){}
//getters and some util methods.
}
ParkingLot is the class that is where the main control lies or where the client may call this class to park the vehicle.
class ParkingLot {
//total levels
private final int NUM_LEVELS = 10;
private Level[] levels;
public void park(Vehicle vehicle) {
Steps
1. Iterate each level
2. at each level if there is a free ParkingSlot check if the vehicle fits
3. If yes park else keep iterating and keep moving up level
4. If parking lot is full throw an error saying parking lot full
}
}
Storing all the level information required by the parking lot.
class Level {
//Remove a vehicle from a spot
public void freeSpot() {...}
//Park a vehicle starting at the spot spotNumber
public boolean parkStartingAtSpot(int spotNumber, Vehicle v) { ... }
// Find a spot to park the vehicle. if not possible return -1.
public int findAvaialableSpot(Vehicle vehicle) { ... }
}