Skip to content

Instantly share code, notes, and snippets.

@doorknob60
Last active December 24, 2015 01:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doorknob60/6723902 to your computer and use it in GitHub Desktop.
Save doorknob60/6723902 to your computer and use it in GitHub Desktop.
// Flight management
// Copyright Austin Beatty 2013
#include <iostream>
#include <list>
#include <string>
using namespace std;
struct Flight
{
list<string> passengers;
int flightNumber;
inline bool operator < (const Flight &flight2) const
{
return flightNumber < flight2.flightNumber;
}
inline bool operator > (const Flight &flight2) const
{
return flightNumber > flight2.flightNumber;
}
};
int displayMenu()
{
cout << "What would you like to do?\n";
cout << "1. Reserve a ticket\n";
cout << "2. Cancel a reservation\n";
cout << "3. Check whether a ticket is reserved for a particular person\n";
cout << "4. Display a manifest for a specific flight\n";
cout << "5. Display a manifest for all flights\n";
cout << "6. Exit\n";
cout << "Please enter a choice (1-6): ";
int choice;
cin >> choice;
return choice;
}
void reserveTicket(list<Flight>& allFlights)
{
cout << "\nPlease enter your name: ";
string name;
cin.ignore();
getline(cin, name);
cout << "Please enter the flight number: ";
int flightNum;
cin >> flightNum;
bool flightFound = false;
for (list<Flight>::iterator it=allFlights.begin(); it != allFlights.end(); ++it)
{
if (it->flightNumber == flightNum)
{
it->passengers.push_back(name);
flightFound = true;
it->passengers.sort();
break;
}
}
if (flightFound == false)
{
Flight newFlight;
newFlight.flightNumber = flightNum;
newFlight.passengers.push_back(name);
allFlights.push_back(newFlight);
allFlights.sort();
}
cout << endl;
cout << "Press any key to continue...";
cin.ignore();
cin.get();
cout << endl;
}
void cancelReservation(list<Flight>& allFlights)
{
cout << "\nPlease enter your name: ";
string name;
cin.ignore();
getline(cin, name);
cout << "Please enter the flight number: ";
int flightNum;
cin >> flightNum;
bool flightFound = false;
bool passengerFound = false;
for (list<Flight>::iterator it=allFlights.begin(); it != allFlights.end(); ++it)
{
if (it->flightNumber == flightNum)
{
flightFound = true;
for (list<string>::iterator passenger=it->passengers.begin(); passenger != it->passengers.end(); ++passenger)
{
if (*passenger == name)
{
it->passengers.erase(passenger);
passengerFound = true;
break;
}
}
break;
}
}
if (flightFound == false)
{
cout << "Flight " << flightNum << " not found\n";
passengerFound = true; // even though this is false, change it to true to not display the other error message
}
if (passengerFound == false)
{
cout << "Passenger not found on flight " << flightNum << endl;
}
cout << "Press any key to continue...";
cin.ignore();
cin.get();
cout << endl;
}
void checkTicket(list<Flight>& allFlights)
{
cout << "\nPlease enter your name: ";
string name;
cin.ignore();
getline(cin, name);
cout << "Please enter the flight number: ";
int flightNum;
cin >> flightNum;
bool flightFound = false;
bool passengerFound = false;
for (list<Flight>::iterator it=allFlights.begin(); it != allFlights.end(); ++it)
{
if (it->flightNumber == flightNum)
{
flightFound = true;
for (list<string>::iterator passenger=it->passengers.begin(); passenger != it->passengers.end(); ++passenger)
{
if (*passenger == name)
{
cout << name << " is on flight " << flightNum << endl;
passengerFound = true;
break;
}
}
break;
}
}
if (flightFound == false)
{
cout << "Flight " << flightNum << " not found\n";
passengerFound = true; // even though this is false, change it to true to not display the other error message
}
if (passengerFound == false)
{
cout << "Passenger not found on flight " << flightNum << endl;
}
cout << "Press any key to continue...";
cin.ignore();
cin.get();
cout << endl;
}
void displayFlightManifest(list<Flight>& allFlights)
{
cout << "\nPlease enter the flight number: ";
int flightNum;
cin >> flightNum;
bool flightFound = false;
for (list<Flight>::iterator it=allFlights.begin(); it != allFlights.end(); ++it)
{
if (it->flightNumber == flightNum)
{
flightFound = true;
cout << "\nPassengers:\n";
for (list<string>::iterator passenger=it->passengers.begin(); passenger != it->passengers.end(); ++passenger)
{
cout << *passenger << endl;
}
break;
}
}
if (flightFound == false)
{
cout << "Flight not found\n";
}
cout << "Press any key to continue...";
cin.ignore();
cin.get();
cout << endl;
}
void displayAllManifests(list<Flight>& allFlights)
{
for (list<Flight>::iterator it=allFlights.begin(); it != allFlights.end(); ++it)
{
cout << "\nPassengers on flight " << it->flightNumber << ":\n";
for (list<string>::iterator passenger=it->passengers.begin(); passenger != it->passengers.end(); ++passenger)
{
cout << *passenger << endl;
}
}
cout << "Press any key to continue...";
cin.ignore();
cin.get();
cout << endl;
}
int main()
{
list<Flight> allFlights;
int menuChoice = 0;
do
{
menuChoice = displayMenu();
switch (menuChoice)
{
case 1:
reserveTicket(allFlights);
break;
case 2:
cancelReservation(allFlights);
break;
case 3:
checkTicket(allFlights);
break;
case 4:
displayFlightManifest(allFlights);
break;
case 5:
displayAllManifests(allFlights);
break;
case 6:
break;
default:
cout << "Invalid choice.\n";
}
}
while (menuChoice != 6);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment