Requirements
1) User should have a vehicle which when enters the parking lot will be assigned a parking spot appropriate for the VEhicle Type. For example - Car will have a Compact parking spot, Bike will have a Bike parking spot, Van will have a Large parking Spot.
2) User will recieve a ticket.
3) User will exit the parking spot after receiving and paying for the invoice
Define Core Objects
ParkingLot
ParkingSpot
ParkingSpotType
Vehicle(ABSTRACT)
VehicleType
ParkingFloor
Ticket
Invoice
Analyze Relationships
Determine how these objects will interact with each other to fulfill the use cases...
Vehicle will have a VehicleType
ParkingSpot Will Hava A ParkingSpotType
PrakingSpot Will Have A parked Vehicle
ParkingFloor Will Have ParkingSpots
Establish Hierarchy
Design inheritance trees where applicable to promote code reuse and polymorphism. This step involves identifying common attributes and behaviors that can be abstracted into parent classes...
Car is a Vehicle
Bike is a Vehicle
Van Is A Vehicle
Design Patterns
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
For fee calcultion I will use strategy pattern
To take out appropriate Fee Strategy I will use Fatory Pattern
ParkingLot Can Be a Singleton class
Define Class Members (write code)
Attributes: For each class, define the attributes (data) it will hold...
Methods: Define the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation.
public class TypeFactory {
private static Map<VehicleType, SpotType> typeMap;
public TypeFactory() {
typeMap.put(VehicleType.CAR, SpotType.COMPACT);
typeMap.put(VehicleType.VAN, SpotType.LARGe);
typeMap.put(VehicleType.BIKE, SpotType.BIKE);
}
public SpotType getSpotType(VehicleType vehicleType)
}
public abstract class Vehicle {
UUID id;
VehicleType vehicleType;
String licensePlate;
public Vehicle(VehicleType vehicleType, String licensePlate) {
this.vehicleType = vehicleType;
this.licensePlate = licensePlate;
}
public SpotType getSpotType() {
RETURN TypeFactory.getSpotType(vehicleTYPE);
}
getVehicleType():VehicleType
}
public class ParkingSpot {
UUID id;
SpotType;
parkedVehicle;
parkVehicle(Vehicle vehicle) {
parkedVehicle = vehicle;
}
unParkVehicle() {
parkedVehicle = null;
}
isAvailable() {
return parkedVehicle == null;
}
getSpotType() : SpotType
}
public class ParkingFloor {
UUID id;
floorNumber;
Map<VehicleType, List<ParkingSpot>> spotList;
public void parkVehicle(Vehicle vehicle) {
for(var entry: spotList.entrySet()) {
for(PAkringSpot spot : entry.getValu()) {
if(spot.isAvailable() && vehicle.getSpotType().equals(spot.getSpotType())) {
spot.parkVehicle(vehicle);
return;
}
}
}
throw new IllegalArgumentException("No Spot found for the given Vehicle);
}
}
public class ParkingLot {
UUID id;
List<ParkingFloor> floors;
parkkingLot
private ParkingLot() {
this.floor = new ArrayList<>();
}
public static ParkingLot getInstance() {
if(parkingLot == null) {
parkingLot = new ParkingLot();
}
return parkingLot;
}
public void addFloor(ParkingFloor floor) {
floors.add(floor);
}
public synchronized void parkVehicle(Vehicle vehicle) {
for(Floor floor : parkingFloors) {
}
}
}
Adhere to SOLID Guidelines
Check and explain whether your design adheres to solid principles (Ask interviewer what SOLID principle is if you can not recall it.)...
Consider Scalability and Flexibility
Explain how your design can handle changes in scale and whether it would be easily to extend with new functionalities...
Create/Explain your diagram(s)
Try creating a class, flow, state and/or sequence diagram using the diagramming tool. Mermaid flow diagrams can be used to represent system use cases. You can ask the interviewer bot to create a starter diagram if unfamiliar with the tool. Briefly explain your diagrams if necessary...
Future improvements
Critically examine your design for any flaws or areas for future improvement...