-
The system should support parking for different types of vehicles like car, truck, van, motorcycle, etc.
-
The parking lot should have multiple floors where customers can park their cars.
-
Each parking floor will have many parking spots. The system should support multiple types of parking spots such as Compact, Large, Handicapped, Motorcycle, etc.
-
The parking lot should have multiple entry and exit points.
-
Customers can collect a parking ticket from the entry points and can pay the parking fee at the exit points on their way out.
-
The system should not allow more vehicles than the maximum capacity of the parking lot.
-
The Parking lot should have some parking spots specified for electric cars. These spots should have electric sockets through which customers can charge their vehicles.
public enum VehicleType {
CAR, TRUCK, ELECTRIC, VAN, MOTORBIKE
}
public enum ParkingSpotType {
HANDICAPPED, COMPACT, LARGE, MOTORBIKE, ELECTRIC
}
public abstract class Vehicle {
private String licenseNumber;
private final VehicleType type;
private ParkingTicket ticket;
public Vehicle(VehicleType type) {
this.type = type;
}
public void assignTicket(ParkingTicket ticket) {
this.ticket = ticket;
}
}
public class Car extends Vehicle {
public Car() {
super(VehicleType.CAR);
}
}
public class Van extends Vehicle {
public Van() {
super(VehicleType.VAN);
}
}
public class Truck extends Vehicle {
public Truck() {
super(VehicleType.TRUCK);
}
}
// Similarly we can define classes for Motorcycle and Electric vehicles etc.
public abstract class ParkingSpot {
private String number;
private boolean free;
private final ParkingSpotType type;
private Vehicle vehicle;
public boolean isFree() {
return free;
}
public ParkingSpot(ParkingSpotType type) {
this.type = type;
}
public boolean assignSpot(Vehicle vehicle) {
this.vehicle = vehicle;
free = false;
}
public boolean freeSpot() {
this.vehicle = null;
free = true;
}
}
public class HandicappedSpot extends ParkingSpot {
public HandicappedSpot() {
super(ParkingSpotType.HANDICAPPED);
}
}
public class CompactSpot extends ParkingSpot {
public CompactSpot() {
super(ParkingSpotType.COMPACT);
}
}
public class LargeSpot extends ParkingSpot {
public LargeSpot() {
super(ParkingSpotType.LARGE);
}
}
public class MotorbikeSpot extends ParkingSpot {
public MotorbikeSpot() {
super(ParkingSpotType.MOTORBIKE);
}
}
public class ElectricSpot extends ParkingSpot {
public ElectricSpot() {
super(ParkingSpotType.ELECTRIC);
}
}
public class ParkingFloor {
private String name;
private HashMap<String, HandicappedSpot> handicappedSpots;
private HashMap<String, CompactSpot> compactSpots;
private HashMap<String, LargeSpot> largeSpots;
private HashMap<String, MotorbikeSpot> motorbikeSpots;
private HashMap<String, ElectricSpot> electricSpots;
public ParkingFloor(String name) {
this.name = name;
}
}
public class ParkingLot {
private String name;
private HashMap<String, ParkingFloor> parkingFloors;
// private constructor to restrict for singleton
private ParkingLot() {
}
// singleton ParkingLot to ensure only one object of ParkingLot in the system
private static class ParkingLotHolder {
private static final ParkingLot INSTANCE = new ParkingLot();
}
// static method to get the singleton instance of ParkingLot
public static ParkingLot getInstance() {
return ParkingLotHolder.INSTANCE;
}
}
https://www.educative.io/courses/grokking-the-object-oriented-design-interview/gxM3gRxmr8Z