/*
File name: hw6.cpp
Author: Bo Bayles
E-mail address: bmb3h6@umr.edu  
Description: 
*/

#include <iostream>
using namespace std;

struct Flight
{
  char flightNumber[10];
  char airlineName[50];
  char cityDepart[50];
  char cityDest[50];
  float fPrice;
  float fDistance;
};

char DisplayMenu(int iFlights);
void AddFlight(Flight flights[], const int iFlights);
void DispFlights(const Flight flights[], const int iFlights);
int findSmallest(const Flight flights[], const int iSize, const int iStart,
                 const int iSortWhat);
void swap(Flight flights[], const int iSize, const int iPos1, const int iPos2);
void selectSort(Flight flights[], const int iSize, const int iSortWhat);
void findDirect(const Flight flights[], const int iFlights);


int main()
{
  int iFlights = 0;
  char cMenu;
  Flight flights[50];
  
  do
  {
    cMenu = DisplayMenu(iFlights);

    switch (cMenu)
    {
      case '1':
              AddFlight(flights, iFlights);
              iFlights++;
              break;
            
      case '2':
              selectSort(flights, iFlights, 2);
              DispFlights(flights, iFlights);
              break;
            
      case '3':
              selectSort(flights, iFlights, 3);
              DispFlights(flights, iFlights);
              break;
            
      case '4':
              selectSort(flights, iFlights, 4);
              DispFlights(flights, iFlights);
              break;
              
      case '5':
              findDirect(flights, iFlights);
              break;
              
      case '6':
              break;
              
      default:
             cout << endl << "Invalid entry." << endl;
             break;
    }
  }  
  while(cMenu != '6');
  
  return 0;
}

char DisplayMenu(int iFlights)
{
  char cMenu;

  cout << endl << "\t \t MENU" << endl
       << "\t 1) Add  Flight" << endl
       << "\t 2) Display Flights (sorted by city of departure)" << endl
       << "\t 3) Display Flights (sorted by city of destination)" << endl
       << "\t 4) Display Flights (sorted by price per mile)" << endl
       << "\t 5) Find direct flight" << endl
       << "\t 6) Quit" << endl;

  do
  {
    cout << "Your choice (1 - 6): ";
    cin >> cMenu;
    cin.ignore(1);
    
    if ((cMenu > '6') || (cMenu <= '0'))
      cout << "Invalid entry!" << endl;
  }
  while ((cMenu > '6') || (cMenu <= '0'));
  
  return cMenu;
}

void AddFlight(Flight flights[], const int iFlights)
{
  cout << endl << "Enter the flight number: ";
  cin.getline(flights[iFlights].flightNumber, 10);
  
  cout << "Enter the name of the airline: ";
  cin.getline(flights[iFlights].airlineName, 50);
  
  cout << "Enter the city of departure: ";
  cin.getline(flights[iFlights].cityDepart, 50);

  cout << "Enter the destination city: ";
  cin.getline(flights[iFlights].cityDest, 50);
  
  do
  {
    cout << "Enter the price of the flight: ";
    cin >> flights[iFlights].fPrice;
    
    if (flights[iFlights].fPrice < 0)
      cout << "Invalid entry." << endl;
  }
  while (flights[iFlights].fPrice < 0);
  
  do
  {
    cout << "Enter the flight distance: ";
    cin >> flights[iFlights].fDistance;
    
    if (flights[iFlights].fDistance < 0)
      cout << "Invalid entry." << endl;
  }
  while (flights[iFlights].fDistance < 0);
  
  return;
}

void DispFlights(const Flight flights[], const int iFlights)
{
  if (iFlights == 0)
    cout << endl << "No flights available." << endl;
    
  for (int i = 0; i < iFlights; i++)
  {
    cout << endl << (i+1)<< ")"
         << " Flight Number: \t \t \t" << flights[i].flightNumber << endl
         << "   Airline: \t \t \t" << flights[i].airlineName << endl
         << "   City of Departure: \t \t" << flights[i].cityDepart << endl
         << "   City of Destination: \t" << flights[i].cityDest << endl
         << "   Ticket price: \t \t" << flights[i].fPrice << endl
         << "   Travel distance: \t \t" << flights[i].fDistance;
  }

  return;
}

int findSmallest(const Flight flights[], const int iSize, const int iStart,
                 const int iSortWhat)
{
  int smallestPos = -1;
  int iCompare = 0;
  
  if (iStart < iSize)
  {
    smallestPos = iStart;
    for (int i = (iStart + 1); i < iSize; i++)
    {
      switch (iSortWhat)
      {
        case 2:
              iCompare = strcmp(flights[i].cityDepart, 
                                flights[smallestPos].cityDepart);
              break;
        case 3:
              iCompare = strcmp(flights[i].cityDest,
                                flights[smallestPos].cityDest);
              break;
        case 4:
              if ( (flights[i].fPrice / flights[i].fDistance) < 
              (flights[smallestPos].fPrice / flights[smallestPos].fDistance) )
                smallestPos = i;
              break;
      }    
      
      if (iCompare < 0)
        smallestPos = i;
    }
  }
  
  return smallestPos;
}

void swap(Flight flights[], const int iSize, const int iPos1, const int iPos2)
{
  Flight temp;
  
  if (iPos1 < iSize && iPos2 < iSize && iPos2 >= 0 && iPos2 >= 0)
  {
    temp = flights[iPos1];
    flights[iPos1] = flights[iPos2];
    flights[iPos2] = temp;
  }
  
  return;
}

void selectSort(Flight flights[], const int iSize, const int iSortWhat)
{
  int smallestPos;
  
  for (int i = 0; i < iSize; i++)
  {
    smallestPos = findSmallest(flights,iSize, i, iSortWhat);
    swap(flights, iSize, i, smallestPos);
  }
  
  return;
}

void findDirect(const Flight flights[], const int iFlights)
{
  Flight temp;
  bool bDepartMatch = false;
  bool bDestMatch = false;
  int iMatchPos = 0;
    
  cout << endl << "Enter the city of departure: ";
  cin.getline(temp.cityDepart, 50);

  cout << "Enter the destination city: ";
  cin.getline(temp.cityDest, 50);
  
  for (int i = 0; i < iFlights; i++)
  {
    if ( strcmp(temp.cityDepart, flights[i].cityDepart) == 0 );
      bDepartMatch = true;
    if ( strcmp(temp.cityDest, flights[i].cityDest) == 0 )
      bDestMatch = true;
    if( (bDepartMatch == true) && (bDestMatch == true) )
    {
      iMatchPos = i;
      break;
    }    
  }
  
  if( (bDepartMatch == true) && (bDestMatch == true) )
  {
    cout << endl << "\t \t Match found: " << endl
        << "Flight Number: \t \t \t" << flights[iMatchPos].flightNumber << endl
        << "Airline: \t \t \t" << flights[iMatchPos].airlineName << endl
        << "City of Departure: \t \t" << flights[iMatchPos].cityDepart << endl
        << "City of Destination: \t \t" << flights[iMatchPos].cityDest << endl
        << "Ticket price: \t \t \t" << flights[iMatchPos].fPrice << endl
        << "Travel distance: \t \t" << flights[iMatchPos].fDistance;
  }
  else
    cout << endl << "No flights match." << endl;
  
  return;
}

