My Solution for Design a Parking Lot with Score: 5/10
by cosmos3181
Requirements
- Check the size of the vehicle and find out if it is available. Notify user if not available.
- Calculate rates based on time parked. Allow for a set maximum rate, which is constant after 24 hours of parking time
- Vehicle size can be selected from SMALL, MIDIUM, and LARGE
- You can check availability by floor.
Define Core Objects
Vehicle: Represents a vehicle entering or leaving the parking lot.
Parking Spot: Base class for different types of spots
Parking Lot: Contains parking lots.
Analyze Relationships
Establish Hierarchy
Design Patterns
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
Define Class Members (write code)
class Vehicle {
String licensePlate;
String size;
LocalDateTime parkedTime;
double calculateFee(double dailyMax, double hourlyRate){
if(parkedTime == null){
return 0.0;
}
Duration sabun = Duration.between(parkedTime, LocalDateTime.now());
long hours = sabun.toHours();
if(hours >= 24){
return dailyMax;
}else{
return hours * hourlyRate;
}
}
}
class ParkingSpot{
String size;
boolean isAvailable = true;
Vehicle vehicle;
void assignVehicle(Vehicle vehicle){
this.vehicle = vehicle;
this.isAvailable = false;
}
void removeVehicle(){
this.vehicle = null;
this.isAvailable = true;
}
}
class ParkingLot{
//List<ParkingSpot> parkingSpots;
Map<Integer, List<ParkingSpot>> parkingSpots;
Map<String, Double> hourlyFee;
double dailyMax;
boolean checkAvailability(String size, int floorNumber){
for(Map.Entry<Integer, List<ParkingSpot>> parkingSpot:parkingSpots.entrySet()){
if(floorNumber == parkingSpot.getKey()){
List<ParkingSpot> spots = parkingSpot.getValue();
for(ParkingSpot spot:spots){
if(spot.isAvailable == spot.size.equals(size)){
return true;
}
}
}
}
return false;
}
ParkingSpot assignSpot(Vehicle vehicle, int floorNumber){
if(checkAvailability(vehicle.size, floorNumber)){
for(Map.Entry<Integer, List<ParkingSpot>> parkingSpot:parkingSpots.entrySet()){
if(floorNumber == parkingSpot.getKey()){
List<ParkingSpot> spots = parkingSpot.getValue();
for(ParkingSpot spot:spots){
spot.assignVehicle(vehicle);
spot.isAvailable = false;
return spot;
}
}
}
}
return null;
}
double calculateFee(Vehicle vehicle){
return vehicle.calculateFee(dailyMax,hourlyFee.get(vehicle.size));
}
}
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...