Flight booking system in C++
Flight booking system using class in C++
The concept of class is best illustrated by using it to build functional systems. A class is a template according to which objects can be created. A class can have data members and member functions. The data members can contain useful information and data specific to an object. A member function can be used for multiple purposes. These purposes include manipulating data members, performing various routines defined by the programmer, etc.
Note: The string.h has been used in this program to perform operations on strings.
#include <iostream> #include <string.h> //for operations on string using namespace std;
How would this system work?
![]() |
credit - https://pixabay.com/users/wal_172619-12138562/ |
A flight booking system entails the user choosing between different flights based on their preferences (departure location, destination, time, day, etc). A flight class is used to create a template or blueprint upon which various flight objects can be created. So, each flight would represent a separate object with its own properties. The properties are essential in the operation of this system. Each flight object binds its own data together and contains internal functions to manipulate or add data to its members or to run the routines of generating a ticket, registering a passenger to its passenger list, etc.
The driver program or the main function ensures that the flight objects are being used in the way they were intended to be used. Checking for the validity of user input, compatibility of preferences with flights, etc.
This system could have been written in several other ways and this is but one way of achieving a functioning flight booking system. In this program, I have used a flight object. One can also classify the user as an object and link the user to a flight object. The possibilities are vast.
Let us look further inside the working of this system.
The flight object
class flight //template for every flight { private: char passengers[25][15]; //list of passengers char pass_days[25][12]; //to recover day passenger has decided to fly int pass_count = 0; //number of passengers public: char flight_name[6]; //limits inside [] are arbitrary and you can select your own limits char from[15]; char to[15]; int days[7] = {0,0,0,0,0,0,0}; // array of integers representing different days of the week char time[20]; flight(char *_flight_name ,char *_from, char *_to, char *_time) //constructor for a flight with all the necessary information { strcpy(flight_name, _flight_name); strcpy(from, _from); strcpy(to, _to); strcpy(time, _time); } void day_status(int mon, int tue, int wed, int thu, int fri, int sat, int sun) //internal function of flight to set the schedule { if(mon == 1){ days[0] = 1; //monday } else if(tue == 1){ days[1] = 2; //tuesday } else if(wed == 1){ days[2] = 3; //wednesday } else if(thu == 1){ days[3] = 4; //thursday } else if(fri == 1){ days[4] = 5; //friday } else if(sat == 1){ days[5] = 6; //saturday } else if(sun == 1){ days[6] = 7; //sunday } } void insertpass(char name[15], int day_num) //internal function of flight to add passenger with their day of departure { strcpy(passengers[pass_count], name); pass_count++; switch(day_num) //recovering day from day_num { case 1: strcpy(pass_days[pass_count], "Monday"); break; case 2: strcpy(pass_days[pass_count], "Tuesday"); break; case 3: strcpy(pass_days[pass_count], "Wednesday"); break; case 4: strcpy(pass_days[pass_count], "Thursday"); break; case 5: strcpy(pass_days[pass_count], "Friday"); break; case 6: strcpy(pass_days[pass_count], "Saturday"); break; case 7: strcpy(pass_days[pass_count], "Sunday"); break; } } void generate_ticket(char name[15]) //printing all details { cout << "----------------------"; cout << "\nTicket\n"; cout << "\tName: "<<name<<"\n"; cout << "\tDeparture: "<<from<<" \tArrival: "<<to; cout << "\n\tDay: "<<pass_days[pass_count]; cout << "\n\tTime: "<<time; cout << "\n\tFlight name: "<<flight_name; cout << "\n\t Enjoy your flight!\n"; } };
The flight class has data members classified as:
- A list of passengers.
- A corresponding array containing the days the corresponding passenger is going to travel on.
- A variable representing the number of passengers.
- Strings containing flight name, departure city, destination, and time.
- An array of integers representing the flight's weekly schedule.
- An internal function that takes in 7 integer arguments that represent the status of the days, i.e., whether or not the flight is scheduled to fly that day. This function is used to set the flight's schedule.
- An internal function of the flight to add passengers with their day of departure. The function takes in an integer that the user inputs and recovers the corresponding day as a string.
- An internal function to generate the ticket after a user has been registered to the flight. It prints all the details.
The driver program
int main() { flight A1 = flight("A1" ,"Mumbai", "Dubai", "9pm to 12:10am"); //flight objects declared A1.day_status(1,1,1,1,1,1,1); flight A2("A2" ,"New York", "London", "3pm to 4pm, next day"); A2.day_status(1,0,1,0,1,0,1); flight A3("A3" ,"Dubai", "Mumbai", "1pm to 4:10pm"); A3.day_status(0,1,0,1,0,1,0); flight A4("A4" ,"Berlin", "New York", "7am to 3:55pm"); A4.day_status(1,0,0,1,1,0,0); char _name[15]; //passenger name int from_num, to_num, day, flight_num; //selection integers char *depart, *arrival; //selection strings obtained from selection integers int flight_confirm[4] = {0,0,0,0}; //array telling if flight matches preferences int identified = 0; //integer telling if anu flight has been identified cout << "\tWelcome to Random airlines\n"; cout << "Please enter your name: "; cin >> _name; label1: cout << "\nPlease select departure city:\n"; cout << "1 for Mumbai\n2 for New York\n3 for Dubai\n4 for Berlin\n"; cin >> from_num; switch(from_num) { case 1: depart = "Mumbai"; break; case 2: depart = "New York"; break; case 3: depart = "Dubai"; break; case 4: depart = "Berlin"; break; default: cout << "Invalid input"; goto label1; } label2: cout << "Please select city for arrival:\n"; cout << "1 for Dubai\n2 for London\n3 for Mumbai\n4 for New York\n"; cin >> to_num; switch(to_num) { case 1: arrival = "Dubai"; break; case 2: arrival = "London"; break; case 3: arrival = "Mumbai"; break; case 4: arrival = "New York"; break; default: cout << "Invalid input"; goto label2; } cout << "Please select the day of departure:\n"; cout << "1 for Monday\n2 for Tuesday\n3 for Wednesday\n4 for Thursday\n5 for Friday\n6 for Saturday\n7 for Sunday\n"; cin >> day; cout << "The following flights have been identified:\n"; flight flights[4] = {A1,A2,A3,A4}; for(int i = 0; i < 4; i++){ if(!(strcmp(flights[i].from, depart)) && !(strcmp(flights[i].to, arrival)) && (flights[i].days[day-1] == 1)){ //search for the right flight cout << "\nName: "<<flights[i].flight_name<<". \tType "<<i<<" to select this flight\n"; //match from, to, and day flight_confirm[i] = 1; //flight identified as matching user preferences identified = 1; //indicator that at least one flight has been identified } } if(identified == 0){ cout << "No flight matches your preferences."; //no flight identified goto label1; //redirect user to change choices } label3: cout << "\nPlease select your preferred flight:\n"; cin >> flight_num; if(flight_confirm[flight_num] == 0){ cout << "\nEntered flight does not match your preference.\n"; //check if entered number is valid or suitable goto label3; } cout << "\nYour ticket is being generated:\n"; flights[flight_num].insertpass( _name, day); //user registered in the flight object flights[flight_num].generate_ticket( _name); //internal function called to display ticket details }
The flights' objects are declared. Their schedules are set separately using:
flight A2("A2" ,"New York", "London", "3pm to 4pm, next day"); A2.day_status(1,0,1,0,1,0,1);
Switch statements are used to store the user choices for departure, arrival, and day.
After these three user choices have been obtained, the program has to search for a flight that satisfies these criteria. To do this, an array of flight objects is declared. A for loop is run over this array checking if the three criteria match with the flight properties.
flight flights[4] = {A1,A2,A3,A4}; for(int i = 0; i < 4; i++){ if(!(strcmp(flights[i].from, depart)) && !(strcmp(flights[i].to, arrival)) && (flights[i].days[day-1] == 1)){ //search for the right flight cout << "\nName: "<<flights[i].flight_name<<". \tType "<<i<<" to select this flight\n"; //match from, to, and day flight_confirm[i] = 1; //flight identified as matching user preferences identified = 1; //indicator that at least one flight has been identified } }
After the criteria match with a flight object, the corresponding element of the array flight_confirm to the identified flight gets assigned a value of 1. The indicator variable "identified" is set to 1, indicating that there is at least one flight that matches the user's preferences.
The Output
Further opportunities:
Such a system would work even better with a user interface, don't you think? The tangents one could take from even a simple project are endless.
The code:
Here is the source code for this project:
#include <iostream> #include <string.h> //for operations on string using namespace std; class flight //template for every flight { private: char passengers[25][15]; //list of passengers char pass_days[25][12]; //to recover day passenger has decided to fly int pass_count = 0; //number of passengers public: char flight_name[6]; //limits inside [] are arbitrary and you can select your own limits char from[15]; char to[15]; int days[7] = {0,0,0,0,0,0,0}; // array of integers representing different days of the week char time[20]; flight(char *_flight_name ,char *_from, char *_to, char *_time) //constructor for a flight with all the necessary information { strcpy(flight_name, _flight_name); strcpy(from, _from); strcpy(to, _to); strcpy(time, _time); } void day_status(int mon, int tue, int wed, int thu, int fri, int sat, int sun) //internal function of flight to set the schedule { if(mon == 1){ days[0] = 1; //monday } else if(tue == 1){ days[1] = 2; //tuesday } else if(wed == 1){ days[2] = 3; //wednesday } else if(thu == 1){ days[3] = 4; //thursday } else if(fri == 1){ days[4] = 5; //friday } else if(sat == 1){ days[5] = 6; //saturday } else if(sun == 1){ days[6] = 7; //sunday } } void insertpass(char name[15], int day_num) //internal function of flight to add passenger with their day of departure { strcpy(passengers[pass_count], name); pass_count++; switch(day_num) //recovering day from day_num { case 1: strcpy(pass_days[pass_count], "Monday"); break; case 2: strcpy(pass_days[pass_count], "Tuesday"); break; case 3: strcpy(pass_days[pass_count], "Wednesday"); break; case 4: strcpy(pass_days[pass_count], "Thursday"); break; case 5: strcpy(pass_days[pass_count], "Friday"); break; case 6: strcpy(pass_days[pass_count], "Saturday"); break; case 7: strcpy(pass_days[pass_count], "Sunday"); break; } } void generate_ticket(char name[15]) //printing all details { cout << "----------------------"; cout << "\nTicket\n"; cout << "\tName: "<<name<<"\n"; cout << "\tDeparture: "<<from<<" \tArrival: "<<to; cout << "\n\tDay: "<<pass_days[pass_count]; cout << "\n\tTime: "<<time; cout << "\n\tFlight name: "<<flight_name; cout << "\n\t Enjoy your flight!\n"; } }; int main() { flight A1 = flight("A1" ,"Mumbai", "Dubai", "9pm to 12:10am"); //flight objects declared A1.day_status(1,1,1,1,1,1,1); flight A2("A2" ,"New York", "London", "3pm to 4pm, next day"); A2.day_status(1,0,1,0,1,0,1); flight A3("A3" ,"Dubai", "Mumbai", "1pm to 4:10pm"); A3.day_status(0,1,0,1,0,1,0); flight A4("A4" ,"Berlin", "New York", "7am to 3:55pm"); A4.day_status(1,0,0,1,1,0,0); char _name[15]; //passenger name int from_num, to_num, day, flight_num; //selection integers char *depart, *arrival; //selection strings obtained from selection integers int flight_confirm[4] = {0,0,0,0}; //array telling if flight matches preferences int identified = 0; //integer telling if anu flight has been identified cout << "\tWelcome to Random airlines\n"; cout << "Please enter your name: "; cin >> _name; label1: cout << "\nPlease select departure city:\n"; cout << "1 for Mumbai\n2 for New York\n3 for Dubai\n4 for Berlin\n"; cin >> from_num; switch(from_num) { case 1: depart = "Mumbai"; break; case 2: depart = "New York"; break; case 3: depart = "Dubai"; break; case 4: depart = "Berlin"; break; default: cout << "Invalid input"; goto label1; } label2: cout << "Please select city for arrival:\n"; cout << "1 for Dubai\n2 for London\n3 for Mumbai\n4 for New York\n"; cin >> to_num; switch(to_num) { case 1: arrival = "Dubai"; break; case 2: arrival = "London"; break; case 3: arrival = "Mumbai"; break; case 4: arrival = "New York"; break; default: cout << "Invalid input"; goto label2; } cout << "Please select the day of departure:\n"; cout << "1 for Monday\n2 for Tuesday\n3 for Wednesday\n4 for Thursday\n5 for Friday\n6 for Saturday\n7 for Sunday\n"; cin >> day; cout << "The following flights have been identified:\n"; flight flights[4] = {A1,A2,A3,A4}; for(int i = 0; i < 4; i++){ if(!(strcmp(flights[i].from, depart)) && !(strcmp(flights[i].to, arrival)) && (flights[i].days[day-1] == 1)){ //search for the right flight cout << "\nName: "<<flights[i].flight_name<<". \tType "<<i<<" to select this flight\n"; //match from, to, and day flight_confirm[i] = 1; //flight identified as matching user preferences identified = 1; //indicator that at least one flight has been identified } } if(identified == 0){ cout << "No flight matches your preferences."; //no flight identified goto label1; //redirect user to change choices } label3: cout << "\nPlease select your preferred flight:\n"; cin >> flight_num; if(flight_confirm[flight_num] == 0){ cout << "\nEntered flight does not match your preference.\n"; //check if entered number is valid or suitable goto label3; } cout << "\nYour ticket is being generated:\n"; flights[flight_num].insertpass( _name, day); //user registered in the flight object flights[flight_num].generate_ticket( _name); //internal function called to display ticket details }
Comments
Post a Comment