-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParkingLot.cpp
284 lines (254 loc) · 6.77 KB
/
ParkingLot.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Not Thread Safe
#include <bits/stdc++.h>
using namespace std;
enum class VehicleType
{
Car,
Bike,
Truck,
};
// Array to map enum class values to string
const string VehicleTypeStrings[] = {"Car", "Bike", "Truck"};
// Utility to convert enum class to string
string vehicleTypeToString(VehicleType type)
{
return VehicleTypeStrings[static_cast<int>(type)];
}
class Vehicle
{
protected:
string license_plate;
VehicleType type;
public:
Vehicle(string license_plate, VehicleType type) : license_plate(license_plate), type(type)
{
cout << "This is a " + vehicleTypeToString(type) << endl;
}
VehicleType getType()
{
return type;
}
};
class Car : public Vehicle
{
public:
Car(string license_plate) : Vehicle(license_plate, VehicleType::Car) {}
};
class Bike : public Vehicle
{
public:
Bike(string license_plate) : Vehicle(license_plate, VehicleType::Bike) {}
};
class Truck : public Vehicle
{
public:
Truck(string license_plate) : Vehicle(license_plate, VehicleType::Truck) {}
};
class ParkingSpot
{
private:
const int spotNumber;
const VehicleType spotType;
Vehicle *parkedVehicle;
public:
ParkingSpot(int spotNumber, VehicleType type) : spotNumber(spotNumber), spotType(type), parkedVehicle(nullptr) {}
bool isAvailable()
{
return this->parkedVehicle == NULL;
}
bool park(Vehicle *vehicle)
{
if (!this->isAvailable())
return false;
if (vehicle->getType() == spotType)
{
parkedVehicle = vehicle;
return true;
}
else
{
return false;
}
}
bool release(Vehicle *vehicle)
{
if (parkedVehicle == vehicle)
{
parkedVehicle = NULL;
return true;
}
return false;
}
Vehicle *getParkedVehicle()
{
return parkedVehicle;
}
int getSpotNumber()
{
return spotNumber;
}
VehicleType getSpotType()
{
return spotType;
}
};
class Level
{
private:
int floorNumber;
list<ParkingSpot *> parkingSpots;
public:
Level(int level, int spots)
{
floorNumber = level;
int numBikes = spots / 2;
int numTrucks = spots / 10;
int numCars = spots - (numBikes + numTrucks);
int i = 1;
for (; i <= numBikes; i++)
{
parkingSpots.push_back(new ParkingSpot(i, VehicleType::Bike));
}
for (; i <= numBikes + numCars; i++)
{
parkingSpots.push_back(new ParkingSpot(i, VehicleType::Car));
}
for (; i <= spots; i++)
{
parkingSpots.push_back(new ParkingSpot(i, VehicleType::Truck));
}
}
Level(int level, int numBikes, int numCars, int numTrucks)
{
floorNumber = level;
int spots = numBikes + numCars + numTrucks;
int i = 1;
for (; i <= numBikes; i++)
{
parkingSpots.push_back(new ParkingSpot(i, VehicleType::Bike));
}
for (; i <= numBikes + numCars; i++)
{
parkingSpots.push_back(new ParkingSpot(i, VehicleType::Car));
}
for (; i <= spots; i++)
{
parkingSpots.push_back(new ParkingSpot(i, VehicleType::Truck));
}
}
int getTotalspots()
{
return parkingSpots.size();
}
bool parkVehicle(Vehicle *vehicle)
{
for (auto spot : parkingSpots)
{
if (spot->park(vehicle))
return true;
}
return false;
}
bool releaseVehicle(Vehicle *vehicle)
{
for (auto spot : parkingSpots)
{
if (spot->release(vehicle))
return true;
}
return false;
}
void realtimeStatus()
{
cout << "Level : " + to_string(floorNumber) << endl;
cout << "Total number of Parking spots: " + to_string(getTotalspots()) << endl;
for (auto spot : parkingSpots)
{
cout << "Spot " + to_string(spot->getSpotNumber()) + " of type " + vehicleTypeToString(spot->getSpotType())
<< (spot->isAvailable() ? " is free." : " is Occupied.") << endl;
}
}
};
class ParkingLot
{
private:
list<Level *> levels;
static ParkingLot *instance;
ParkingLot() {}
public:
// Delete copy constructor and assignment operator
ParkingLot(const ParkingLot &) = delete;
ParkingLot &operator=(const ParkingLot &) = delete;
// Static method to provide access to the singleton instance
static ParkingLot *getInstance()
{
if (instance == nullptr)
{
instance = new ParkingLot();
}
return instance;
}
void addLevel(Level *level)
{
levels.push_back(level);
}
void parkVehicle(Vehicle *vehicle)
{
for (auto level : levels)
{
if (level->parkVehicle(vehicle))
{
cout << "Vehicle Parked Successfully." << endl;
return;
}
}
cout << "Could not park." << endl;
}
void releaseVehicle(Vehicle *vehicle)
{
for (auto level : levels)
{
if (level->releaseVehicle(vehicle))
{
cout << "Vehicle Released Successfully." << endl;
return;
}
}
cout << "Could not find Vehicle." << endl;
}
void displayAvailability()
{
for (auto level : levels)
{
level->realtimeStatus();
}
}
};
ParkingLot *ParkingLot ::instance = NULL;
int main() {
// Get the singleton instance of ParkingLot
ParkingLot* parkingLot = ParkingLot::getInstance();
// Add levels to the parking lot
Level* level1 = new Level(1, 10); // 10 spots (default distribution of bikes, cars, and trucks)
Level* level2 = new Level(2, 5, 3, 2); // Custom distribution: 5 bike spots, 3 car spots, and 2 truck spots
parkingLot->addLevel(level1);
parkingLot->addLevel(level2);
// Create vehicles
Vehicle* car1 = new Car("CAR123");
Vehicle* bike1 = new Bike("BIKE456");
Vehicle* truck1 = new Truck("TRUCK789");
// Park vehicles
parkingLot->parkVehicle(car1); // Should find a car spot
parkingLot->parkVehicle(bike1); // Should find a bike spot
parkingLot->parkVehicle(truck1); // Should find a truck spot
// Try parking a car again (to test availability)
Vehicle* car2 = new Car("CAR234");
parkingLot->parkVehicle(car2); // Should find the next available car spot
// Display parking availability
parkingLot->displayAvailability();
// Release a vehicle
parkingLot->releaseVehicle(car1); // Should release car1 from its spot
// Display parking availability after releasing
parkingLot->displayAvailability();
return 0;
}