-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleParkSystem_v1.0.cpp
More file actions
147 lines (123 loc) · 4.59 KB
/
SimpleParkSystem_v1.0.cpp
File metadata and controls
147 lines (123 loc) · 4.59 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
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
#include <iostream>
#include <vector>
#include <ctime>
#include <iomanip>
#include <windows.h> // For Sleep function
using namespace std;
// Vehicle information structure
struct Car {
int id; // Vehicle ID
string plate; // License plate
time_t parkTime; // Parking time
bool isParked; // Is currently parked
};
class ParkingSystem {
private:
vector<Car> cars; // Store all vehicles
double hourlyRate; // Hourly fee
int nextId; // Next vehicle ID
public:
ParkingSystem(double rate) {
hourlyRate = rate;
nextId = 1;
cout << "Parking system started. Fee rate: " << rate << " yuan/hour" << endl;
}
// Park a car
void parkCar() {
string plate;
cout << "\nEnter license plate: ";
cin >> plate;
// Check if plate already exists
for (int i = 0; i < cars.size(); i++) {
if (cars[i].plate == plate && cars[i].isParked) {
cout << "This car is already parked!" << endl;
return;
}
}
Car newCar;
newCar.id = nextId++;
newCar.plate = plate;
newCar.parkTime = time(0); // Get current time
newCar.isParked = true;
cars.push_back(newCar);
cout << "Car parked successfully!" << endl;
cout << "Vehicle ID: " << newCar.id << endl;
cout << "License plate: " << newCar.plate << endl;
cout << "Parking time: " << ctime(&newCar.parkTime);
}
// Take a car
void takeCar() {
int id;
cout << "\nEnter vehicle ID: ";
cin >> id;
for (int i = 0; i < cars.size(); i++) {
if (cars[i].id == id && cars[i].isParked) {
time_t now = time(0);
double hours = difftime(now, cars[i].parkTime) / 3600.0;
// Minimum 1 hour
if (hours < 1) hours = 1;
double fee = hours * hourlyRate;
cout << "\n=== Vehicle Retrieval Information ===" << endl;
cout << "License plate: " << cars[i].plate << endl;
cout << "Parking time: " << ctime(&cars[i].parkTime);
cout << "Retrieval time: " << ctime(&now);
cout << "Parking duration: " << fixed << setprecision(1) << hours << " hours" << endl;
cout << "Fee: " << fixed << setprecision(2) << fee << " yuan" << endl;
cout << "===================================" << endl;
cars[i].isParked = false;
return;
}
}
cout << "Vehicle not found or already taken!" << endl;
}
// Check parking lot status
void showStatus() {
int parkedCount = 0;
cout << "\n=== Parking Lot Status ===" << endl;
for (int i = 0; i < cars.size(); i++) {
if (cars[i].isParked) {
parkedCount++;
cout << "ID: " << cars[i].id
<< " Plate: " << cars[i].plate
<< " Parking time: " << ctime(&cars[i].parkTime);
}
}
cout << "Currently " << parkedCount << " cars in parking lot" << endl;
cout << "==========================" << endl;
}
};
int main() {
// Set hourly fee to 5 yuan
ParkingSystem parking(5.0);
int choice;
while (true) {
cout << "\n=== Parking Management System ===" << endl;
cout << "1. Park a car" << endl;
cout << "2. Take a car" << endl;
cout << "3. Check parking lot status" << endl;
cout << "4. Exit system" << endl;
cout << "Choose option (1-4): ";
cin >> choice;
switch (choice) {
case 1:
parking.parkCar();
break;
case 2:
parking.takeCar();
break;
case 3:
parking.showStatus();
break;
case 4:
cout << "Thank you for using! Goodbye!" << endl;
Sleep(2000); // Wait 2 seconds before exit
return 0;
default:
cout << "Wrong input, please try again!" << endl;
}
// Pause 1 second after each operation
cout << "\nOperation completed. Returning to menu in 1 second..." << endl;
Sleep(1000);
}
return 0;
}