-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicketReservation.cpp
356 lines (311 loc) · 10.3 KB
/
TicketReservation.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
/**
* @class Event
* @brief Represents an event with methods for booking and cancellation of seats.
*/
class Event {
public:
int eventID; ///< Unique event ID
string eventName; ///< Name of the event
int totalSeats; ///< Total number of seats available
int availableSeats; ///< Number of seats currently available
Event() : eventID(0), totalSeats(0), availableSeats(0) {}
/**
* @brief Attempts to book a seat for the event.
* @return True if booking is successful, otherwise false.
*/
bool bookSeat() {
if (availableSeats > 0) {
availableSeats--;
return true;
}
return false;
}
/**
* @brief Attempts to cancel a seat for the event.
* @return True if cancellation is successful, otherwise false.
*/
bool cancelSeat() {
if (availableSeats < totalSeats) {
availableSeats++;
return true;
}
return false;
}
};
/**
* @class Reservation
* @brief Manages reservations made by users for events.
*/
class Reservation {
public:
int reservationID; ///< Unique reservation ID
string userID; ///< ID of the user who made the reservation
int eventID; ///< ID of the event for which the reservation was made
Reservation() : reservationID(0), eventID(0) {}
/**
* @brief Generates a unique reservation ID by checking the last used ID.
* @return A new unique reservation ID.
*/
int generateReservationID() {
ifstream reservationFile("reservations.txt");
int lastID = 0;
string line;
// Get the last reservation ID from the file
while (getline(reservationFile, line)) {
stringstream ss(line);
int idTemp;
ss >> idTemp;
lastID = idTemp;
}
reservationFile.close();
return lastID + 1;
}
/**
* @brief Cancels the reservation and frees up a seat in the event.
* @return True if cancellation is successful, otherwise false.
*/
bool cancelSeat() {
Event event;
event.eventID = eventID;
return event.cancelSeat();
}
};
/**
* @class User
* @brief Handles user operations such as registration, login, booking, and cancellation of tickets.
*/
class User {
public:
string username; ///< Stores the username of the user
string password; ///< Stores the password of the user
vector<int> reservationIDs; ///< Stores the list of reservation IDs associated with the user
User() : username(""), password("") {}
/**
* @brief Constructor to initialize a User object.
* @param user The username of the user.
* @param pass The password of the user.
*/
User(string user, string pass) : username(user), password(pass) {}
/**
* @brief Registers a new user by saving the username and hashed password to a file.
*/
void registerUser() {
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
// Check if the username is already taken
if (!isUsernameAvailable(username)) {
cout << "Username already taken. Please choose another one." << endl;
return;
}
// Save the new user's data to the users.txt file
ofstream userFile("users.txt", ios::app);
if (userFile.is_open()) {
userFile << username << " " << hashPassword(password) << endl;
userFile.close();
cout << "User registered successfully!" << endl;
} else {
cout << "Error: Unable to open file." << endl;
}
}
/**
* @brief Logs in a user by checking the entered credentials against the stored data.
* @return True if login is successful, otherwise false.
*/
bool loginUser() {
string inputUsername, inputPassword;
cout << "Enter username: ";
cin >> inputUsername;
cout << "Enter password: ";
cin >> inputPassword;
// Check the entered credentials against the stored data
ifstream userFile("users.txt");
string fileUsername, filePassword;
while (userFile >> fileUsername >> filePassword) {
if (fileUsername == inputUsername && filePassword == hashPassword(inputPassword)) {
username = inputUsername;
return true;
}
}
cout << "Invalid username or password." << endl;
return false;
}
/**
* @brief Displays the list of reservations made by the user.
*/
void viewTickets() const {
if (reservationIDs.empty()) {
cout << "No reservations found." << endl;
return;
}
cout << "Your reservations:" << endl;
for (int id : reservationIDs) {
cout << "Reservation ID: " << id << endl;
}
}
/**
* @brief Books a ticket for the user by associating an event with the user.
*/
void bookTicket() {
int eventID;
cout << "Enter event ID: ";
cin >> eventID;
Event event;
event.eventID = eventID;
if (event.bookSeat()) {
Reservation reservation;
reservation.reservationID = reservation.generateReservationID();
reservation.userID = username;
reservation.eventID = eventID;
// Save the reservation details to the reservations.txt file
ofstream reservationFile("reservations.txt", ios::app);
reservationFile << reservation.reservationID << " " << reservation.userID << " " << reservation.eventID << endl;
reservationFile.close();
cout << "Ticket booked successfully!" << endl;
reservationIDs.push_back(reservation.reservationID);
} else {
cout << "Event is fully booked or does not exist." << endl;
}
}
/**
* @brief Cancels a ticket by removing the reservation from the user's list.
*/
void cancelTicket() {
int reservationID;
cout << "Enter reservation ID: ";
cin >> reservationID;
Reservation reservation;
reservation.reservationID = reservationID;
if (reservation.cancelSeat()) {
auto it = find(reservationIDs.begin(), reservationIDs.end(), reservationID);
if (it != reservationIDs.end()) {
reservationIDs.erase(it);
}
cout << "Ticket cancelled successfully!" << endl;
} else {
cout << "Reservation does not exist." << endl;
}
}
private:
/**
* @brief Checks if the username is available by comparing it against stored usernames.
* @param user The username to check.
* @return True if the username is available, otherwise false.
*/
bool isUsernameAvailable(const string& user) {
ifstream userFile("users.txt");
string fileUsername, filePassword;
while (userFile >> fileUsername >> filePassword) {
if (fileUsername == user) {
return false;
}
}
return true;
}
/**
* @brief Hashes the user's password for secure storage.
* @param pass The password to hash.
* @return The hashed password.
*/
string hashPassword(const string& pass) {
string hash = "";
for (char c : pass) {
hash += to_string(int(c) * 5); // Simple hash function (for demonstration purposes)
}
return hash;
}
};
/**
* @class Admin
* @brief Handles admin operations such as adding, viewing, and removing events.
*/
class Admin {
public:
string adminUsername; ///< Admin username for login
string adminPassword; ///< Admin password for login
Admin() : adminUsername(""), adminPassword("") {}
/**
* @brief Constructor to initialize an Admin object.
* @param user The admin username.
* @param pass The admin password.
*/
Admin(string user, string pass) : adminUsername(user), adminPassword(pass) {}
/**
* @brief Logs in the admin by checking credentials.
* @return True if login is successful, otherwise false.
*/
bool loginAdmin() {
string inputUsername, inputPassword;
cout << "Enter admin username: ";
cin >> inputUsername;
cout << "Enter admin password: ";
cin >> inputPassword;
// Check credentials
if (inputUsername == adminUsername && inputPassword == adminPassword) {
return true;
}
cout << "Invalid admin credentials." << endl;
return false;
}
/**
* @brief Adds a new event to the system.
*/
void addEvent() {
Event event;
cout << "Enter event name: ";
cin >> event.eventName;
cout << "Enter total seats: ";
cin >> event.totalSeats;
event.availableSeats = event.totalSeats;
event.eventID = generateEventID();
// Save the event details to the events.txt file
ofstream eventFile("events.txt", ios::app);
eventFile << event.eventID << " " << event.eventName << " " << event.totalSeats << " " << event.availableSeats << endl;
eventFile.close();
cout << "Event added successfully!" << endl;
}
private:
/**
* @brief Generates a unique event ID.
* @return A new unique event ID.
*/
int generateEventID() {
ifstream eventFile("events.txt");
int lastID = 0;
string line;
// Get the last event ID from the file
while (getline(eventFile, line)) {
stringstream ss(line);
int idTemp;
ss >> idTemp;
lastID = idTemp;
}
eventFile.close();
return lastID + 1;
}
};
int main() {
// Example usage
User user("exampleUser", "examplePass");
Admin admin("adminUser", "adminPass");
// Registration and login example
user.registerUser();
if (user.loginUser()) {
cout << "Login successful!" << endl;
}
// Admin adding an event example
if (admin.loginAdmin()) {
admin.addEvent();
}
// Booking and viewing tickets
user.bookTicket();
user.viewTickets();
return 0;
}