//
//  NAME
//    asgn3.cpp
//
//  DESCRIPTION
//    This file contains the functions in the Main module.
//    This module contains the top-level functions of the program.
//    They display a menu, read the user input, and carry out the
//    user's request.
//


//
//  Include files.
//

#include <iostream.h>
#include <ctype.h>
#include "bool.h"
#include "person.h"
#include "plist.h"


//
//  Function prototypes.
//

static void displayMenu();
static void doAdd(plist lists[]);
static void doPrint(plist lists[]);
static void doEmpty(plist lists[]);
static void doCopy(plist lists[]);


//
//  Constants.
//

const int NumOfLists = 2;     //  Number of lists.


//
//  NAME
//    main
//
//  DESCRIPTION
//    This function is the top-level function of the program.  It
//    displays a menu, reads the user's input, then carries out the
//    user's request.
//
//  PARAMETERS
//    (none)
//
//  RETURNS
//    0
//

int main() {
   char  choice = '1';
   bool  done   = false;
   plist lists[NumOfLists];

   while (!done) {
      displayMenu();
      cin >> choice;

      switch (choice) {
         case '1': doAdd(lists);    break;
         case '2': doPrint(lists);  break;
         case '3': doEmpty(lists);  break;
         case '4': doCopy(lists);   break;
         case '5': done = true;     break;
         }

      }

   return 0;
}


//
//  NAME
//    displayMenu
//
//  DESCRIPTION
//    This function displays the menu to the standard output.
//
//  PARAMETERS
//    (none)
//
//  RETURNS
//    (none)
//

static void displayMenu() {
   cout << endl;
   cout << "1.  Add person"            << endl;
   cout << "2.  Print list"            << endl;
   cout << "3.  Empty list"            << endl;
   cout << "4.  Copy list 1 to list 2" << endl;
   cout << "5.  Quit"                  << endl;
   cout << endl;
   cout << "> ";
   return;
}


//
//  NAME
//    chooseList
//
//  DESCRIPTION
//    This function prompts the user to choose a list, then
//    returns a reference to the chosen list.
//
//  PARAMETERS
//    lists  (in) - array of lists
//
//  RETURNS
//    reference to the chosen list
//

static plist& chooseList(plist lists[]) {
   int choice;

   cout << endl;
   cout << "Which list (1 or 2): ";
   cin  >> choice;
   return lists[choice-1];
}


//
//  NAME
//    doAdd
//
//  DESCRIPTION
//    This function reads a person from the standard input stream
//    then adds the person to a list of people selected from the
//    given array of lists.
//
//  PARAMETERS
//    lists  (in/out) - array of lists
//
//  RETURNS
//    (none)
//

static void doAdd(plist lists[]) {
   int      choice;
   person * p = NULL;

   cout << endl << "1) student,  2) instructor,  or 3) programmer? ";
   cin  >> choice;

   switch (choice) {
      case 1: p = new student();     break;
      case 2: p = new instructor();  break;
      case 3: p = new programmer();  break;
      }

   cout << endl;
   p->read();
   chooseList(lists).addToEnd(p);
   delete p;

   return;
}


//
//  NAME
//    doPrint
//
//  DESCRIPTION
//    This function outputs to the standard output stream a list
//    of people selected from the given array of lists.
//
//  PARAMETERS
//    lists  (in) - array of lists
//
//  RETURNS
//    (none)
//

static void doPrint(plist lists[]) {
   cout << chooseList(lists);
   return;
}


//
//  NAME
//    doEmpty
//
//  DESCRIPTION
//    This function empties a list of people selected from the
//    given array of lists.
//
//  PARAMETERS
//    lists  (in/out) - array of lists
//
//  RETURNS
//    (none)
//

static void doEmpty(plist lists[]) {
   chooseList(lists).clearList();
   return;
}


//
//  NAME
//    doCopy
//
//  DESCRIPTION
//    This function copies the second list in the array to the
//    first array in the list.
//
//  PARAMETERS
//    list  (in/out) - array of lists
//
//  RETURNS
//    (none)
//

static void doCopy(plist lists[]) {
   lists[1] = lists[0];
   return;
}
       
//
//  End of file.
//