import java.io.*;
import java.util.NoSuchElementException;

/**
 * The controller for a 5-function calculator.
 * A Controller repeatedly reads from an InputStream, evaluates the
 * expression, and writes the result to an OutputStream.
 *
 * @author  Professor E. Adams, James Madison University
 * @version 1.0
 */
 public class ControllerA
 {
    private BufferedReader     myBufferedReader;
    private CalculatorA        myCalculator;
    private PrintWriter        myPrintWriter;

    /**
     * Explicit Value Constructor
     *
     * @param myInputStream   The InputStream to read from
     * @param myOutputStream   The OutputStream to write to
     */
     public ControllerA(InputStream myInputStream, OutputStream myOutputStream)
     {
	      myCalculator = new CalculatorA();

			myBufferedReader  = new BufferedReader(new InputStreamReader(myInputStream));
			myPrintWriter = new PrintWriter(new OutputStreamWriter(myOutputStream));
     }

     /**
      * Run the control loop until there are no more expressions to process
		* at which time the expression read will be null 
      */
    	public void run()
    	{
			int                result;
			String             aLine;


			try 
			{

	 	   		// Prompt the user
			   myPrintWriter.println("Ready...");
	  			myPrintWriter.flush();
	    
	    			// Read the first line
	    		aLine = myBufferedReader.readLine();

	    			// Loop until an End-Of-File (EOF) is read
	    		while (aLine != null) 
            {
						// Echo the input
					myPrintWriter.println("Echo of Input: "+ aLine);
					myPrintWriter.flush();
		
						// Perform the calculation if possible
						// and display an appropriate message (the answer or an error)
					try 
               {
		 			   result = myCalculator.calculate(aLine); // asking the Calculator object
																			 // to perform the calculation							
					   myPrintWriter.println(result);
		   			myPrintWriter.flush();
					}    
					catch (ArithmeticException ae) // divide by 0
					{
					   myPrintWriter.println(" Division by 0 is illegal ");
		   			myPrintWriter.flush();
					} 
					catch (NoSuchElementException nsee) 
					{
		   			myPrintWriter.println(" Invalid operator or too few operators/operands");
		   			myPrintWriter.flush();
					}
					catch (NumberFormatException nfe)
					{
		   			myPrintWriter.println(" Operand is not an int: "+ nfe.getMessage());
		   			myPrintWriter.flush();
					}

							// Read the next line -- note that this occurs AFTER 
							//  the TRY blocks if successfully calculated
							//  one of the CATCH blocks if couldn't successfully calculate
					   aLine = myBufferedReader.readLine();
	    		}

		 } 
		 catch (IOException ioe) 
		 {
					 // This shouldn't happen under normal circumstances
	    		System.err.println("I/O Problem");
	    		ioe.printStackTrace();
		 }
	myPrintWriter.println (" Done.");
	myPrintWriter.flush();	 
   } // end run

 } // end Controller class
