-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
678 lines (586 loc) · 21.1 KB
/
main.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
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
/*
main.cpp
Author: M00776456
Created: 23/12/21
Updated: 13/01/22
*/
#include "main.h"
#include "events.h"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <algorithm>
// filename
const std::string FILENAME = "file.txt";
int liveSize;
int standUpSize;
int filmSize;
int eventChoice;
details* liveEvent;
details* standUpEvent;
details* filmEvent;
std::vector <Event*> events;
int main()
{
// check if the file exists
try{
std::ifstream myFile(FILENAME);
if(myFile.fail()){
// throw an exception when the file does not exist
throw 404;
} else{
determineSize();
// create dynamically allocated vectors of user defined datatype
liveEvent = new details[liveSize];
standUpEvent = new details[standUpSize];
filmEvent = new details[filmSize];
// if exists, call function loadFile()
loadFile();
}
}
// Call function addData if an error occurs in the try block
catch (...) {
addData();
}
mainMenu();
}
int mainMenu()
{
int option = 0;
// sort the seat track by ascending order
for (int i = 0; i < standUpSize; i++){
std::sort(standUpEvent[i].seatTrack.begin(),
standUpEvent[i].seatTrack.end());
}
// keep looping until 5 entered
do {
std::cout << std::endl << "******** Main Menu ********" << std::endl;
std::cout << "1: Live event" << std::endl;
std::cout << "2. Stand Up" << std::endl;
std::cout << "3. Film " << std::endl;
std::cout << "4. List details for all events " << std::endl;
std::cout << "5. Quit " << std::endl;
std::cout << std::endl << "Enter your choice: " ;
std::cin >> option;
// Live event
if (option == 1) {
// clear the screen
std::cout << "\x1B[2J\x1B[H";
std::cout << std::endl << "******** Live Event ********"
<< std::endl;
menuChoice(liveSize, liveEvent);
// Stand Up event
} else if (option == 2) {
std::cout << "\x1B[2J\x1B[H";
std::cout << std::endl << "******** StandUp Event ********"
<< std::endl;
menuChoice(standUpSize, standUpEvent);
// Film Event
} else if (option == 3) {
std::cout << "\x1B[2J\x1B[H";
std::cout << std::endl << "******** Film Event ********"
<< std::endl;
menuChoice(filmSize, filmEvent);
// List details for all events
} else if (option == 4) {
std::cout << "\x1B[2J\x1B[H";
std::cout << std::endl << "List details for all events"
<< std::endl;
listAll();
// Exit
} else if (option == 5) {
quit();
} else if (option != 5) {
std::cout << std::endl
<< "Invalid choice. Please enter 1,2,3,4 or 5"
<< std::endl;
}
} while (option != 5);
return option;
}
void menuChoice(int vectorSize, details* event){
// print the shows of live event
for (int i = 0; i < vectorSize; i++){
std::cout << (i+1) << ": ";
printName(&event[i]);
}
std::cout << (vectorSize + 1) << ": Back to main menu" << std::endl;
do {
std::cout << "Choose your event: ";
std::cin >> eventChoice;
if (eventChoice < 1 || eventChoice > (vectorSize + 1)) {
std::cout << "Invalid choice." << std::endl;
}
} while (eventChoice < 1 || eventChoice > (vectorSize + 1));
if (eventChoice == (vectorSize + 1)){
mainMenu();
} else {
// dynamically allocated variable of type Event
Event* eventClass;
if (event->ref == 1){
eventClass = new Live(&event[eventChoice - 1]);
live(eventClass);
} else if (event->ref == 2){
eventClass = new StandUp(&event[eventChoice - 1]);
standUp(eventClass);
} else {
eventClass = new Film(&event[eventChoice - 1]);
film(eventClass);
}
// clear the memory
delete eventClass;
eventClass = NULL;
mainMenu();
}
}
void listAll(){
// clear the event vector
events.clear();
// Append the details of each event vector to a main vector
for (int i = 0; i < liveSize; i++){
events.push_back(new Live(&liveEvent[i]));
}
for (int i = 0; i < standUpSize; i++){
events.push_back(new StandUp(&standUpEvent[i]));
}
for (int i = 0; i < filmSize; i++){
events.push_back(new Film(&filmEvent[i]));
}
int ref = 0;
// loop through dynamically allocated vector array
for(size_t i = 0; i < events.size(); i++){
// display the details
if (ref == events[i]->getRef()){
std::cout << events[i]->description() << std::endl;
// std::cout << std::endl;
} else {
std::cout << std::endl;
ref = events[i]->getRef();
if (ref == 1) {
std::cout << "******** Live Event ********"
<< std::endl;
} else if (ref == 2){
std::cout << "******** Stand-Up Event ********"
<< std::endl;
} else {
std::cout << "******** Film Event ********"
<< std::endl;
}
std::cout << events[i]->description() << std::endl;
}
}
mainMenu();
}
void quit(){
std::cout << "Bye Bye" << std::endl;
// save data to the file
saveData();
// clear the heap
for (Event* e: events){
delete e;
}
events.clear();
delete[] liveEvent;
delete[] standUpEvent;
delete[] filmEvent;
liveEvent = NULL;
standUpEvent = NULL;
filmEvent = NULL;
exit(0);
}
int menu(){
int option;
// prompt user to make a choice
do {
std::cout << std::endl << "******** Menu ********" << std::endl;
std::cout << "1: Booking" << std::endl;
std::cout << "2. Cancel Booking" << std::endl;
std::cout << "3. List Details " << std::endl;
std::cout << "4. Back to menu " << std::endl;
std::cout << std::endl << "Enter your choice: " << std::endl;
std::cin >> option;
if (option < 1 || option > 4) {
std::cout << std::endl
<< "Invalid choice. Please enter 1,2,3 or 4 "
<< std::endl;
}
} while (option < 1 || option > 4);
return option;
}
void booking(Event* details){
int seatToBeBooked = 0;
char choice;
std::cout << "Theatre maximum capacity: " << details->getMaxCapacity()
<< std::endl;
std::cout << "Number of seats available for booking: "
<< details->getAvailableSeat() << std::endl;
// check if available seat is less than maximum seating capacity
if (details->getAvailableSeat() != 0){
std::cout << "You can proceed with booking " << std::endl;
do {
std::cout << std::endl << "How much seat to book? "
<< std::endl;
std::cin >> seatToBeBooked;
if ((details->getAvailableSeat() - seatToBeBooked) < 0) {
std::cout << "Error. Maximum Seat Capacity reached."
<< std::endl;
}
} while ((details->getAvailableSeat() - seatToBeBooked) < 0);
do {
std::cout << "Are you sure to book " << seatToBeBooked
<< " seats? [Y/N]: ";
std::cin >> choice;
if (choice != 'Y' && choice != 'N')
{
std::cout << "Wrong input. Enter Y (Yes) or N (No)"
<< std::endl;
}
} while (choice != 'Y' && choice != 'N' );
if (choice == 'Y'){
details->booking(seatToBeBooked);
std::cout << seatToBeBooked << " seats booked SUCCESSFULLY. "
<< std::endl;
}
} else {
std::cout << "No more seats available " << std::endl;
}
mainMenu();
}
void cancelBooking(Event* details)
{
int seatsToCancel = 0;
char choice;
std::cout << "Number of seats already booked: "
<< (details->getMaxCapacity() - details->getAvailableSeat())
<< std::endl;
if (details->getAvailableSeat() != details->getMaxCapacity()){
std::cout << "You can proceed with cancellation" << std::endl;
do {
std::cout << "How many booking to cancel? " << std::endl;
std::cin >> seatsToCancel;
if (( details->getAvailableSeat() + seatsToCancel) >
details->getMaxCapacity()){
std::cout << "Error. Cannot proceed with cancellation"
<< std::endl;
}
} while ((details->getAvailableSeat() + seatsToCancel) >
details->getMaxCapacity());
do {
std::cout << "Are you sure to cancel booking " << seatsToCancel
<< " seats? [Y/N]: ";
std::cin >> choice;
if (choice != 'Y' && choice != 'N')
{
std::cout << "Wrong input. Enter Y (Yes) or N (No)"
<< std::endl;
}
} while (choice != 'Y' && choice != 'N');
if (choice == 'Y'){
details->cancel(seatsToCancel);
std::cout << seatsToCancel << " seats unbooked SUCCESSFULLY. "
<< std::endl;
}
} else {
std::cout << "There is no booking to cancel" << std::endl;
}
mainMenu();
}
void live(Event* liveDetails)
{
int option;
do {
std::cout << "You choose: " << liveDetails->getName() << std::endl;
option = menu();
if (option == 1) {
std::cout << std::endl << "Booking for Live Event" << std::endl;
booking(liveDetails);
} else if (option == 2){
std::cout << std::endl << "Cancel/Refund Booking" << std::endl;
cancelBooking(liveDetails);
} else if (option == 3){
std::cout << std::endl
<< "List details and availability for Live Event"
<< std::endl;
std::cout << liveDetails->description() << std::endl;
std::cout << std::endl;
} else if (option == 4){
mainMenu();
}
if (option < 1 || option > 4) {
std::cout << std::endl
<< "Invalid choice. Please enter 1,2,3 or 4 "
<< std::endl;
}
} while (option < 1 || option > 4);
}
void standUp(Event* standUpDetails)
{
int option;
do{
std::cout << "You choose: " << standUpDetails->getName() << std::endl;
option = menu();
if (option == 1) {
std::cout << std::endl << "Booking for Standing Event"
<< std::endl;
booking(standUpDetails);
} else if (option == 2){
std::cout << std::endl << "Cancel/Refund Booking" << std::endl;
cancelBooking(standUpDetails);
} else if (option == 3){
std::cout << std::endl
<< "List details and availability for Standing Event"
<< std::endl;
std::cout << standUpDetails->description();
std::cout << std::endl;
} else if (option ==4 ){
mainMenu();
}
if (option < 1 || option > 4) {
std::cout << std::endl
<< "Invalid choice. Please enter 1,2,3 or 4 "
<< std::endl;
}
} while (option < 1 || option > 4);
}
void film(Event* filmDetails)
{
int option;
do {
std::cout << "You choose: " << filmDetails->getName() << std::endl;
option = menu();
if (option == 1) {
std::cout << std::endl << "Booking for film Event" << std::endl;
booking(filmDetails);
} else if (option == 2){
std::cout << std::endl << "Cancel/Refund Booking" << std::endl;
cancelBooking(filmDetails);
} else if (option == 3){
std::cout << std::endl
<< "List details and availability for film Event"
<< std::endl;
std::cout << filmDetails->description();
std::cout << std::endl;
} else {
mainMenu();
}
if (option < 1 || option > 4) {
std::cout << std::endl
<< "Invalid choice. Please enter 1,2,3 or 4 "
<< std::endl;
}
} while (option < 1 || option > 4);
}
void loadFile(){
std::ifstream myFile(FILENAME);
std::string myText;
int count = 0;
int ref;
int tmpRef = 1;
// loop until last line
while (std::getline(myFile, myText)){
// check if myText matches the substring
if (myText.find("Ref: ") != std::string::npos){
// append the string after the substring to the variable ref
ref = std::stoi(myText.substr(myText.find("Ref: ") + 5));
}
// check if ref is not same as tmpRef
if(ref != tmpRef){
// append zero to count
count = 0;
// append new ref to tmpRef
tmpRef = ref;
}
// Event reference
if (ref == 1){
liveEvent[count].ref = ref;
if (myText.find("Name: ") != std::string::npos){
liveEvent[count].name =
(myText.substr(myText.find("Name: ") + 6));
}
if (myText.find("SeatCapacity: ") != std::string::npos){
liveEvent[count].seatCapacity =
std::stoi(myText.substr(myText.find("SeatCapacity: ") + 14));
}
if (myText.find("AvailableSeat: ") != std::string::npos){
liveEvent[count].availableSeat =
std::stoi(myText.substr(myText.find("AvailableSeat: ") + 15));
}
// StandUp Reference
} else if (ref == 2){
standUpEvent[count].ref = ref;
if (myText.find("Name: ") != std::string::npos){
standUpEvent[count].name =
(myText.substr(myText.find("Name: ") + 6));
}
if (myText.find("SeatCapacity: ") != std::string::npos){
standUpEvent[count].seatCapacity =
std::stoi(myText.substr(myText.find("SeatCapacity: ") + 14));
}
if (myText.find("AvailableSeat: ") != std::string::npos){
standUpEvent[count].availableSeat =
std::stoi(myText.substr(myText.find("AvailableSeat: ") + 15));
}
if (myText.find("SeatTrack: ") != std::string::npos){
int tmpTrack = std::stoi(myText.substr(
myText.find("SeatTrack: ")
+ 11));
standUpEvent[count].seatTrack.push_back(tmpTrack);
}
// Film reference
} else if (ref == 3){
filmEvent[count].ref = ref;
if (myText.find("Name: ") != std::string::npos){
filmEvent[count].name = (myText.substr(myText.find("Name: ")
+ 6));
}
if (myText.find("AvailableSeat: ") != std::string::npos){
filmEvent[count].availableSeat = std::stoi(myText.substr(
myText.find("AvailableSeat: ")
+ 15));
}
if (myText.find("SeatCapacity: ") != std::string::npos){
filmEvent[count].seatCapacity = std::stoi(myText.substr(
myText.find("SeatCapacity: ")
+ 14));
}
if (myText.find("FilmType: ") != std::string::npos){
filmEvent[count].filmType = (myText.substr(
myText.find("FilmType: ")
+ 10));
}
}
// check if myText is empty
if (myText.empty()){
// increment count
count += 1;
}
}
// close the file
myFile.close();
}
void addData(){
std::ofstream MyFile(FILENAME);
liveSize = 3;
standUpSize = 2;
filmSize = 2;
// create dynamically allocated vectors of user defined datatype
liveEvent = new details[liveSize];
standUpEvent = new details[standUpSize];
filmEvent = new details[filmSize];
liveEvent[0].ref = 1;
liveEvent[0].name = "TomorrowIsland";
liveEvent[0].availableSeat = 300;
liveEvent[0].seatCapacity = 300;
liveEvent[1].ref = 1;
liveEvent[1].name = "Major Lazer";
liveEvent[1].availableSeat = 300;
liveEvent[1].seatCapacity = 300;
liveEvent[2].ref = 1;
liveEvent[2].name = "Artbat";
liveEvent[2].availableSeat = 300;
liveEvent[2].seatCapacity = 300;
standUpEvent[0].ref = 2;
standUpEvent[0].name = "Komiko";
standUpEvent[0].availableSeat = 200;
standUpEvent[0].seatCapacity = 200;
standUpEvent[0].seatTrack = {};
standUpEvent[1].ref = 2;
standUpEvent[1].name = "Comedy Show";
standUpEvent[1].availableSeat = 200;
standUpEvent[1].seatCapacity = 200;
standUpEvent[1].seatTrack = {};
filmEvent[0].ref = 3;
filmEvent[0].name = "Spiderman: Work From Home";
filmEvent[0].availableSeat = 200;
filmEvent[0].seatCapacity = 200;
filmEvent[0].filmType = "3D";
filmEvent[1].ref = 3;
filmEvent[1].name = "Avengers: Age of Covid";
filmEvent[1].availableSeat = 200;
filmEvent[1].seatCapacity = 200;
filmEvent[1].filmType = "2D";
for (int i = 0; i < liveSize; i++){
MyFile << "Ref: " << liveEvent[i].ref << "\n";
MyFile << "Name: " << liveEvent[i].name << "\n";
MyFile << "SeatCapacity: " << liveEvent[i].seatCapacity << "\n";
MyFile << "AvailableSeat: " << liveEvent[i].availableSeat << "\n";
MyFile << "\n";
}
for (int i = 0; i < standUpSize; i++){
MyFile << "Ref: " << standUpEvent[i].ref << "\n";
MyFile << "Name: " << standUpEvent[i].name << "\n";
MyFile << "SeatCapacity: " << standUpEvent[i].seatCapacity << "\n";
MyFile << "AvailableSeat: " << standUpEvent[i].availableSeat << "\n";
for (long unsigned int j = 0; j < standUpEvent[i].seatTrack.size();
j++){
MyFile << "SeatTrack: " << standUpEvent[i].seatTrack[j] << "\n";
}
MyFile << "\n";
}
for (int i = 0; i < filmSize; i++){
MyFile << "Ref: " << filmEvent[i].ref << "\n";
MyFile << "Name: " << filmEvent[i].name << "\n";
MyFile << "SeatCapacity: " << filmEvent[i].seatCapacity << "\n";
MyFile << "AvailableSeat: " << filmEvent[i].availableSeat << "\n";
MyFile << "FilmType: " << filmEvent[i].filmType << "\n";
MyFile << "\n";
}
MyFile.close();
}
void printName(details* array){
std::cout << array->name << std::endl;
}
void saveData()
{
std::ofstream MyFile(FILENAME, std::ofstream::trunc);
for (int i = 0; i < liveSize; i++){
MyFile << "Ref: " << liveEvent[i].ref << "\n";
MyFile << "Name: " << liveEvent[i].name << "\n";
MyFile << "SeatCapacity: " << liveEvent[i].seatCapacity << "\n";
MyFile << "AvailableSeat: " << liveEvent[i].availableSeat << "\n";
MyFile << "\n";
}
for (int i = 0; i < standUpSize; i++){
MyFile << "Ref: " << standUpEvent[i].ref << "\n";
MyFile << "Name: " << standUpEvent[i].name << "\n";
MyFile << "SeatCapacity: " << standUpEvent[i].seatCapacity << "\n";
MyFile << "AvailableSeat: " << standUpEvent[i].availableSeat << "\n";
for (long unsigned int j = 0; j < standUpEvent[i].seatTrack.size();
j++){
MyFile << "SeatTrack: " << standUpEvent[i].seatTrack[j] << "\n";
}
MyFile << "\n";
}
for (int i = 0; i < filmSize; i++){
MyFile << "Ref: " << filmEvent[i].ref << "\n";
MyFile << "Name: " << filmEvent[i].name << "\n";
MyFile << "SeatCapacity: " << filmEvent[i].seatCapacity << "\n";
MyFile << "AvailableSeat: " << filmEvent[i].availableSeat << "\n";
MyFile << "FilmType: " << filmEvent[i].filmType << "\n";
MyFile << "\n";
}
MyFile.close();
}
void determineSize()
{
liveSize = 0;
standUpSize = 0;
filmSize = 0;
std::ifstream myFile(FILENAME);
int ref;
std::string myText;
while (std::getline(myFile, myText)){
if (myText.find("Ref: ") != std::string::npos){
ref = std::stoi(myText.substr(myText.find("Ref: ") + 5));
if (ref == 1){
liveSize += 1;
} else if (ref == 2){
standUpSize += 1;
} else if (ref == 3){
filmSize += 1;
}
}
}
myFile.close();
}