/* Copyright: Gaurav Sharma Email: gsharma _ayet_ gmail _dawt_ com Url: http://www.gsharma.com Completed: Jan 19th 2005 Course: CS - 3240 Prof: Jim Daley Title: Online Airline Reservation System - Part 1 OS Used: Windows XP Compiler(s): Dev C++ 4 and MS Visual Studio 6.0 Description: The program uses linked list to Add, Delete and List the Flights. It also creates a Trace file which keeps log of the activity from user and system. It is a menu based applicatio, which guides the user throughout and prompts for required values. Functions: The program uses 3 main functions: "add_flight", "delete_flight" and "print_flights." It also uses open_file and search_list functions. add_flight: Prompts for the flight number, verifies if it is in database or not, asks for seating capacity if the flight is not present and stores the values. If the flight already exists, then it informs the user. delete_flight: Checks if database contains any flights, if there is, asks for the flight number. If the flight number matches with an existing flight, the function deletes it or shows the appropriate message. print_flights: Prints all the flights, in the order they were added. open_file: Opens the trace file for writing. search_list: Looks for a particular flight number in the list. Sets the search_flag to 1 if it finds it. */ #include #include struct node { int flight_number; // Flight Numbers int seats; // Seating Capacity node *nxt; // Pointer to next node }; char outputFilename[] = "trace.log"; ofstream outFile; node *start_ptr = NULL; node *current; // Used to move along the list int option = 0; int open_file() { outFile.open(outputFilename, ios::out); if (!outFile) { cerr << "Can't open output file " << outputFilename << endl; return(1); } } int search_list(int f_number) // Searches the database { node *temp; temp = start_ptr; int search_flag; search_flag = 0; cout << endl; while (temp != NULL && search_flag == 0) { // Display details for what temp points to if (f_number == temp->flight_number) { search_flag = 1; } temp = temp->nxt; } return search_flag; // Returns the result } void add_flight() { node *temp, *temp1; // Temporary pointers int temp_f_number; // Reserve space for new node and fill it with data temp = new node; cout << "Please enter the flight number: "; cin >> temp_f_number; outFile << "Please enter the flight number: " << temp_f_number<flight_number = temp_f_number; cout << "Please enter seating capacity : "; cin >> temp->seats; outFile << "Please enter seating capacity : " << temp->seats<< endl; temp->nxt = NULL; cout << "\n Flight Added to the database: \n Flight Number: "<< temp->flight_number << " \t Seating Capacity: "<< temp->seats; outFile << "\n Flight Added to the database: \n Flight Number: "<< temp->flight_number << " \t Seating Capacity: "<< temp->seats; // Set up link to this node if (start_ptr == NULL) { start_ptr = temp; current = start_ptr; } else { temp1 = start_ptr; // We know this is not NULL - list not empty! while (temp1->nxt != NULL) { temp1 = temp1->nxt; // Move to next link in chain } temp1->nxt = temp; } } else { cout << "Sorry, Flight Already Exists in the database"; outFile << "Sorry, Flight Already Exists in the database"<> flight; outFile << "Enter the flight number to be deleted: " << flight << endl; do { // Display details for what temp points to if (flight == temp->flight_number) { delete_flag = 1; if (temp->nxt == NULL) { cout << "\n Flight Deleted from the database: \n Flight Number: "<< temp->flight_number << " \t Seating Capacity: "<< temp->seats; outFile << "\n Flight Deleted from the database: \n Flight Number: "<< temp->flight_number << " \t Seating Capacity: "<< temp->seats; delete temp; if(temp1 == NULL) { start_ptr = NULL; } else { temp1->nxt = NULL; } } else { if(temp1 == NULL) { start_ptr = temp->nxt; } else { temp1->nxt = temp->nxt; } cout << "\n Flight Deleted from the database: \n Flight Number: "<< temp->flight_number << " \t Seating Capacity: "<< temp->seats; outFile << "\n Flight Deleted from the database: \n Flight Number: "<< temp->flight_number << " \t Seating Capacity: "<< temp->seats; delete temp; } } temp1 = temp; temp = temp->nxt; } while (temp != NULL && delete_flag == 0); if (temp == NULL && delete_flag == 0) { cout << " \n Flight Number not in database \n"; outFile << " \n Flight Number not in database \n"; } } } void print_flights() { node *temp; temp = start_ptr; cout << endl; if (temp == NULL) { cout << "There is no flight in the database!" << endl; outFile << "There is no flight in the database!" << endl; } else { while (temp != NULL) { cout << "Flight Number : " << temp->flight_number << " "; cout << "Seating Capacity : " << temp->seats << " "; outFile << "Flight Number : " << temp->flight_number << " "; outFile << "Seating Capacity : " << temp->seats << " "; if (temp == current) cout << endl; temp = temp->nxt; } cout << " \n End of list!" << endl; outFile << " \n End of list!" << endl; } } void main() { open_file(); start_ptr = NULL; outFile << endl; outFile << "Please select an option : " << endl; outFile << "Press 0 to exit the system." << endl; outFile << "Press 1 to add a flight." << endl; outFile << "Press 2 to delete a flight." << endl; outFile << "Press 3 to print the flight list." << endl; do { cout << endl; cout << "Please select an option : " << endl; cout << "Press 0 to exit the system." << endl; cout << "Press 1 to add a flight." << endl; cout << "Press 2 to delete a flight." << endl; cout << "Press 3 to print the flight list." << endl; cout << endl << " >> "; cin >> option; outFile << "\n >> "<< option<< endl; switch (option) { case 1 : add_flight(); break; case 2 : delete_flight(); break; case 3 : print_flights(); } } while (option != 0); }