-
Notifications
You must be signed in to change notification settings - Fork 0
/
oops_proj.cpp
456 lines (399 loc) · 13.1 KB
/
oops_proj.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
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
using namespace std;
class Movie {
public:
string movieName;
vector<string> showTimes;
map<string, vector<vector<string>>> availableSeats;
Movie() {}
Movie(string name, vector<string> times) {
movieName = name;
showTimes = times;
for (string time : times) {
vector<vector<string>> seats(5, vector<string>(5));
char rowLabel = 'A';
for (int row = 0; row < 5; ++row) {
for (int col = 0; col < 5; ++col) {
seats[row][col] = string(1, rowLabel) + to_string(col + 1);
}
rowLabel++;
}
availableSeats[time] = seats;
}
}
void displayMovieDetails() {
cout << "Movie Name: " << movieName << endl;
for (string time : showTimes) {
cout << "Show Time: " << time << " | Available Seats: \n";
vector<vector<string>>& seats = availableSeats[time];
for (int row = 0; row < seats.size(); ++row) {
for (int col = 0; col < seats[row].size(); ++col) {
if (seats[row][col] != "XX") {
cout << seats[row][col] << " ";
} else {
cout << "XX ";
}
}
cout << endl;
}
cout << endl;
}
}
// Book specific seats for a showtime
bool bookSeats(string time, vector<string> seatsToBook) {
vector<vector<string>>& seats = availableSeats[time];
bool allAvailable = true;
for (const string& seat : seatsToBook) {
bool seatFound = false;
for (int row = 0; row < seats.size(); ++row) {
for (int col = 0; col < seats[row].size(); ++col) {
if (seats[row][col] == seat) {
seatFound = true;
seats[row][col] = "XX";
break;
}
}
if (seatFound) break;
}
if (!seatFound) {
allAvailable = false;
cout << "Seat " << seat << " is already booked or invalid!" << endl;
}
}
return allAvailable;
}
void updateShowtime(string time, int totalSeats) {
int rows = totalSeats / 5;
vector<vector<string>> newSeats(rows, vector<string>(5));
char rowLabel = 'A';
for (int row = 0; row < rows; ++row) {
for (int col = 0; col < 5; ++col) {
newSeats[row][col] = string(1, rowLabel) + to_string(col + 1);
}
rowLabel++;
}
availableSeats[time] = newSeats;
}
};
class Customer {
public:
string customerName;
vector<pair<string, pair<string, vector<string>>>> bookings;
Customer() {}
Customer(string name) {
customerName = name;
}
void addBooking(string movie, string time, vector<string> seats) {
bookings.push_back(make_pair(movie, make_pair(time, seats)));
saveBookingToFile(movie, time, seats);
}
void saveBookingToFile(string movie, string time, vector<string> seats) {
ofstream file("booking_history.txt", ios::app);
if (file.is_open()) {
file << "Customer: " << customerName << "\n";
file << "Movie: " << movie << " | Time: " << time << " | Seats: ";
for (const string& seat : seats) {
file << seat << " ";
}
file << "\n\n";
file.close();
} else {
cout << "Error: Could not open booking history file!" << endl;
}
}
void displayBookingHistory() {
for (auto& booking : bookings) {
cout << "Movie: " << booking.first << " | Time: " << booking.second.first << " | Seats: ";
for (const string& seat : booking.second.second) {
cout << seat << " ";
}
cout << endl;
}
}
};
class User {
public:
string username;
string password;
bool login(string uname, string pass) {
return username == uname && password == pass;
}
};
vector<Movie> loadMovies() {
vector<Movie> movies;
ifstream file("movies.csv");
string line;
if (!file.is_open()) {
cout << "Error opening file!" << endl;
return movies;
}
getline(file, line);
while (getline(file, line)) {
stringstream ss(line);
string name, timesStr;
vector<string> times;
if (!getline(ss, name, ',')) continue;
if (!getline(ss, timesStr, ',')) continue;
stringstream timesStream(timesStr);
string time;
while (getline(timesStream, time, '|')) {
times.push_back(time);
}
movies.push_back(Movie(name, times));
}
file.close();
return movies;
}
vector<Customer> loadCustomers() {
vector<Customer> customers;
ifstream file("customers.txt");
string line;
while (getline(file, line)) {
Customer customer;
customer.customerName = line;
customers.push_back(customer);
}
file.close();
return customers;
}
void saveCustomers(const vector<Customer>& customers) {
ofstream file("customers.txt");
for (const Customer& customer : customers) {
file << customer.customerName << endl;
}
file.close();
}
void saveAdmin(const User& admin) {
ofstream file("admin.txt");
file << admin.username << "," << admin.password << endl;
file.close();
}
User loadAdmin() {
User admin;
ifstream file("admin.txt");
string line;
if (getline(file, line)) {
size_t pos = line.find(",");
admin.username = line.substr(0, pos);
admin.password = line.substr(pos + 1);
}
file.close();
return admin;
}
void adminMode(vector<Movie>& movies) {
int choice;
do {
cout << "Admin Menu:\n1. Add Movie\n2. Update Seats\n3. Display Movies\n4. Logout\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
string name;
vector<string> times;
cout << "Enter movie name: ";
cin.ignore();
getline(cin, name);
cout << "Enter number of shows: ";
int numShows;
cin >> numShows;
for (int i = 0; i < numShows; i++) {
string time;
cout << "Enter show time " << i + 1 << ": ";
cin >> time;
times.push_back(time);
}
movies.push_back(Movie(name, times));
ofstream file("movies.csv", ios::app);
if (file.is_open()) {
file << name << ",";
for (size_t i = 0; i < times.size(); ++i) {
file << times[i];
if (i < times.size() - 1) file << "|";
}
file << ",25";
file << "\n";
file.close();
cout << "Movie added successfully\n";
} else {
cout << "Error: Could not open movies.csv for updating!\n";
}
break;
}
case 2: {
string name, time;
int seats;
cout << "Enter movie name: ";
cin.ignore();
getline(cin, name);
for (Movie& movie : movies) {
if (movie.movieName == name) {
cout << "Enter show time to update: ";
cin >> time;
cout << "Enter new available seats: ";
cin >> seats;
movie.updateShowtime(time, seats);
break;
}
}
break;
}
case 3:
for (Movie& movie : movies) {
movie.displayMovieDetails();
}
break;
case 4:
cout << "Logging out...\n";
break;
default:
cout << "Invalid choice!\n";
break;
}
} while (choice != 4);
}
void customerMode(vector<Movie>& movies, vector<Customer>& customers) {
string name;
cout << "Enter your name: ";
cin.ignore();
getline(cin, name);
// Find or create the customer
Customer* currentCustomer = nullptr;
for (Customer& customer : customers) {
if (customer.customerName == name) {
currentCustomer = &customer;
break;
}
}
if (!currentCustomer) {
// New customer
customers.push_back(Customer(name));
currentCustomer = &customers.back();
}
int choice;
do {
cout << "Customer Menu:\n1. View Movies\n2. Book Ticket\n3. View Booking History\n4. Logout\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
for (Movie& movie : movies) {
movie.displayMovieDetails();
}
break;
case 2: {
// Display available movies and showtimes
cout << "Available movies:\n";
for (int i = 0; i < movies.size(); ++i) {
cout << i + 1 << ". " << movies[i].movieName << endl;
}
int movieIndex;
cout << "Enter movie number: ";
cin >> movieIndex;
--movieIndex;
if (movieIndex < 0 || movieIndex >= movies.size()) {
cout << "Invalid movie selection!\n";
break;
}
Movie& selectedMovie = movies[movieIndex];
cout << "Available showtimes:\n";
for (int i = 0; i < selectedMovie.showTimes.size(); ++i) {
cout << i + 1 << ". " << selectedMovie.showTimes[i] << endl;
}
int showtimeIndex;
cout << "Enter showtime number: ";
cin >> showtimeIndex;
--showtimeIndex;
if (showtimeIndex < 0 || showtimeIndex >= selectedMovie.showTimes.size()) {
cout << "Invalid showtime selection!\n";
break;
}
string selectedTime = selectedMovie.showTimes[showtimeIndex];
vector<vector<string>>& seats = selectedMovie.availableSeats[selectedTime];
cout << "Available seats (XX = booked):\n";
for (int row = 0; row < seats.size(); ++row) {
for (int col = 0; col < seats[row].size(); ++col) {
cout << seats[row][col] << " ";
}
cout << endl;
}
int numSeats;
cout << "Enter number of seats to book: ";
cin >> numSeats;
vector<string> seatsToBook(numSeats);
cout << "Enter seat numbers (e.g., A1, B2): ";
for (int i = 0; i < numSeats; ++i) {
cin >> seatsToBook[i];
}
if (selectedMovie.bookSeats(selectedTime, seatsToBook)) {
cout << "Seats booked successfully!\n";
currentCustomer->addBooking(selectedMovie.movieName, selectedTime, seatsToBook);
} else {
cout << "Some seats were unavailable. Please try again.\n";
}
break;
}
case 3: {
cout << "Booking History for " << name << ":\n";
currentCustomer->displayBookingHistory();
break;
}
case 4:
cout << "Logging out...\n";
break;
default:
cout << "Invalid choice!\n";
break;
}
} while (choice != 4);
}
int main() {
User admin = loadAdmin();
vector<Movie> movies = loadMovies();
vector<Customer> customers = loadCustomers();
string inputUsername, inputPassword;
int loginAttempts = 0;
int choice;
do {
cout << "Welcome to the Movie Ticket Booking System\n";
cout << "1. Admin Login\n2. Customer Mode\n3. Exit\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
bool loggedIn = false;
do {
cout << "Enter admin username: ";
cin >> inputUsername;
cout << "Enter admin password: ";
cin >> inputPassword;
if (admin.login(inputUsername, inputPassword)) {
loggedIn = true;
adminMode(movies);
} else {
cout << "Incorrect username or password! Try again.\n";
loginAttempts++;
}
} while (!loggedIn && loginAttempts < 3);
if (!loggedIn) {
cout << "Too many failed attempts. Exiting.\n";
return 0;
}
break;
}
case 2:
customerMode(movies, customers);
break;
case 3:
cout << "Exiting the system.\n";
break;
default:
cout << "Invalid choice!\n";
break;
}
} while (choice != 3);
saveCustomers(customers);
saveAdmin(admin);
return 0;
}