The aim of this project is to implement a seat reservation system for a passenge
ID: 3810913 • Letter: T
Question
The aim of this project is to implement a seat reservation system for a passenger airplane. We assume a small airplane with 10 rows and 4 scats per row. We assume that the seat chart is initially stored in a file "chartIn.txt' in the following format: 1 A B C D 2 A B C D 3 A B C D 4 A B C D 5 A B C D 6 A B C D 7 A B C D 8 A B C D 9 A B C D 10 A B C D The example given above means that seats 1A, 1B, 1C, 1D, 2A, 2B, 2C, 2D, 3A, .. are all available. Note that the number 1, 2, 3...also belong to the file. Below is another example of data store in the file "ChartIn.txt": 1 A X C D 2 A B C D 3 A B C D 5 A X C D 6 A B C D 7 A B C D 8 A B C D 9 A B X D 10 A X X D This second example mentions that the following seats marked with "X" are reserved: 1B, 5B, 9C, 10B, 10C. The program should include a function that initially reads the seat chart from the file "chartIn.tzt'' and fill out a 2-dimentional array called seatChart. It then display the following menu: Menu 1. Display Seat Chart 2. Reserve Seat 3. Cancel Reservation 4. Save Seat Chart to File 5.Statistics 6. Help 7. Quit Please Enter Your Choice [1-7); Below is a description of each option in the menu: Option 1 (Display Seat Chart) The program calls a function that displays the seat. chart from the array seatChart using the format town above. Option 2 (Reserve Seat): The program calls a function that prompts users for the seat desired as a string (eg. 3A, 7D, etc.) and then determines the rownumber and column number of the array dement that needs to be accessed. If the seat is available, reserve it by assigning 'X' to the seat and display a message stating that the seat is reserved. Otherwise, display a message stating that the seat is not available. Option 3(Cancel Reservation): The program calls a function that prompts users for the seat to be canceled as a string (e, g, 3A, 7D, etc.) and then determines the row number and column number of the array dement that needs to be accessed. If the seat is available, the program displays an error message stating that the seat to be canceled is not already reserved. Otherwise, make the seat available and display a message stating that the seat reservation has been cancelled. Option 4 (Save Seat Chart to File): The program calls a function that prompts users for a file name. Then it saves the chart to that file in the format shown in page 1. Option 5 (statistics): The program calls a function that displays the following statistics: Number of available seats, percentage of seats that are reserved (to one decimal place), list of window seats that are available, list of aisle seats that available. By our definition the first and 1ast seat in a row are window seats and all other considered aisle seats. Option 6(Help): the program calls a function that displays a detailed message on how to use the program. Option 7(Quit): The program displays a thank you message and terminates.Explanation / Answer
#include<iostream>
#include <fstream>
#include<string.h>
#define ROW 10 //constans
#define COL 4
using namespace std;
/**
* NOTE that the booked status is stored in a 2D int array
* 0 represent vaccant seat
* 1 represent booked seat
*
* You are requested to write the help menu in the function help()
*/
int countBookedSeats(int **a) {
int count = 0, i, j;
for (i=0; i<ROW; i++)
for(j=0; j<COL; j++)
if(a[i][j] == 1)
count ++; //count all booked seats
return count;
}
void reserveSeat(int **a, int row, int col) {
a[row][col] = 1; //reseve a seat
}
void cancelReservation(int **a, int row, int col) {
a[row][col] = 0; //revoke reservation
}
void disp(int **a) {
int i,j;
for (i=0; i<ROW; i++) {
cout<<(i+1);
for(j=0; j<COL; j++)
if(a[i][j] == 1)
cout<<" X"; //if booked print x
else
cout<<" "<<(char)('A'+j); //else print seat no
cout<<endl;
}
}
void write(int **a, char* fileName) { //very similar to disp function
ofstream outFile;
outFile.open(fileName);
int i, j;
for (i=0; i<ROW; i++) {
outFile<<(i+1)<<" ";
for(j=0; j<COL; j++)
if (a[i][j] == 0)
outFile<<(char)(j+'A')<<" ";
else
outFile<<"X ";
outFile<<endl;
}
outFile.close();
}
int** read(char* fileName) {
ifstream inFile;
inFile.open(fileName);
int i, j;
int k;
char ch;
int **a = new int*[ROW];
for (i=0; i<ROW; i++) {
a[i] = new int[COL];
inFile>>k; //skip the seat row number
for(j=0; j<COL; j++){
inFile>>ch;
if (ch == 'X') { //if x, seat is booked
a[i][j] = 1;
}
else {
a[i][j] = 0; //else not booked :)
}
}
}
inFile.close();
return a;
}
void help() {
cout<<"YOU ARE VIEWING THE HELP";
}
int main() {
int **a;
int i;
char j;
int ch;
char* fileName = new char[20];
strcpy(fileName,"chartin.txt");
a = read(fileName);
do {
cout<<"1. Display Seat Chart 2. Reserve Seat 3. Cancel Reservation 4. Save Seat Chart to file 5. Statistics 6. Help 7). Quit ";
cin>>ch;
if (ch == 1)
disp(a);
else if (ch == 2) {
cout<<"Enter row: ";
cin>>i;
cout<<"Enter seat(A-D): ";
cin>>j;
reserveSeat(a,i-1,j-'A');
}
else if (ch == 3) {
cout<<"Enter row: ";
cin>>i;
cout<<"Enter seat(A-D): ";
cin>>j;
cancelReservation(a,i-1,j-'A');
}
else if (ch == 4) {
cout<<"Enter file name: ";
cin>>fileName;
write(a,fileName);
}
else if (ch == 5) {
i = countBookedSeats(a);
cout<<i<<" seats are already booked"<<endl;
cout<<(40-i)<<" seats are vacant"<<endl;
}
else if (ch == 6)
help();
}while (ch != 7);
cout<<"Thank you!"<<endl;
}
I have commented the code as far as possible. let me know if you need more clarification. I hope you like the answer!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.