Repository navigation
Expand file tree
/
Copy pathSmartRide.cpp
More file actions
253 lines (221 loc) · 7.26 KB
/
Copy pathSmartRide.cpp
File metadata and controls
253 lines (221 loc) · 7.26 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
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Ride structure (doubly linked list node for storing ride details)
struct Ride {
string rideID;
string pickupLocation;
string dropoffLocation;
Ride* next = nullptr; // pointer to the next ride
Ride* prev = nullptr; // pointer to the previous ride
Ride(string id, string pickup, string dropoff)
: rideID(id), pickupLocation(pickup), dropoffLocation(dropoff) {}
};
// Class to manage ride history as a doubly linked list
class RideHistory {
private:
Ride* head; // pointer to the first ride
Ride* tail; // pointer to the last ride
public:
RideHistory() : head(nullptr), tail(nullptr) {} // initialize empty list
// Add a new ride to the history
void addRide(string id, string pickup, string dropoff) {
Ride* newRide = new Ride(id, pickup, dropoff);
if (!head) { // if the list is empty
head = tail = newRide;
} else { // append to the end of the list
tail->next = newRide;
newRide->prev = tail;
tail = newRide;
}
cout << "Ride added: " << id << endl;
}
// Display ride history in backward direction (most recent to oldest)
void displayHistoryBackward() {
Ride* current = tail;
cout << "Ride History (Backward):\n";
while (current) {
cout << "Ride ID: " << current->rideID
<< ", Pickup: " << current->pickupLocation
<< ", Dropoff: " << current->dropoffLocation << endl;
current = current->prev;
}
}
};
// User structure (including ride history)
struct User {
string username;
string password;
bool hasActiveRide = false;
RideHistory rideHistory; // ride history for the user
};
// Driver structure
struct Driver {
string username;
string password;
bool isAvailable = true;
};
// Hash Table Implementation (for storing users and drivers)
template <typename T>
class HashTable {
private:
vector<vector<T>> table;
int size;
int hashFunction(string key) {
int hash = 0;
for (char ch : key) hash += ch;
return hash % size;
}
public:
HashTable(int tableSize) : size(tableSize) {
table.resize(size);
}
void insert(string key, T item) {
int idx = hashFunction(key);
table[idx].push_back(item);
}
T* search(string key, string password) {
int idx = hashFunction(key);
for (auto &item : table[idx]) {
if (item.username == key && item.password == password) {
return &item;
}
}
return nullptr;
}
};
// Main SmartRide System Class
class SmartRide {
private:
HashTable<User> users;
HashTable<Driver> drivers;
public:
SmartRide() : users(10), drivers(10) {}
void registerUser(string username, string password) {
users.insert(username, {username, password});
cout << "User " << username << " registered successfully.\n";
}
void registerDriver(string username, string password) {
drivers.insert(username, {username, password});
cout << "Driver " << username << " registered successfully.\n";
}
User* userLogin(string username, string password) {
User* user = users.search(username, password);
if (user) {
cout << "User " << username << " logged in successfully.\n";
return user;
} else {
cout << "Invalid credentials for user.\n";
return nullptr;
}
}
Driver* driverLogin(string username, string password) {
Driver* driver = drivers.search(username, password);
if (driver) {
cout << "Driver " << username << " logged in successfully.\n";
return driver;
} else {
cout << "Invalid credentials for driver.\n";
return nullptr;
}
}
};
// Main CLI driver code
int main() {
SmartRide app;
string username, password;
bool running = true;
while (running) {
cout << "\n=== SMART RIDE CLI ===\n";
cout << "1. Register User\n";
cout << "2. Register Driver\n";
cout << "3. User Login\n";
cout << "4. Driver Login\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
int choice;
cin >> choice;
switch (choice) {
case 1:
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
app.registerUser(username, password);
break;
case 2:
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
app.registerDriver(username, password);
break;
case 3: {
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
User* user = app.userLogin(username, password);
if (user) {
bool userMenu = true;
while (userMenu) {
cout << "\n--- USER MENU ---\n";
cout << "1. Request Ride\n";
cout << "2. View Ride History\n";
cout << "3. Logout\n";
cout << "Enter your choice: ";
int userChoice;
cin >> userChoice;
switch (userChoice) {
case 1:
if (user->hasActiveRide) {
cout << "You already have an active ride.\n";
} else {
string rideID, pickup, dropoff;
cout << "Enter ride ID: ";
cin >> rideID;
cout << "Enter pickup location: ";
cin >> pickup;
cout << "Enter dropoff location: ";
cin >> dropoff;
user->rideHistory.addRide(rideID, pickup, dropoff);
user->hasActiveRide = true;
cout << "Ride requested successfully.\n";
}
break;
case 2:
user->rideHistory.displayHistoryBackward();
break;
case 3:
userMenu = false;
break;
default:
cout << "Invalid choice.\n";
}
}
}
break;
}
case 4: {
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
Driver* driver = app.driverLogin(username, password);
if (driver) {
cout << "Welcome driver " << username << "!\n";
// Additional driver menu options can be added here
}
break;
}
case 5:
running = false;
break;
default:
cout << "Invalid choice. Try again.\n";
}
}
cout << "Exiting SmartRide. Goodbye!\n";
return 0;
}