import java.io.*;
import java.util.*;

/**
 * An abstract dispatch handler.
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public abstract class AbstractDispatchHandler
{
    protected BufferedReader   in;    
    protected Dispatcher       dispatcher;

    /**
     * Construct a new DailyDispatchHandler
     *
     * @param d   The Dispatcher to use
     * @param f   The name of the dispatch file
     */
    public AbstractDispatchHandler(Dispatcher d, BufferedReader in) 
    {
       dispatcher = d;
       this.in    = in;
    }

    /**
     * Process an individual dispatch.
     *
     * @param line  The dispatch
     */
    protected abstract void processDispatch(String line);
    

    /**
     * Process the daily dispatches
     *
     * Note: This method will continue to execute until 
     * all vehicles have been dispatched.  This could take
     * all day.  The application will not be able to
     * do anything else while this method is executing.
     */
    public void processDispatches()
    {
       String            line;

       do
       {
           try
           {
               line = in.readLine();               
               if (line != null) processDispatch(line);
           }
           catch (IOException ioe)
           {
               line = "";               
               System.err.println("Unable to read a dispatch");
           }
       } while (line != null);       
    }

    /**
     * Start this DailyDispatchHandler
     *
     * Specifically, process the daily dispatches
     */
    public void start()
    {
        processDispatches();

        try 
        {
            in.close();
        }
        catch (IOException ioeClose)
        {
            System.err.println("Unable to close dispatches");
        }
    }
}
