/* Program extracts from Chapter 2 of
   "Data Structures and Program Design in C++"
   by Robert L. Kruse and Alexander J. Ryba
   Copyright (C) 1999 by Prentice-Hall, Inc.  All rights reserved.
   Extracts from this file may be used in the construction of other programs,
   but this code will not compile or execute as given here. */
// Section 2.1:

#include "utility.h"
#include "queue.h"
#include <iostream>
#include <cctype>

using namespace std;

void help();
bool do_command(char c, Extended_queue &test_queue);
char get_command();


void main()
/*
Post: Accepts commands from user as a menu-driven demonstration program for
      the class Extended_queue.
Uses: The class Extended_queue and the functions introduction,
      get_command, and do_command.
*/

{
   Extended_queue test_queue;
   //introduction();
   help();
   while (do_command(get_command(), test_queue));
}


void help()
/*
Post: A help screen for the program is printed, giving the meaning of each
      command that the user may enter.
*/

{
   cout << endl
        << "This program allows the user to enter one command" << endl
        << "(but only one) on each input line." << endl
        << "For example, if the command S is entered, then" << endl
        << "the program will serve the front of the queue." << endl
        << endl

        << "The valid commands are:" << endl
        << "A - Append the next input character to the extended queue" << endl
        << "S - Serve the front of the extended queue" << endl
        << "R - Retrieve and print the front entry." << endl
        << "# - The current size of the extended queue" << endl
        << "C - Clear the extended queue (same as delete)" << endl
        << "P - Print the extended queue" << endl
        << "H - This help screen" << endl
        << "Q - Quit" << endl

       // << "Press <Enter> to continue." << flush;

	    << flush;
  /*
   char c;
   do {
      cin.get(c);
	  c = tolower(c);
   } while (c != '\n');
   
   cout << endl;
*/
}


bool do_command(char c, Extended_queue &test_queue)
/*
Pre:  c represents a valid command.
Post: Performs the given command c on the Extended_queue test_queue.
      Returns false if c == 'q', otherwise returns true.
Uses: The class Extended_queue.
*/

{
   bool continue_input = true;
   Queue_entry x;

   switch (c) 
   {
   case 'h':
	   help();
	   break;
   case 'r':
      if (test_queue.retrieve(x) == underflow)
         cout << "Queue is empty." << endl;
      else
         cout << endl
              << "The first entry is: " << x
              << endl;
      break;

   case 'q':
      cout << "Extended queue demonstration finished." << endl;
      continue_input = false;
      break;
    //  Additional cases will cover other commands.
   }
   return continue_input;
}

char get_command()
{
	char command = 'q';	
	bool waiting = true;
	cout << "Select a command and press <Enter> (H for the Help Menu): ";
	while (waiting)
	{
		cin >> command;
		command = tolower(command);
		if(command == 'a' || command == 's' || command == 'r' ||
			command == '#' || command == 'c' || command == 'p' ||
			command == 'h' || command == 'q' )
			waiting = false;
		else
		{
			cout << "Please enter a valid command (H for the Help Menu)" << endl
				<< "A,S,R,#,C,P,H,Q" << endl << endl;
		}
           
	}
	
	
	return command;
}

