-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
152 lines (130 loc) · 3.94 KB
/
main.cpp
File metadata and controls
152 lines (130 loc) · 3.94 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
#include "common.h"
#include "RouteGraph.h"
#include "BusScheduler.h"
#include "DataLoader.h"
#include "BookingManager.h"
#include <iostream>
#include <string>
using namespace std;
// Function to print the main menu
void printMenu()
{
cout << "\n======= Smart Bus System Menu =======" << endl;
cout << " 1. Plan a New Journey" << endl;
cout << " 2. View an Existing Ticket" << endl;
cout << " 3. Cancel a Ticket" << endl;
cout << " 4. List All Available Stops" << endl;
cout << " 5. Quit" << endl;
cout << "=======================================" << endl;
cout << "Enter your choice (1-5): ";
}
// Function to handle planning and booking
void planAndBookJourney(RouteGraph &graph, BusScheduler &scheduler, BookingManager &booker)
{
string startStop, endStop, startTimeStr, passengerName, choice;
graph.printAllStops();
cout << "\nEnter your starting stop: ";
getline(cin, startStop);
cout << "Enter your destination stop: ";
getline(cin, endStop);
cout << "Enter desired departure time (HH:MM, e.g., 09:30): ";
getline(cin, startTimeStr);
int startTimeMinutes = timeToMinutes(startTimeStr); // Validate time format
if (startTimeMinutes == -1)
{
cout << "Error: Invalid time format. Please use HH:MM." << endl;
return;
}
// Find Optimal Path -> fastest travel time
RoutePlan optimalPath = graph.findShortestPath(startStop, endStop);
if (optimalPath.totalTime == -1)
{
return;
}
// Find Actual Scheduled Journey
JourneyPlan finalJourney = scheduler.findJourney(optimalPath, startTimeMinutes);
if (finalJourney.finalArrivalTime == -1)
{
cout << "Tip: Try departing at a different time." << endl;
return;
}
// Print the plan for user confirmation
scheduler.printJourneyPlan(finalJourney);
// Ask user to book
cout << "\nDo you want to book this journey? (y/n): ";
getline(cin, choice);
if (normalizeString(choice) == "y")
{
cout << "Please enter passenger name: ";
getline(cin, passengerName);
// This is where we use the BookingManager
// Add request to the queue
booker.requestTicket(passengerName, finalJourney);
// Process from queue, save to map, and save to CSV
booker.processNextBooking();
}
else
{
cout << "Booking canceled. Returning to main menu." << endl;
}
}
// view a ticket
void viewTicket(BookingManager &booker)
{
string ticketId;
cout << "\nEnter your Ticket ID (e.g., T-1001): ";
getline(cin, ticketId);
booker.viewTicket(ticketId);
}
// cancel a ticket
void cancelTicket(BookingManager &booker)
{
string ticketId;
cout << "\nEnter Ticket ID to cancel (e.g., T-1001): ";
getline(cin, ticketId);
booker.cancelTicket(ticketId);
}
int main()
{
// --- 1. System Initialization ---
cout << "Initializing Smart Bus System..." << endl;
RouteGraph graph;
BusScheduler scheduler(&graph);
BookingManager booker("tickets.csv");
// using the DataLoader to populate the system
DataLoader::loadNetworkData(graph);
DataLoader::loadBusSchedules(scheduler, graph);
cout << "\n--- Welcome to the Smart Bus Route Planner ---" << endl;
string choice;
while (true)
{
printMenu();
getline(cin, choice);
if (choice == "1")
{
planAndBookJourney(graph, scheduler, booker);
}
else if (choice == "2")
{
viewTicket(booker);
}
else if (choice == "3")
{
cancelTicket(booker);
}
else if (choice == "4")
{
graph.printAllStops();
}
else if (choice == "5")
{
cout << "Thank you for using the Smart Bus Planner!" << endl;
break;
}
else
{
cout << "Invalid choice. Please enter a number from 1 to 5." << endl;
}
}
return 0;
}