import java.io.*;    // Needed for BufferedReader etc.

/**
 * Driver to test methods in the class Heap.
 */
public class TestHeap02
{  public static void main ( String[] args )
   {  MinHeap02 jobQ = new MinHeap02();
      JobEntry  job;
      String    lineIn;
      int       time;
      char      op;

      System.out.println ("Generate initial heap quickly:");
      while (true) // will use "break" on the option "Q"
      {  do
         {  System.out.print ("operation (E,Q):  ");
            lineIn = getLine();
         }  while (lineIn.length() < 1);
         op = Character.toUpperCase(lineIn.charAt(0));
         if (op == 'Q')      // Middle exit from loop.  Sorry!
            break;
         if (op == 'E')
         {  do
            {  System.out.print ("time required:  ");
               lineIn = getLine();
               time = atoi(lineIn);
               if ( time <= 0 )
                  System.err.println ("Time > 0, please.  Try again.");
            }  while ( time <= 0 );

            System.out.print ("Description:  ");
            lineIn = getLine();
            job = new JobEntry(time, lineIn);
            jobQ.enter(job);
         }
         else
         {  System.out.println ("Unrecognized option " + op);
            continue;      // No need to do the display.
         }
      }
      jobQ.heapify();

      System.out.println ("\nBegin maintenance operations:  " +
                          "Enqueue/Dequeue");
      while (true) // will use "break" on the option "Q"
      {  do
         {  System.out.print ("operation (E,D,Q):  ");
            lineIn = getLine();
         }  while (lineIn.length() < 1);
         op = Character.toUpperCase(lineIn.charAt(0));
         op = Character.toUpperCase(lineIn.charAt(0));
         if (op == 'Q')      // Middle exit from loop.  Sorry!
            break;
         if (op == 'E')
         {  do
            {  System.out.print ("time required:  ");
               lineIn = getLine();
               time = atoi(lineIn);
               if ( time <= 0 )
                  System.err.println ("Time > 0, please.  Try again.");
            }  while ( time <= 0 );

            System.out.print ("Description:  ");
            lineIn = getLine();
            job = new JobEntry(time, lineIn);
            jobQ.enqueue(job);
         }
         else if (op == 'D')
         {  job = jobQ.dequeue();
            if ( job != null )
            {  System.out.println ("     " + job.timeRqd
                    + "  " + job.otherInfo);
               job = null;   // I.e., ready for job collection
            }
            else
               System.out.println ("The queue is empty!");
         }
         else
         {  System.out.println ("Unrecognized option " + op);
            continue;      // No need to do the display.
         }
         jobQ.display();
      }
   }

// Code for reading console from "Java in a Nutshell", p. 166
   static BufferedReader console = new BufferedReader
                                  (new InputStreamReader(System.in));
// UTILITY FUNCTIONS:
// Pull a line of text from the console.
   static String getLine ()
   {  String text = "";
      try
      {  text = console.readLine();  }
      catch (IOException e)
      {  /* let it pass */  }
      return text;
   } // end getLine()

// replacement for parseInt that acts like the C function atoi
   static int atoi(String number)
   {  int rtnVal = 0;   // on invalid string, return a zero

      try
      {  rtnVal = Integer.parseInt(number);  }
      catch (NumberFormatException e)
      { /* Let it pass! */  }

      return rtnVal;
   } // end atoi

} // end TestHeap02
