// A program that performs operations on a queue of people.
// Converted to MVC by Neil Broadbent

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class PersonArrayQueueProg 
{
   private static Queue iPersonQueue;
   private static BufferedReader iKeyboard;
   public static void main(String[] pArgs) throws IOException
   {
      iPersonQueue = new ArrayQueue();
      final OutputQueue tOutputView = new OutputQueue();
      iPersonQueue.addObserver(tOutputView);
      
      iKeyboard = new BufferedReader(new InputStreamReader(System.in));
      while (true)
      {
         System.out.print("Command? ");
         System.out.flush();
         final char tCommand = iKeyboard.readLine().toLowerCase().charAt(0);
         if (tCommand=='q')
         {
            break;
         }
         iProcessCommand(tCommand);
         System.out.println();
      }
   }
   private static void iProcessCommand(final char pCommand) throws IOException
   {
      switch (pCommand)
      {
         case 'a':
         {
            System.out.print("Person? ");  System.out.flush();
            final Person tPerson = new PersonImpl(iKeyboard.readLine());
            iPersonQueue.add(tPerson);
         }
         break;
         case 'd':
         {
            iPersonQueue = new ArrayQueue();
         }
         break;
         case 'e':
         {
            System.out.print("The queue is ");
            if ( ! iPersonQueue.isEmpty() )
            {
               System.out.print("not ");
            }
            System.out.println("empty");
         }
         break;
         case 'g':
         {
            if (iPersonQueue.isEmpty())
            {
               System.out.println("The queue is empty");
            }
            else
            {
               final Person tPerson = (Person)iPersonQueue.getFirst();
               System.out.println("The first person is: " + tPerson);
            }
         }
         break;
         case 'r':
         {
            if (iPersonQueue.isEmpty())
            {
               System.out.println("The queue is empty");
            }
            else
            {
               final Person tPerson = (Person)iPersonQueue.remove();
               System.out.println(tPerson + " has been removed");
            }
         }
         break;
         default:
         {
            System.out.println(pCommand + " is not a valid command letter");
         }
         break;
      }
   }
}
