Requirements
The parking lot caters to different vehicles. It has multiple spots available based on size of the vehicle(bike , car , semi trcuk etc. The main functionalities will include:
- Parking spot assignment
- Multiple floor support
- Spot availability
- User or Admin
- Ticketing
- Paymet
Define Core Objects
Analyze Relationships
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...
Design Patterns
Consider using design patterns (e.g., Factory, Singleton, Observer, Strategy) that fit the problem...
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Vehicle {
public:
string licensePlate;
string size;
Vehicle(string plate, string sz) : licensePlate(plate), size(sz) {}
};
class ParkingSpot {
public:
string size;
string identifier;
int floorNumber; // Added floor number to each spot
bool isOccupied;
Vehicle* v; // Pointer to a vehicle
ParkingSpot(string sz, string id, int floor)
: size(sz), identifier(id), floorNumber(floor), isOccupied(false), v(nullptr) {}
// Assign a vehicle to the parking spot
void assignVehicle(Vehicle* vehicle) {
if (!isOccupied && vehicle->size == size) {
v = vehicle;
isOccupied = true;
}
}
// Remove the vehicle from the parking spot
void removeVehicle() {
if (isOccupied) {
v = nullptr;
isOccupied = false;
}
}
};
class ParkingFloor {
public:
int floorNumber;
vector<ParkingSpot> spots; // Vector to store parking spots on this floor
ParkingFloor(int floorNum, int totalSpots) : floorNumber(floorNum) {
// Initialize parking spots for this floor with different identifiers
for (int i = 0; i < totalSpots; i++) {
spots.push_back(ParkingSpot("small", "S" + to_string(i), floorNum)); // Example: small-sized spots
}
}
// Find an available spot for the vehicle on this floor
ParkingSpot* findAvailableSpot(Vehicle* v) {
for (auto& spot : spots) {
if (spot.size == v->size && !spot.isOccupied) {
return &spot;
}
}
return nullptr; // No available spot found
}
};
class ParkingLot {
public:
vector<ParkingFloor> floors; // Vector to store multiple floors
int totalFloors;
int spotsPerFloor;
ParkingLot(int totalFloors, int spotsPerFloor) : totalFloors(totalFloors), spotsPerFloor(spotsPerFloor) {
// Initialize the parking lot with floors and spots
for (int i = 0; i < totalFloors; i++) {
floors.push_back(ParkingFloor(i, spotsPerFloor)); // Create floors
}
}
// Assign a parking spot to the vehicle across multiple floors
void assignSpot(Vehicle* v) {
for (auto& floor : floors) {
ParkingSpot* availableSpot = floor.findAvailableSpot(v);
if (availableSpot != nullptr) {
availableSpot->assignVehicle(v);
cout << "Assigned vehicle " << v->licensePlate << " to spot " << availableSpot->identifier
<< " on floor " << availableSpot->floorNumber << endl;
return;
}
}
cout << "No available spot for vehicle " << v->licensePlate << endl;
}
// Release the parking spot occupied by the vehicle
void releaseSpot(Vehicle* v) {
for (auto& floor : floors) {
for (auto& spot : floor.spots) {
if (spot.v == v && spot.isOccupied) {
spot.removeVehicle();
cout << "Released spot " << spot.identifier << " on floor " << spot.floorNumber << endl;
return;
}
}
}
cout << "Vehicle " << v->licensePlate << " not found in the parking lot." << endl;
}
};
int main() {
// Create a parking lot with 3 floors, each with 5 spots
ParkingLot lot(3, 5);
// Create some vehicles
Vehicle car1("ABC123", "small");
Vehicle car2("XYZ789", "small");
// Assign spots to vehicles
lot.assignSpot(&car1);
lot.assignSpot(&car2);
// Release a spot
lot.releaseSpot(&car1);
return 0;
}
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...