// A program that tests the Pop interface and the PopImpl class.
// Barry Cornelius, 13th November 1998
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import uk.ac.dur.dcl0bjc.pop.Person;
import uk.ac.dur.dcl0bjc.pop.Pop;
import uk.ac.dur.dcl0bjc.pop.PopFactory;
import uk.ac.dur.dcl0bjc.pop.PopFactoryImpl;
public class PopImplProg
{
   private static Pop iPop;
   private static BufferedReader iKeyboard;
   private static PopFactory iPopFactory;
   public static void main(String[] pArgs) throws IOException
   {
      iPopFactory = new PopFactoryImpl();
      iPop = iPopFactory.createPop();
      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 = iPopFactory.createPerson(iKeyboard.readLine());
            if ( ! iPop.add(tPerson) )
            {
               System.out.println("The person is already in the population");
            }
         }
         break;
         case 'd':
         {
            iPop = iPopFactory.createPop();
         }
         break;
         case 'g':
         {
            System.out.print("Name? ");
            System.out.flush();
            final String tName = iKeyboard.readLine();
            final Person tPerson = iPop.get(tName);
            if (tPerson==null)
            {
               System.out.println("No such person in the population");
            }
            else 
            {
               System.out.println(tPerson);
            }
         }
         break;
         case 'r':
         {
            System.out.print("Name? ");
            System.out.flush();
            final String tName = iKeyboard.readLine();
            if ( ! iPop.remove(tName) )
            {
               System.out.println("The person is not in the population");
            }
         }
         break;
         default:
         {
            System.out.println(pCommand + " is not a valid command letter");
         }
         break;
      }
   }
}
