forked from Rishav123918/Parking_Application_C-
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparking1.cpp
More file actions
828 lines (748 loc) · 22.8 KB
/
parking1.cpp
File metadata and controls
828 lines (748 loc) · 22.8 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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
#include <iostream>
#include <vector>
#include <string>
#include <conio.h>
#include <cctype>
#include <algorithm>
#include <windows.h>
#include <iomanip>
#include <sstream>
#include <regex>
#include <fstream>
using namespace std;
// This sets the width of the menu box
constexpr int MENU_WIDTH = 50;
// This helps center text in the middle
void printHeaderCentered(const string& text) {
auto len = static_cast<int>(text.length());
if (len >= MENU_WIDTH)
{
cout << text << endl;
}
else
{
int padding = (MENU_WIDTH - len) / 2;
cout << string(padding, ' ') << text << endl;
}
}
// This prints the menu title with borders
void printHeader(const string& title) {
system("cls");
cout << string(MENU_WIDTH, '=') << endl;
printHeaderCentered(title);
cout << string(MENU_WIDTH, '=') << endl;
}
// This prints a separator line
void printDivider() {
cout << string(MENU_WIDTH, '-') << endl;
}
// This shows the goodbye message
void showExitMessage() {
system("cls");
cout << "\n";
cout << string(MENU_WIDTH, '=') << endl;
printHeaderCentered("THANK YOU FOR USING OUR SYSTEM");
printHeaderCentered("HAVE A GREAT DAY!");
cout << string(MENU_WIDTH, '=') << endl;
cout << "\n";
Sleep(1000);
}
// This ensures the user enters a number
[[nodiscard]] int getIntInput() {
string line;
while (true)
{
if (!getline(cin, line))
{
return 0;
}
try
{
if (!line.empty())
{
return stoi(line);
}
}
catch (...)
{
// Do nothing if conversion fails
}
cout << "Invalid input! Please enter a number: ";
}
}
// This converts text to UPPERCASE
[[nodiscard]] string toUpperStr(const string& s) {
string result = s;
transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;
}
// --- CLASS: TIME ---
class Time {
public:
int hh = 0;
int mm = 0;
int ss = 0;
// Convert time to seconds for math
[[nodiscard]] long toSeconds() const {
return hh * 3600 + mm * 60 + ss;
}
// Format time nicely
[[nodiscard]] string toString() const {
stringstream ss_stream;
ss_stream << setfill('0') << setw(2) << hh << ":"
<< setfill('0') << setw(2) << mm << ":"
<< setfill('0') << setw(2) << ss;
return ss_stream.str();
}
// Check if time is valid
[[nodiscard]] static bool isValid(int h, int m, int s) {
return (h >= 0 && h <= 23 && m >= 0 && m <= 59 && s >= 0 && s <= 59);
}
};
// --- CLASS: DATE ---
class Date {
public:
int dd = 0;
int mm = 0;
int yy = 0;
// Format date nicely
[[nodiscard]] string toString() const {
stringstream ss_stream;
ss_stream << setfill('0') << setw(2) << dd << "/"
<< setfill('0') << setw(2) << mm << "/" << yy;
return ss_stream.str();
}
// Check if date is valid
[[nodiscard]] static bool isValid(int d, int m, int y) {
if (m < 1 || m > 12 || d < 1)
{
return false;
}
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// Check for leap year
if (m == 2 && ((y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)))
{
days[2] = 29;
}
return d <= days[m];
}
};
// --- CLASS: VEHICLE ---
class Vehicle {
int token;
string owner;
string vno;
Time inT;
Time outT;
Date d;
int durationHours;
bool isCar;
public:
// Constructor for new vehicles
Vehicle(int t, bool c) : token(t), durationHours(0), isCar(c) {}
// Constructor for loading from file
Vehicle(int t, string o, string v, bool c, int h1, int m1, int s1, int h2, int m2, int s2, int dd, int mm, int yy)
: token(t), owner(std::move(o)), vno(std::move(v)), durationHours(0), isCar(c)
{
inT.hh = h1;
inT.mm = m1;
inT.ss = s1;
outT.hh = h2;
outT.mm = m2;
outT.ss = s2;
d.dd = dd;
d.mm = mm;
d.yy = yy;
calculateDuration();
}
[[nodiscard]] int getToken() const { return token; }
[[nodiscard]] string getVNo() const { return vno; }
void setVNo(const string& v) { vno = v; }
[[nodiscard]] bool getType() const { return isCar; }
// Check if it is Rush Hour
[[nodiscard]] static bool isPeakHour(int h) {
return ((h >= 9 && h <= 11) || (h >= 17 && h <= 20));
}
// Calculate parking duration
void calculateDuration() {
long diff = outT.toSeconds() - inT.toSeconds();
if (diff < 0)
{
diff += 24 * 3600;
}
durationHours = (diff + 3599) / 3600;
if (durationHours == 0)
{
durationHours = 1;
}
}
// Calculate the price
[[nodiscard]] int getFare() const {
int totalFare = 0;
int currentClockHour = inT.hh;
for (int i = 0; i < durationHours; i++)
{
bool isPeak = isPeakHour(currentClockHour);
if (isCar)
{
if (isPeak) {
totalFare += 30;
} else {
totalFare += 20;
}
}
else
{
if (isPeak) {
totalFare += 15;
} else {
totalFare += 10;
}
}
currentClockHour++;
if (currentClockHour >= 24)
{
currentClockHour = 0;
}
}
return totalFare;
}
// Prepare data for saving
[[nodiscard]] string getFileString() const {
stringstream ss;
ss << token << "|" << owner << "|" << vno << "|" << isCar << "|"
<< inT.hh << " " << inT.mm << " " << inT.ss << "|"
<< outT.hh << " " << outT.mm << " " << outT.ss << "|"
<< d.dd << " " << d.mm << " " << d.yy;
return ss.str();
}
// Get input from user
void inputDetails() {
cout << "Enter Owner Name: ";
getline(cin, owner);
bool validIn = false;
while (!validIn)
{
cout << "Arrival Time (HH MM SS): ";
cin >> inT.hh >> inT.mm >> inT.ss;
cin.ignore(1000, '\n');
if (Time::isValid(inT.hh, inT.mm, inT.ss))
{
validIn = true;
}
}
bool validOut = false;
while (!validOut)
{
cout << "Exit Time (HH MM SS): ";
cin >> outT.hh >> outT.mm >> outT.ss;
cin.ignore(1000, '\n');
if (Time::isValid(outT.hh, outT.mm, outT.ss))
{
validOut = true;
}
}
bool validDate = false;
while (!validDate)
{
cout << "Date (DD MM YY): ";
cin >> d.dd >> d.mm >> d.yy;
cin.ignore(1000, '\n');
if (Date::isValid(d.dd, d.mm, d.yy))
{
validDate = true;
}
}
calculateDuration();
}
// Show vehicle details
void display() const {
cout << string(MENU_WIDTH, '-') << endl;
cout << " Token : " << token << endl;
cout << " Vehicle No : " << vno << endl;
cout << " Type : " << (isCar ? "Car" : "Bike") << endl;
cout << " Owner : " << owner << endl;
cout << " Date : " << d.toString() << endl;
cout << " Time : " << inT.toString() << " -> " << outT.toString() << endl;
cout << " Duration : " << durationHours << " Hours" << endl;
cout << " Total Fare : Rs " << getFare() << endl;
cout << string(MENU_WIDTH, '-') << endl;
}
};
// --- CLASS: SECURITY SYSTEM ---
class SecuritySystem {
private:
string adminPass = "1234";
string staffPass = "0000";
// Hide password input
[[nodiscard]] static string getMaskedInput() {
string pass;
while (true)
{
int ch = _getch();
if (ch == 13)
{
cout << endl;
break;
}
else if (ch == 8)
{
if (!pass.empty())
{
pass.pop_back();
cout << "\b \b";
}
}
else if (isprint(static_cast<unsigned char>(ch)))
{
pass.push_back(static_cast<char>(ch));
cout << "*";
}
}
return pass;
}
public:
// Login logic
[[nodiscard]] int login() const {
while (true)
{
printHeader("SYSTEM LOGIN");
cout << "Username (admin/staff): ";
string user;
getline(cin, user);
user = toUpperStr(user);
if (user != "ADMIN" && user != "STAFF")
{
cout << "\n[!] Invalid Username!\n";
system("pause");
continue;
}
int attempts = 0;
while (attempts < 3)
{
printHeader("SYSTEM LOGIN");
cout << "Username : " << user << endl;
cout << "Password : ";
string pass = getMaskedInput();
if (user == "ADMIN" && pass == adminPass)
{
return 1;
}
if (user == "STAFF" && pass == staffPass)
{
return 2;
}
attempts++;
cout << "\n[!] Incorrect Password! (" << (3 - attempts) << " attempts left)\n";
if (attempts >= 3)
{
cout << "\n[!] Security Lockout. Exiting...\n";
exit(0);
}
system("pause");
}
}
}
// Change password logic
void changePassword() {
while (true)
{
printHeader("CHANGE PASSWORD");
cout << "Enter Username (admin/staff) or '0' to Back: ";
string user;
getline(cin, user);
if (user == "0")
{
return;
}
user = toUpperStr(user);
if (user != "ADMIN" && user != "STAFF")
{
cout << "\n[!] Invalid Username!\n";
system("pause");
continue;
}
int attempts = 0;
while (true)
{
cout << "Enter Old Password: ";
string oldP = getMaskedInput();
if ((user == "ADMIN" && oldP == adminPass) || (user == "STAFF" && oldP == staffPass))
{
cout << "Enter New Password: ";
string newP = getMaskedInput();
if (user == "ADMIN")
{
adminPass = newP;
}
else
{
staffPass = newP;
}
cout << "\n[+] Password Changed Successfully!\n";
system("pause");
return;
}
else
{
attempts++;
if (attempts >= 3)
{
cout << "\n[!] Too many attempts. Exiting...\n";
exit(0);
}
cout << "[!] Wrong Password!\n";
}
}
}
}
};
// --- CLASS: PARKING SYSTEM ---
class ParkingSystem {
vector<Vehicle> vehicles;
int nextToken = 100;
// Check if vehicle already exists
[[nodiscard]] bool isDuplicate(const string& vno) const {
return any_of(vehicles.begin(), vehicles.end(),
[&vno](const Vehicle& x) { return x.getVNo() == vno; });
}
// Validate number plate format
[[nodiscard]] static bool isValidPlateFormat(const string& plate) {
const regex pattern(R"(^[A-Z]{2}[0-9]{2} [A-Z]{1,2} [0-9]{1,7}$)");
return regex_match(plate, pattern);
}
// Save to file
void saveToFile() const {
ofstream outFile("parking_data.txt");
if (!outFile)
{
return;
}
for (const auto& v : vehicles)
{
outFile << v.getFileString() << endl;
}
outFile.close();
}
// Load from file
void loadFromFile() {
ifstream inFile("parking_data.txt");
if (!inFile)
{
return;
}
string line;
while (getline(inFile, line))
{
stringstream ss(line);
string segment;
vector<string> data;
while (getline(ss, segment, '|'))
{
data.push_back(segment);
}
if (data.size() == 7)
{
int t = stoi(data[0]);
string o = data[1];
string v = data[2];
bool c = (data[3] == "1");
stringstream t1(data[4]);
int h1;
int m1;
int s1;
t1 >> h1 >> m1 >> s1;
stringstream t2(data[5]);
int h2;
int m2;
int s2;
t2 >> h2 >> m2 >> s2;
stringstream d1(data[6]);
int dd;
int mm;
int yy;
d1 >> dd >> mm >> yy;
vehicles.emplace_back(t, o, v, c, h1, m1, s1, h2, m2, s2, dd, mm, yy);
if (t >= nextToken)
{
nextToken = t + 1;
}
}
}
inFile.close();
}
public:
// Initialize system
ParkingSystem() {
loadFromFile();
}
// Show rates
static void showRates() {
printHeader("PARKING RATES");
cout << " TYPE | NORMAL (Per Hr) | RUSH HOUR (Per Hr)\n";
printDivider();
cout << " BIKE | Rs 10 | Rs 15 \n";
cout << " CAR | Rs 20 | Rs 30 \n";
printDivider();
cout << " * Rush Hours: 09:00 - 11:00 & 17:00 - 20:00\n";
printDivider();
system("pause");
}
// Add vehicle
void addVehicle() {
printHeader("ADD NEW VEHICLE");
cout << "<< CURRENT TOKEN NUMBER: " << nextToken << " >>\n\n";
cout << "1. Car\n2. Bike\nSelect Type: ";
int t = getIntInput();
if (t != 1 && t != 2)
{
cout << "\n[!] Invalid choice!\n";
system("pause");
return;
}
string vInput;
bool validPlate = false;
while (!validPlate)
{
cout << "Enter Vehicle No (Format: XX00 XX 0000): ";
getline(cin, vInput);
vInput = toUpperStr(vInput);
if (isValidPlateFormat(vInput))
{
validPlate = true;
}
else
{
cout << "[!] Invalid Format! Example: JH05 CJ 5596\n";
}
}
if (isDuplicate(vInput))
{
cout << "\n[!] Error: Vehicle already parked!\n";
system("pause");
return;
}
Vehicle v(nextToken, t == 1);
v.setVNo(vInput);
v.inputDetails();
vehicles.push_back(v);
nextToken++;
saveToFile();
cout << "\n[+] Vehicle Parked Successfully! Token: " << v.getToken() << endl;
system("pause");
}
// Search for vehicle
void searchToken() const {
printHeader("SEARCH BY TOKEN");
cout << "Enter Token ID: ";
int t = getIntInput();
bool found = false;
for (const auto& v : vehicles)
{
if (v.getToken() == t)
{
v.display();
found = true;
system("pause");
return;
}
}
if (!found)
{
cout << "\n[!] Vehicle Not Found!\n";
system("pause");
}
}
// Delete vehicle (Checkout)
void deleteVehicle() {
printHeader("VEHICLE CHECKOUT");
cout << "Enter Token ID(s) separated by space: ";
string line;
getline(cin, line);
stringstream ss(line);
int t = 0;
vector<int> tokensToFind;
while (ss >> t)
{
tokensToFind.push_back(t);
}
sort(tokensToFind.begin(), tokensToFind.end());
tokensToFind.erase(unique(tokensToFind.begin(), tokensToFind.end()), tokensToFind.end());
if (tokensToFind.empty())
{
cout << "\n[!] No input provided.\n";
system("pause");
return;
}
vector<int> validTokens;
int totalBatchFare = 0;
cout << "\n======= CHECKOUT SUMMARY =======\n";
for (int token : tokensToFind)
{
bool found = false;
for (auto& v : vehicles)
{
if (v.getToken() == token)
{
v.display();
totalBatchFare += v.getFare();
validTokens.push_back(token);
found = true;
break;
}
}
if (!found)
{
cout << "Token " << token << ": Not Found (Skipped)\n";
}
}
if (validTokens.empty())
{
cout << "\n[!] No valid vehicles found.\n";
system("pause");
return;
}
printDivider();
cout << " Vehicles to Remove : " << validTokens.size() << endl;
cout << " Grand Total Fare : Rs " << totalBatchFare << endl;
printDivider();
cout << "Confirm Checkout ALL? (y/n): ";
string c;
getline(cin, c);
if (toUpperStr(c) == "Y")
{
auto newEnd = remove_if(vehicles.begin(), vehicles.end(),
[&](const Vehicle& v) {
return find(validTokens.begin(), validTokens.end(), v.getToken()) != validTokens.end();
});
vehicles.erase(newEnd, vehicles.end());
saveToFile();
cout << "\n[+] Checkout Successful!\n";
}
else
{
cout << "\n[-] Operation Cancelled.\n";
}
system("pause");
}
// Show report
void report() const {
printHeader("FULL PARKING REPORT");
if (vehicles.empty())
{
cout << "\n[i] No vehicles currently parked.\n";
system("pause");
return;
}
int cars = 0;
int bikes = 0;
int total = 0;
for (const auto& v : vehicles)
{
v.display();
if (v.getType())
{
cars++;
}
else
{
bikes++;
}
total += v.getFare();
}
printDivider();
cout << " SUMMARY REPORT\n";
printDivider();
cout << " Total Cars : " << cars << endl;
cout << " Total Bikes : " << bikes << endl;
cout << " Total Revenue : Rs " << total << endl;
printDivider();
system("pause");
}
};
// Shows the opening screen
void showWelcome() {
system("cls");
cout << "\n\n";
cout << string(MENU_WIDTH, '=') << endl;
printHeaderCentered("WELCOME TO PARKING SYSTEM");
printHeaderCentered("Manage Vehicles Efficiently");
cout << string(MENU_WIDTH, '=') << endl;
printHeaderCentered("System Loading...");
cout << "\n\n";
Sleep(1500);
}
// --- MAIN FUNCTION ---
int main() {
SecuritySystem security;
ParkingSystem parking;
int role = 0;
showWelcome();
bool appRunning = true;
while (appRunning)
{
printHeader("MAIN MENU");
cout << "1. Login\n2. Change Password\n3. View Parking Rates\n4. Exit\n";
printDivider();
cout << "Choice: ";
int choice = getIntInput();
if (choice == 1)
{
role = security.login();
bool loggedIn = true;
while (loggedIn)
{
string roleTitle = (role == 1) ? "ADMIN DASHBOARD" : "STAFF DASHBOARD";
printHeader(roleTitle);
cout << "1. Add Vehicle\n2. Search Token\n3. View Rates\n";
if (role == 1)
{
cout << "4. Checkout (Delete)\n5. Full Report\n6. Logout\n";
}
else
{
cout << "4. Full Report\n5. Logout\n";
}
printDivider();
cout << "Choice: ";
int pChoice = getIntInput();
if (role == 1)
{
if (pChoice == 1) parking.addVehicle();
else if (pChoice == 2) parking.searchToken();
else if (pChoice == 3) ParkingSystem::showRates();
else if (pChoice == 4) parking.deleteVehicle();
else if (pChoice == 5) parking.report();
else if (pChoice == 6) loggedIn = false;
}
else
{
if (pChoice == 1) parking.addVehicle();
else if (pChoice == 2) parking.searchToken();
else if (pChoice == 3) ParkingSystem::showRates();
else if (pChoice == 4) parking.report();
else if (pChoice == 5) loggedIn = false;
}
}
}
else if (choice == 2)
{
security.changePassword();
}
else if (choice == 3)
{
ParkingSystem::showRates();
}
else if (choice == 4)
{
showExitMessage();
appRunning = false;
}
else
{
cout << "\n[!] Invalid Choice\n";
system("pause");
}
}
return 0;
}