-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Expand file tree
/
Copy pathParkingLot.cpp
More file actions
92 lines (75 loc) · 2.61 KB
/
ParkingLot.cpp
File metadata and controls
92 lines (75 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include "ParkingLot.hpp"
#include <iostream>
ParkingLot::ParkingLot(int numCompact, int numRegular, int numLarge)
: capacity(numCompact + numRegular + numLarge), availableSpots(capacity) {
int spotNumber = 1;
// Create compact spots
for (int i = 0; i < numCompact; i++) {
spots.push_back(new ParkingSpot(spotNumber++, SpotType::COMPACT));
}
// Create regular spots
for (int i = 0; i < numRegular; i++) {
spots.push_back(new ParkingSpot(spotNumber++, SpotType::REGULAR));
}
// Create large spots
for (int i = 0; i < numLarge; i++) {
spots.push_back(new ParkingSpot(spotNumber++, SpotType::LARGE));
}
}
ParkingLot::~ParkingLot() {
for (auto spot : spots) {
delete spot;
}
}
int ParkingLot::getCapacity() const { return capacity; }
int ParkingLot::getAvailableSpots() const { return availableSpots; }
bool ParkingLot::parkVehicle(Vehicle* vehicle) {
if (!vehicle) return false;
// Check if vehicle is already parked
if (occupiedSpots.find(vehicle->getLicensePlate()) != occupiedSpots.end()) {
return false;
}
ParkingSpot* spot = findAvailableSpot(vehicle);
if (!spot) return false;
if (spot->parkVehicle(vehicle)) {
occupiedSpots[vehicle->getLicensePlate()] = spot;
availableSpots--;
return true;
}
return false;
}
Vehicle* ParkingLot::removeVehicle(const std::string& licensePlate) {
auto it = occupiedSpots.find(licensePlate);
if (it == occupiedSpots.end()) return nullptr;
ParkingSpot* spot = it->second;
Vehicle* vehicle = spot->removeVehicle();
if (vehicle) {
occupiedSpots.erase(it);
availableSpots++;
}
return vehicle;
}
ParkingSpot* ParkingLot::findVehicle(const std::string& licensePlate) const {
auto it = occupiedSpots.find(licensePlate);
return it != occupiedSpots.end() ? it->second : nullptr;
}
void ParkingLot::displayInfo() const {
std::cout << "\nParking Lot Status:" << std::endl;
std::cout << "Total Capacity: " << capacity << std::endl;
std::cout << "Available Spots: " << availableSpots << std::endl;
std::cout << "Occupied Spots: " << (capacity - availableSpots) << std::endl;
}
void ParkingLot::displayOccupancy() const {
std::cout << "\nDetailed Occupancy:" << std::endl;
for (const auto& spot : spots) {
spot->displayInfo();
}
}
ParkingSpot* ParkingLot::findAvailableSpot(const Vehicle* vehicle) const {
for (auto spot : spots) {
if (spot->isAvailable() && spot->canFitVehicle(vehicle)) {
return spot;
}
}
return nullptr;
}