import java.io.*;
import java.util.Scanner;

/**
 * A command-line application that can be used to calculate sales taxes
 * on different kinds of items.
 *
 * This application accepts one command-line argument, containing the
 * name of the output file.  If the command-line argument is omitted,
 * this application will send all output to the console 
 * (i.e., standard output).
 *
 *
 * This work complies with the JMU Honor Code.
 *
 * @author  Anthony Miles
 * @version 1.0
 */
public class TaxOnomy
{
    private static PrintStream    tape;
    private static Scanner        keyboard = new Scanner(System.in);
	 private static TaxCalculator  taxCalc  = new TaxCalculator(); //taxCalc
	 				//variable declared globally so multiple methods can use it

    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
       File           tapeFile;     
		 int				 numbOfItems; //to be used to create the two arrays
		 int[]			 itemsArray; //array that will hold the amount of items
		 						//the user enters
		 double[]		 pricesArray; //array to hold the prices that the user
		 						//enters
		 
		 numbOfItems		 = requestInt(); //assigning numbOfItems the value
		 						//requestInt returns and running requestInt to
								//receive that value.  By assigning numbOfItems
								//the value the user enters we can put numbOfItems
								//into the instantiation of the arrays
	  	 itemsArray  		 = new int[numbOfItems];
		 pricesArray 		 = new double[numbOfItems];
		 
       // Code that handles the command-line arguments
       tape = null;

       // If there is a command-line argument, use it as the
       // file name (if possible)
       if ((args != null) && (args.length > 0))
       {
          tapeFile = new File(args[0]);
          try
          {
             tape  = new PrintStream(tapeFile);
          }
          catch (IOException ioe)
          {
             tape  = System.out;          
          }
       }
       // Otherwise, use System.out
       else
       {
          tape = System.out;          
       }
       
      
       
       // The application logic

	 	 for(int i = 0; i < itemsArray.length; i++) //for loop to go through the array
		 				//and assign the variables requestCategory and requestDouble
						//create to the array in the appropriate location
	 	 {
				itemsArray[i] = requestCategory();
				pricesArray[i] = requestDouble();
		 }
       



       // Generate the required output
		 
		 tape.printf("\n%10s\n %10s %8.4f\n %10s %8.4f\n"+ //print statement that will
		 									//automatically format the output and will print out
											//the necessary data
				" %10s %8.4f\n","Summary","Food:",taxCalc.foodTax(pricesArray, itemsArray)
					,"Prepared:", taxCalc.preparedfoodTax(pricesArray, itemsArray)
					,"Other:", taxCalc.nonfoodTax(pricesArray, itemsArray));
	//	tape.print;


    }//end main






    /**
     * Prompt the user to enter a category and then
     * read it in (using the Scanner named keyboard).
     *
     * This method will/must continue to prompt the user
     * until she/he enters a valid category
     *
     * @return         The value 
     */
    public static int requestCategory()
    {
       int        category;
		 int			errorInt;
		 String 		input;
		 Scanner 	lineScan;		 
		 
       category = 0;
		 
		 do //do while loop to error check the user input to ensure it is valid
		 {
			 System.out.print( "Enter 0 for non-food, 1 for food, or 2 for prepared: " );
			 	input = keyboard.nextLine(); //initial scan to take in the user's input
				lineScan = new Scanner(input);
			 
			 if( !lineScan.hasNextInt() ) //check to see if the input has an int
			 								//if not an error message appears and we re-ask
											//for user input
			 	System.out.println("You've entered " + input + 
					" which is an invalid input.");
			 else if ( lineScan.hasNextInt() ) //check to see if the input is within
			 								//bounds specified 0, 1, or 2.
			 {
			 	errorInt = Integer.parseInt(input);
				
				if( errorInt > 2 || errorInt < 0 ) //if not and error message
											//appears and we re-ask user for input
				 	System.out.println("You've entered " + errorInt + 
						" which is an invalid input.");
			 }
			 	category = lineScan.nextInt(); //if no errors we assign the 
											//category int the value the user gave it
		 }while( !taxCalc.isValid(category) ); //end do while

       return category;
    }//end requestCategory




    /**
     * Prompt the user to enter a double and then
     * read it in (using the Scanner named keyboard)
     *
     *
     * @return         The value 
     */
    public static double requestDouble()
    {
       double       value; 
		 String       input;
		 Scanner      lineScan;
		 
		 value = 0;
		 
		 do //do while loop to error check the user input to ensure it is valid
		 {
			 System.out.print("Enter the price: "); //ask the user for initial
			 					//input
			 	input = keyboard.nextLine();
				lineScan = new Scanner(input);
			 
			 if( !lineScan.hasNextDouble() ) //if there is no int in the line
			 					//we give an error message and ask user for
								//input again
			 	System.out.println("You've entered " + input + 
					" which is an invalid input.");
			 else
			 	value = lineScan.nextDouble(); //if nothing is wrong with the
								//input we assign value to the input
		 }while( value == 0 ); //end do while

       return value;
    }//end requestDouble



    /**
     * Prompt the user to enter an int and then
     * read it in (using the Scanner named keyboard)
     *
     *
     * @return         The value 
     */
    public static int requestInt()
    {
		 int 			value;
		 String     input;
		 Scanner    lineScan;
		 
		 value = 0;
		 
		 do //do while loop to error check the user input to ensure it is valid
		 {
			 System.out.print("Enter the number of items: "); //as user for inital
			 				//input
			 	input = keyboard.nextLine();
				lineScan = new Scanner(input);
			 
			 if( !lineScan.hasNextInt() ) //if input has no int we give an
			 				//error message and ask user to re-enter input
			 	System.out.println("You've entered " + input + 
					" which is an invalid input.");
			 else
			 	value = lineScan.nextInt(); //if no errors we assign
							//value to the input
		 }while( value == 0 ); //end do while
		 
       return value;
    }//end requestInt

    
}//end TaxOnomy
