-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransportSystem.cpp
More file actions
700 lines (688 loc) · 21.4 KB
/
Copy pathTransportSystem.cpp
File metadata and controls
700 lines (688 loc) · 21.4 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
#include "TransportSystem.h"
#include <iostream>
using namespace std;
TransportSystem::TransportSystem()
{
studentCount = 0;
adminCount = 0;
vehicleCount = 0;
routeCount = 0;
regCount = 0;
nextStudentID = 1;
nextVehicleID = 1;
nextRouteID = 1;
nextRegID = 1;
nextBillID = 1;
for (int i = 0; i < MAX_STUDENTS; i++)
students[i] = nullptr;
for (int i = 0; i < MAX_ADMINS; i++)
admins[i] = nullptr;
for (int i = 0; i < MAX_VEHICLES; i++)
vehicles[i] = nullptr;
for (int i = 0; i < MAX_ROUTES; i++)
routes[i] = nullptr;
for (int i = 0; i < MAX_REGS; i++)
registrations[i] = nullptr;
}
TransportSystem::~TransportSystem()
{
for (int i = 0; i < MAX_STUDENTS; i++)
{
delete students[i];
students[i] = nullptr;
}
for (int i = 0; i < MAX_ADMINS; i++)
{
delete admins[i];
admins[i] = nullptr;
}
for (int i = 0; i < MAX_VEHICLES; i++)
{
delete vehicles[i];
vehicles[i] = nullptr;
}
for (int i = 0; i < MAX_ROUTES; i++)
{
delete routes[i];
routes[i] = nullptr;
}
for (int i = 0; i < MAX_REGS; i++)
{
delete registrations[i];
registrations[i] = nullptr;
}
}
// Data Persistence
void TransportSystem::loadAllData()
{
cout << " Loading data from files..." << endl;
studentCount = FileHandler::loadStudents(students, MAX_STUDENTS);
adminCount = FileHandler::loadAdmins(admins, MAX_ADMINS);
vehicleCount = FileHandler::loadVehicles(vehicles, MAX_VEHICLES);
routeCount = FileHandler::loadRoutes(routes, MAX_ROUTES);
regCount = FileHandler::loadRegistrations(registrations, MAX_REGS);
// Update ID counters so new IDs don't clash with loaded ones
for (int i = 0; i < studentCount; i++)
{
if (students[i] && students[i]->getID() >= nextStudentID)
{
nextStudentID = students[i]->getID() + 1;
}
}
for (int i = 0; i < vehicleCount; i++)
{
if (vehicles[i] && vehicles[i]->getID() >= nextVehicleID)
{
nextVehicleID = vehicles[i]->getID() + 1;
}
}
for (int i = 0; i < routeCount; i++)
{
if (routes[i] && routes[i]->getID() >= nextRouteID)
{
nextRouteID = routes[i]->getID() + 1;
}
}
for (int i = 0; i < regCount; i++)
{
if (!registrations[i])
{
continue;
}
if (registrations[i]->getRegistrationID() >= nextRegID)
{
nextRegID = registrations[i]->getRegistrationID() + 1;
Bill* b = registrations[i]->getBill();
if (b && b->getBillID() >= nextBillID)
{
nextBillID = b->getBillID() + 1;
}
}
}
// Create a default admin if no admin exists yet
if (adminCount == 0)
{
admins[0] = new Admin(1, "Admin", "admin@uet.edu.pk", "admin123", "ADM001");
adminCount = 1;
cout << " Default admin created." << endl;
cout << " Email: admin@uet.edu.pk | Password: admin123" << endl;
}
cout << " Loaded: " << studentCount << " students, "
<< vehicleCount << " vehicles, "
<< routeCount << " routes, "
<< regCount << " registrations." << endl;
}
void TransportSystem::saveAllData()
{
cout << "\n Saving all data..." << endl;
FileHandler::saveStudents(students, studentCount);
FileHandler::saveAdmins(admins, adminCount);
FileHandler::saveVehicles(vehicles, vehicleCount);
FileHandler::saveRoutes(routes, routeCount);
FileHandler::saveRegistrations(registrations, regCount);
cout << " All data saved successfully!" << endl;
}
// Helper Finders
Student* TransportSystem::findStudent(int id)
{
for (int i = 0; i < studentCount; i++)
if (students[i] && students[i]->getID() == id) return students[i];
return nullptr;
}
Route* TransportSystem::findRoute(int id)
{
for (int i = 0; i < routeCount; i++)
if (routes[i] && routes[i]->getID() == id) return routes[i];
return nullptr;
}
Vehicle* TransportSystem::findVehicle(int id)
{
for (int i = 0; i < vehicleCount; i++)
if (vehicles[i] && vehicles[i]->getID() == id) return vehicles[i];
return nullptr;
}
Registration* TransportSystem::findRegistration(int id)
{
for (int i = 0; i < regCount; i++)
if (registrations[i] && registrations[i]->getRegistrationID() == id)
return registrations[i];
return nullptr;
}
Registration* TransportSystem::findRegistrationByStudent(int studentID)
{
for (int i = 0; i < regCount; i++) {
if (!registrations[i]) continue;
string s = registrations[i]->getStatus();
if (registrations[i]->getStudentID() == studentID
&& s != "cancelled" && s != "rejected")
return registrations[i];
}
return nullptr;
}
// User Management
void TransportSystem::registerStudent()
{
if (studentCount >= MAX_STUDENTS)
{
cout << " ERROR: Student limit reached." << endl;
return;
}
cout << "\n --- Register New Student ---" << endl;
string name, email, pass, roll, dept;
int sem;
cout << " Name : "; cin.ignore(); getline(cin, name);
cout << " Email : "; getline(cin, email);
// Check duplicate email
for (int i = 0; i < studentCount; i++) {
if (students[i] && students[i]->getEmail() == email) {
cout << " ERROR: Email already registered." << endl;
return;
}
}
cout << " Password : "; getline(cin, pass);
cout << " Roll No : "; getline(cin, roll);
cout << " Department : "; getline(cin, dept);
cout << " Semester : "; cin >> sem;
students[studentCount] = new Student(nextStudentID, name, email, pass, roll, dept, sem);
cout << "\n Registered! Your Student ID is: " << nextStudentID << endl;
nextStudentID++;
studentCount++;
}
User* TransportSystem::login() {
cout << "\n --- Login ---" << endl;
string email, pass;
cout << " Email : "; cin.ignore(); getline(cin, email);
cout << " Password : "; getline(cin, pass);
for (int i = 0; i < adminCount; i++)
if (admins[i] && admins[i]->getEmail() == email && admins[i]->getPassword() == pass)
{
cout << " Welcome, " << admins[i]->getName() << " (Admin)" << endl;
return admins[i];
}
for (int i = 0; i < studentCount; i++)
if (students[i] && students[i]->getEmail() == email && students[i]->getPassword() == pass)
{
cout << " Welcome, " << students[i]->getName() << " (Student)" << endl;
return students[i];
}
cout << " ERROR: Invalid email or password." << endl;
return nullptr;
}
// Vehicle Management (Admin)
void TransportSystem::addVehicle()
{
if (vehicleCount >= MAX_VEHICLES)
{
cout << " ERROR: Vehicle limit reached." << endl;
return;
}
cout << "\n --- Add New Vehicle ---" << endl;
int type, cap;
string num;
cout << " Type (1=Bus, 2=Van): "; cin >> type;
cin.ignore();
cout << " Vehicle Number : "; getline(cin, num);
cout << " Capacity (seats) : "; cin >> cap;
if (type == 1)
{
int ac;
cout << " Has AC? (1=Yes, 0=No): "; cin >> ac;
vehicles[vehicleCount] = new Bus(nextVehicleID, num, cap, ac == 1);
}
else
{
string model;
cin.ignore();
cout << " Van Model: "; getline(cin, model);
vehicles[vehicleCount] = new Van(nextVehicleID, num, cap, model);
}
cout << " Vehicle added! ID: " << nextVehicleID << endl;
nextVehicleID++;
vehicleCount++;
}
void TransportSystem::editVehicle()
{
cout << "\n --- Edit Vehicle ---" << endl;
int id;
cout << " Enter Vehicle ID: "; cin >> id;
Vehicle* v = findVehicle(id);
if (!v) { cout << " Vehicle not found." << endl; return; }
v->displayInfo();
cin.ignore();
cout << " New Vehicle Number (Enter to skip): ";
string num; getline(cin, num);
if (!num.empty()) v->setVehicleNumber(num);
cout << " New Capacity (0 to skip): ";
int cap; cin >> cap;
if (cap > 0) v->setCapacity(cap);
cout << " Vehicle updated!" << endl;
}
void TransportSystem::removeVehicle()
{
cout << "\n --- Remove Vehicle ---" << endl;
int id;
cout << " Enter Vehicle ID: "; cin >> id;
for (int i = 0; i < vehicleCount; i++) {
if (!vehicles[i] || vehicles[i]->getID() != id) continue;
// Block removal if vehicle has active registrations
for (int j = 0; j < regCount; j++)
{
if (registrations[j]
&& registrations[j]->getVehicleID() == id
&& registrations[j]->getStatus() == "approved")
{
cout << " Cannot remove: vehicle has active registrations." << endl;
return;
}
}
delete vehicles[i];
for (int k = i; k < vehicleCount - 1; k++)
vehicles[k] = vehicles[k + 1];
vehicleCount--;
vehicles[vehicleCount] = nullptr;
cout << " Vehicle removed." << endl;
return;
}
cout << " Vehicle not found." << endl;
}
void TransportSystem::viewAllVehicles()
{
cout << "\n --- All Vehicles ---" << endl;
if (vehicleCount == 0)
{
cout << " No vehicles in the system." << endl; return;
}
for (int i = 0; i < vehicleCount; i++)
if (vehicles[i]) vehicles[i]->displayInfo();
}
// Route Management (Admin)
void TransportSystem::addRoute()
{
if (routeCount >= MAX_ROUTES)
{
cout << " ERROR: Route limit reached." << endl; return;
}
cout << "\n --- Add New Route ---" << endl;
string start, end;
float dist, fee;
cin.ignore();
cout << " Start Point : "; getline(cin, start);
cout << " End Point : "; getline(cin, end);
cout << " Distance (km) : "; cin >> dist;
cout << " Monthly Fee (Rs.): "; cin >> fee;
routes[routeCount] = new Route(nextRouteID, start, end, dist, fee);
cout << " Route added! ID: " << nextRouteID << endl;
nextRouteID++;
routeCount++;
}
void TransportSystem::assignVehicleToRoute()
{
cout << "\n --- Assign Vehicle to Route ---" << endl;
cout << " Routes:" << endl;
for (int i = 0; i < routeCount; i++)
{
if (routes[i]) routes[i]->displayInfo();
}
cout << " Vehicles:" << endl;
for (int i = 0; i < vehicleCount; i++)
{
if (vehicles[i]) vehicles[i]->displayInfo();
}
int routeID, vehicleID;
cout << " Route ID : "; cin >> routeID;
cout << " Vehicle ID: "; cin >> vehicleID;
Route* r = findRoute(routeID);
Vehicle* v = findVehicle(vehicleID);
if (!r)
{
cout << " Route not found." << endl;
return;
}
if (!v)
{
cout << " Vehicle not found." << endl;
return;
}
r->setAssignedVehicleID(vehicleID);
v->setAssignedRouteID(routeID);
cout << " Vehicle " << vehicleID << " assigned to Route " << routeID << "!" << endl;
}
void TransportSystem::viewAllRoutes()
{
cout << "\n --- All Routes ---" << endl;
if (routeCount == 0)
{
cout << " No routes in the system." << endl;
return;
}
for (int i = 0; i < routeCount; i++)
if (routes[i]) routes[i]->displayInfo();
}
// Registration (Student)
void TransportSystem::applyForTransport(int studentID)
{
Student* s = findStudent(studentID);
if (!s)
{
cout << " Student not found." << endl;
return;
}
if (s->hasRegistration())
{
cout << " You already have a registration (ID: " << s->getRegistrationID() << ")." << endl;
return;
}
if (routeCount == 0)
{
cout << " No routes available." << endl;
return; }
if (regCount >= MAX_REGS)
{
cout << " ERROR: Registration limit reached." << endl;
return;
}
cout << "\n --- Apply for Transport ---" << endl;
viewAllRoutes();
int routeID;
cout << " Enter Route ID: "; cin >> routeID;
Route* r = findRoute(routeID);
if (!r)
{
cout << " Invalid Route ID." << endl;
return;
}
// Check available seats
Vehicle* v = nullptr;
if (r->getAssignedVehicleID() != -1)
v = findVehicle(r->getAssignedVehicleID());
if (!v || v->getAvailableSeats() <= 0)
{
cout << " No available seats on this route." << endl;
return;
}
registrations[regCount] = new Registration(nextRegID, studentID, routeID, "2025-06-01");
cout << " Application submitted! Registration ID: " << nextRegID << endl;
cout << " Status: Pending (waiting for admin approval)" << endl;
s->setRegistrationID(nextRegID);
nextRegID++;
regCount++;
}
void TransportSystem::viewMyRegistration(int studentID)
{
cout << "\n --- My Registration ---" << endl;
Registration* r = findRegistrationByStudent(studentID);
if (!r)
{
cout << " No active registration." << endl;
return;
}
r->displayInfo();
Route* route = findRoute(r->getRouteID());
if (route)
{
cout << " Route info: "; route->displayInfo();
}
}
void TransportSystem::cancelRegistration(int studentID)
{
Student* s = findStudent(studentID);
if (!s || !s->hasRegistration())
{
cout << " No active registration to cancel." << endl; return;
}
Registration* r = findRegistration(s->getRegistrationID());
if (!r)
{
cout << " Registration not found." << endl; return;
}
if (r->getStatus() == "approved")
{
Vehicle* v = findVehicle(r->getVehicleID());
if (v) v->releaseSeat();
}
r->setStatus("cancelled");
s->setRegistrationID(-1);
cout << " Registration cancelled." << endl;
}
void TransportSystem::viewMyBill(int studentID)
{
cout << "\n --- My Bill ---" << endl;
Registration* r = findRegistrationByStudent(studentID);
if (!r)
{
cout << " No active registration." << endl;
return;
}
r->displayBill();
}
// Admin Actions
void TransportSystem::viewAllApplications()
{
cout << "\n --- All Applications ---" << endl;
bool found = false;
for (int i = 0; i < regCount; i++)
{
if (registrations[i])
{
registrations[i]->displayInfo();
found = true;
}
}
if (!found) cout << " No applications found." << endl;
}
void TransportSystem::approveOrRejectRequest()
{
cout << "\n --- Approve / Reject Request ---" << endl;
cout << " Pending applications:" << endl;
for (int i = 0; i < regCount; i++)
if (registrations[i] && registrations[i]->getStatus() == "pending")
registrations[i]->displayInfo();
int regID;
cout << " Enter Registration ID: "; cin >> regID;
Registration* r = findRegistration(regID);
if (!r)
{
cout << " Not found." << endl;
return;
}
if (r->getStatus() != "pending")
{
cout << " This application is already: " << r->getStatus() << endl;
return;
}
cout << " 1. Approve 2. Reject" << endl;
cout << " Choice: ";
int choice; cin >> choice;
if (choice == 1)
{
Route* route = findRoute(r->getRouteID());
if (!route)
{
cout << " Route not found." << endl;
return;
}
Vehicle* v = findVehicle(route->getAssignedVehicleID());
if (!v || !v->bookSeat())
{
cout << " Cannot approve: no available seats." << endl;
return;
}
r->setStatus("approved");
r->setVehicleID(v->getID());
r->generateBill(nextBillID, route->getMonthlyFee(), "2025-06-30");
nextBillID++;
cout << " Registration APPROVED. Seat booked on Vehicle " << v->getID() << endl;
}
else if (choice == 2)
{
r->setStatus("rejected");
Student* s = findStudent(r->getStudentID());
if (s) s->setRegistrationID(-1);
cout << " Registration REJECTED." << endl;
}
else
{
cout << " Invalid choice." << endl;
}
}
void TransportSystem::applyLateFine()
{
cout << "\n --- Apply Late Fine ---" << endl;
int regID;
float fine;
cout << " Registration ID : "; cin >> regID;
cout << " Fine Amount (Rs.) : "; cin >> fine;
Registration* r = findRegistration(regID);
if (!r)
{
cout << " Not found." << endl;
return;
}
r->applyLateFine(fine);
}
void TransportSystem::generateReports()
{
cout << "\n --- Reports ---" << endl;
cout << " 1. System Summary" << endl;
cout << " 2. Revenue Report" << endl;
cout << " 3. Route-wise Usage" << endl;
cout << " Choice: ";
int ch; cin >> ch;
Report rpt("Admin Report", "2025-06-01");
if (ch == 1)
{
rpt.printSummary(studentCount, vehicleCount, routeCount, regCount);
}
else if (ch == 2)
{
float totalRevenue = 0;
int totalBills = 0, paidBills = 0;
for (int i = 0; i < regCount; i++) {
if (!registrations[i]) continue;
Bill* b = registrations[i]->getBill();
if (b) {
totalBills++;
totalRevenue += b->getTotalAmount();
if (b->getIsPaid()) paidBills++;
}
}
rpt.printRevenueReport(totalRevenue, totalBills, paidBills);
}
else if (ch == 3)
{
cout << "\n Route-wise Student Count:" << endl;
cout << " ===============================" << endl;
for (int i = 0; i < routeCount; i++)
{
if (!routes[i]) continue;
int cnt = 0;
for (int j = 0; j < regCount; j++)
{
if (registrations[j]
&& registrations[j]->getRouteID() == routes[i]->getID()
&& registrations[j]->getStatus() == "approved")
cnt++;
}
string routeName = routes[i]->getStartPoint() + " to " + routes[i]->getEndPoint();
rpt.printRouteReport(routes[i]->getID(), routeName, cnt);
}
}
}
// Menu Runners
void TransportSystem::runStudentMenu(Student* s)
{
int choice;
do {
s->showMenu();
cin >> choice;
switch (choice)
{
case 1: viewAllRoutes();
break;
case 2: applyForTransport(s->getID());
break;
case 3: viewMyRegistration(s->getID());
break;
case 4: cancelRegistration(s->getID());
break;
case 5: viewMyBill(s->getID());
break;
case 6: cout << " Logged out." << endl;
break;
default: cout << " Invalid choice." << endl;
}
} while (choice != 6);
}
void TransportSystem::runAdminMenu(Admin* a)
{
int choice;
do {
a->showMenu();
cin >> choice;
switch (choice)
{
case 1: addVehicle();
break;
case 2: editVehicle();
break;
case 3: removeVehicle();
break;
case 4: addRoute();
break;
case 5: assignVehicleToRoute();
break;
case 6: viewAllApplications();
break;
case 7: approveOrRejectRequest();
break;
case 8: generateReports();
break;
case 9: applyLateFine();
break;
case 10: saveAllData();
break;
case 11: cout << " Logged out." << endl;
break;
default: cout << " Invalid choice." << endl;
}
} while (choice != 11);
}
void TransportSystem::run()
{
loadAllData();
int choice;
do {
cout << "\n ============================================" << endl;
cout << " University Transport Management System " << endl;
cout << " ============================================" << endl;
cout << " 1. Register as Student" << endl;
cout << " 2. Login" << endl;
cout << " 3. Exit" << endl;
cout << " Enter choice: ";
cin >> choice;
if (choice == 1)
{
registerStudent();
}
else if (choice == 2)
{
User* user = login();
if (user != nullptr)
{
if (user->getRole() == "admin")
runAdminMenu(dynamic_cast<Admin*>(user));
else
runStudentMenu(dynamic_cast<Student*>(user));
}
}
else if (choice == 3)
{
saveAllData();
cout << " Goodbye!" << endl;
}
else
{
cout << " Invalid choice." << endl;
}
} while (choice != 3);
}