Ticket Booking System
// Group C// Q 20. The Ticket Booking System of Cinemax Theatre Has To Be Implemented Using C++ Program.// There Are 10 Rows And 7 Seats in Each Row. Doubly Circular Linked List Has To Be Maintained// To Keep Track of Free Seats At Rows. Assume Some Random Booking To Start With.// Use Array To Store Pointers(Head Pointers) To Each Row. On Demand// A) The List of Available Seats is to be Displayed.// B) The Seats Are To Be Booked.// C) The Booking Can be Cancelled.#include <iostream>using namespace std;class Seat{public:int row, column;char status;Seat *prev, *next;};class BookingSystem : public Seat{Seat *head[10], *tail[10];public:BookingSystem(); // Default Constructer Declaration~BookingSystem() {} // DestructerSeat *createSeat(int, int);void bookSeat();void cancelSeat();void displaySeat();};// Function To Handle Wrong User Inputtemplate <typename T>T checkInput(T input, string content){while (!cin.good()){cout << "\nWrong Input, Please Enter It Correctly!!\n";cin.clear();cin.ignore(100, '\n');cout << content;cin >> input;}while (input < 0){cout << "\nThis Input Cannot Be Negative!!\n";cin.clear();cin.ignore(100, '\n');cout << content;cin >> input;}return input;}// Default Constructer DefinitionBookingSystem ::BookingSystem(){Seat *temp;for (int i = 1; i <= 10; i++){head[i] = tail[i] = NULL;temp = new (nothrow) Seat;if (!temp)throw bad_alloc();else{for (int j = 1; j <= 7; j++){temp = createSeat(i, j);if (head[i] == tail[i] && head[i] == NULL){head[i] = tail[i] = temp;head[i]->prev = tail[i];tail[i]->next = head[i];}else{temp->next = head[i];head[i]->prev = temp;head[i] = temp;head[i]->prev = tail[i];tail[i]->next = head[i];}}}}delete temp;}Seat *BookingSystem ::createSeat(int x, int y){Seat *temp = new (nothrow) Seat;if (!temp)throw bad_alloc();temp->row = x;temp->column = y;temp->status = 'A';temp->prev = NULL;temp->next = NULL;return temp;}void BookingSystem ::bookSeat(){int num;string content("\nHow Many Seats You Want To Book : ");cout << content;cin >> num;num = checkInput(num, content);while (num > 70){cout << "\nThere are only 70 Seats in The Theatre!!\n";cin.clear();cin.ignore(100, '\n');char userInput;while (true){cout << "\nDo you want to try it again (y/n)\n";cin >> userInput;if (userInput == 'y' || userInput == 'Y'){cout << content;cin >> num;num = checkInput(num, content);if (num > 70)break;elsegoto label1;}else if (userInput == 'n' || userInput == 'N'){cout << "\n**** Thanks For Coming!! ****\n";exit(0);}else{cout << "\nPlease Enter Correct Input!!\n";continue;}}}label1:for (int i = 1; i <= num; i++){int rowNo, columnNo;char content1[30];sprintf(content1, "\nEnter Row No. of Seat %d : ", i); // F - Stringcout << content1;cin >> rowNo;rowNo = checkInput(rowNo, content1);while (rowNo > 10){cout << "\nThere are only 10 Rows in The Theatre!!\n";cin.clear();cin.ignore(100, '\n');cout << content1;cin >> rowNo;rowNo = checkInput(rowNo, content1);}char content2[30];sprintf(content2, "\nEnter Column No. of Seat %d : ", i); // F - Stringcout << content2;cin >> columnNo;columnNo = checkInput(columnNo, content2);while (columnNo > 7){cout << "\nThere are only 7 Columns in The Theatre!!\n";cin.clear();cin.ignore(100, '\n');cout << content2;cin >> columnNo;columnNo = checkInput(columnNo, content2);}Seat *temp = head[rowNo];for (int i = 1; i <= 7; i++){if (temp->column == columnNo){if (temp->status == 'A'){temp->status = 'B';cout << "\nSeat Booked Successfully\n";}elsecout << "\nSorry!, The Seat is Already Booked!!\n";}temp = temp->next;}}}void BookingSystem ::cancelSeat(){int num;string content5("\nHow Many Seats You Want To Cancel : ");cout << content5;cin >> num;num = checkInput(num, content5);while (num > 70){cout << "\nThere are only 70 Seats in The Theatre!!\n";cin.clear();cin.ignore(100, '\n');char userInput;while (true){cout << "\nDo you want to try it again (y/n)\n";cin >> userInput;if (userInput == 'y'){cout << content5;cin >> num;num = checkInput(num, content5);if (num > 70)break;elsegoto label2;}else if (userInput == 'n'){cout << "\n**** Thanks For Coming!! ****\n\n";exit(0);}else{cout << "\nPlease Enter Correct Input!!\n";continue;}}}label2:for (int i = 1; i <= num; i++){int rowNo, columnNo;char content3[30];sprintf(content3, "\nEnter Row No. of Seat %d : ", i);cout << content3;cin >> rowNo;rowNo = checkInput(rowNo, content3);while (rowNo > 10){cout << "\nThere are only 10 Rows in The Theatre!!\n";cin.clear();cin.ignore(100, '\n');cout << content3;cin >> rowNo;rowNo = checkInput(rowNo, content3);}char content4[30];sprintf(content4, "\nEnter Column No. of Seat %d : ", i); // F - Stringcout << content4;cin >> columnNo;columnNo = checkInput(columnNo, content4);while (columnNo > 7){cout << "\nThere are only 7 Columns in The Theatre!!\n";cin.clear();cin.ignore(100, '\n');cout << content4;cin >> columnNo;columnNo = checkInput(columnNo, content4);}Seat *temp = NULL;temp = head[rowNo];for (int i = 1; i <= 7; i++){if (temp->column == columnNo){if (temp->status == 'B'){temp->status = 'A';cout << "\nSeat Cancelled Successfully\n";}elsecout << "\nSorry!, The Seat is Already Cancelled!!\n";}temp = temp->next;}}}void BookingSystem ::displaySeat(){cout << "\n";for (int i = 1; i <= 10; i++){Seat *temp = head[i];for (int j = 1; j <= 7; j++){cout << "[" << temp->row << "," << temp->column<< "," << temp->status << "]"<< " ";temp = temp->next;}cout << "\n";}}int main(){cout << "\n***** Ticket Booking System *****\n";string content("\n1. Press 1 To Display Seats\n2. Press 2 To Book Seat\n3. Press 3 To Cancel Seat\n4. Press 4 To Exit\n>>>> ");BookingSystem *bs = new (nothrow) BookingSystem();if (!bs)throw bad_alloc();int userInput1;char userInput2;while (true){cout << content;cin >> userInput1;userInput1 = checkInput(userInput1, content);switch (userInput1){case 1:bs->displaySeat();break;case 2:bs->bookSeat();break;case 3:bs->cancelSeat();break;case 4:cout << "\n**** Thanks For Coming!! ****\n\n";exit(0);default:cout << "\nPlease Enter Correct Input!!\n";continue;}while (true){cout << "\nPress 'c' To Continue and 'e' To Exit\n";cin >> userInput2;if (userInput2 == 'c')break;else if (userInput2 == 'e'){cout << "\n**** Thanks For Coming!! ****\n\n";exit(0);}elsecout << "\nPlease Enter Correct Input!!\n";}}delete bs;return 0;}// Output// 1. Press 1 To Display Seats// 2. Press 2 To Book Seat// 3. Press 3 To Cancel Seat// 1// [1,7,A] [1,6,A] [1,5,A] [1,4,A] [1,3,A] [1,2,A] [1,1,A]// [2,7,A] [2,6,A] [2,5,A] [2,4,A] [2,3,A] [2,2,A] [2,1,A]// [3,7,A] [3,6,A] [3,5,A] [3,4,A] [3,3,A] [3,2,A] [3,1,A]// [4,7,A] [4,6,A] [4,5,A] [4,4,A] [4,3,A] [4,2,A] [4,1,A]// [5,7,A] [5,6,A] [5,5,A] [5,4,A] [5,3,A] [5,2,A] [5,1,A]// [6,7,A] [6,6,A] [6,5,A] [6,4,A] [6,3,A] [6,2,A] [6,1,A]// [7,7,A] [7,6,A] [7,5,A] [7,4,A] [7,3,A] [7,2,A] [7,1,A]// [8,7,A] [8,6,A] [8,5,A] [8,4,A] [8,3,A] [8,2,A] [8,1,A]// [9,7,A] [9,6,A] [9,5,A] [9,4,A] [9,3,A] [9,2,A] [9,1,A]// [10,7,A] [10,6,A] [10,5,A] [10,4,A] [10,3,A] [10,2,A] [10,1,A]// Press 'c' To Continue and 'e' To Exit// c// 1. Press 1 To Display Seats// 2. Press 2 To Book Seat// 3. Press 3 To Cancel Seat// 2// How Many Seats You Want To Book// 2// Enter Row No. of Seat 1// 1// Enter Column No. of Seat 1// 1// Enter Row No. of Seat 2// 2// Enter Column No. of Seat 2// 2// Press 'c' To Continue and 'e' To Exit// c// 1. Press 1 To Display Seats// 2. Press 2 To Book Seat// 3. Press 3 To Cancel Seat// 1// [1,7,A] [1,6,A] [1,5,A] [1,4,A] [1,3,A] [1,2,A] [1,1,B]// [2,7,A] [2,6,A] [2,5,A] [2,4,A] [2,3,A] [2,2,B] [2,1,A]// [3,7,A] [3,6,A] [3,5,A] [3,4,A] [3,3,A] [3,2,A] [3,1,A]// [4,7,A] [4,6,A] [4,5,A] [4,4,A] [4,3,A] [4,2,A] [4,1,A]// [5,7,A] [5,6,A] [5,5,A] [5,4,A] [5,3,A] [5,2,A] [5,1,A]// [6,7,A] [6,6,A] [6,5,A] [6,4,A] [6,3,A] [6,2,A] [6,1,A]// [7,7,A] [7,6,A] [7,5,A] [7,4,A] [7,3,A] [7,2,A] [7,1,A]// [8,7,A] [8,6,A] [8,5,A] [8,4,A] [8,3,A] [8,2,A] [8,1,A]// [9,7,A] [9,6,A] [9,5,A] [9,4,A] [9,3,A] [9,2,A] [9,1,A]// [10,7,A] [10,6,A] [10,5,A] [10,4,A] [10,3,A] [10,2,A] [10,1,A]// Press 'c' To Continue and 'e' To Exit// c// 1. Press 1 To Display Seats// 2. Press 2 To Book Seat// 3. Press 3 To Cancel Seat// 2// How Many Seats You Want To Book// 88// There are only 70 Seats in The Theatre// Do you want to try it again (y/n)// y// How Many Seats You Want To Book// 88// There are only 70 Seats in The Theatre// Do you want to try it again (y/n)// y// How Many Seats You Want To Book// 2// Enter Row No. of Seat 1// 11// There are only 10 Rows in The Theatre// Enter Row No. of Seat 1// 1// Enter Column No. of Seat 1// 1// Sorry!, The Seat is Already Booked// Enter Row No. of Seat 2// 2// Enter Column No. of Seat 2// 2// Sorry!, The Seat is Already Booked// Press 'c' To Continue and 'e' To Exit// c// 1. Press 1 To Display Seats// 2. Press 2 To Book Seat// 3. Press 3 To Cancel Seat// 3// How Many Seats You Want To Cancel// 88// There are only 70 Seats in The Theatre// Do you want to try it again (y/n)// 8// Please Enter Correct Input// y// How Many Seats You Want To Cancel// 2// Enter Row No. of Seat 1// 11// There are only 10 Rows in The Theatre// Enter Row No. of Seat 1// 1// Enter Column No. of Seat 1// 11// There are only 7 Columns in The Theatre// Enter Column No. of Seat 1// 1// Enter Row No. of Seat 2// 2// Enter Column No. of Seat 2// 2// Press 'c' To Continue and 'e' To Exit// c// 1. Press 1 To Display Seats// 2. Press 2 To Book Seat// 3. Press 3 To Cancel Seat// 1// [1,7,A] [1,6,A] [1,5,A] [1,4,A] [1,3,A] [1,2,A] [1,1,A]// [2,7,A] [2,6,A] [2,5,A] [2,4,A] [2,3,A] [2,2,A] [2,1,A]// [3,7,A] [3,6,A] [3,5,A] [3,4,A] [3,3,A] [3,2,A] [3,1,A]// [4,7,A] [4,6,A] [4,5,A] [4,4,A] [4,3,A] [4,2,A] [4,1,A]// [5,7,A] [5,6,A] [5,5,A] [5,4,A] [5,3,A] [5,2,A] [5,1,A]// [6,7,A] [6,6,A] [6,5,A] [6,4,A] [6,3,A] [6,2,A] [6,1,A]// [7,7,A] [7,6,A] [7,5,A] [7,4,A] [7,3,A] [7,2,A] [7,1,A]// [8,7,A] [8,6,A] [8,5,A] [8,4,A] [8,3,A] [8,2,A] [8,1,A]// [9,7,A] [9,6,A] [9,5,A] [9,4,A] [9,3,A] [9,2,A] [9,1,A]// [10,7,A] [10,6,A] [10,5,A] [10,4,A] [10,3,A] [10,2,A] [10,1,A]// Press 'c' To Continue and 'e' To Exit// e
Comments
Post a Comment