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  Travis Tucker
 * @version 1.0
 */
public class TaxOnomy
{
    private static PrintStream    tape;
    private static Scanner        keyboard = new Scanner(System.in);

    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
       int index;	 
		 int [] categories;
	    double [] prices;
		 double foodTax;
		 double preparedFoodTax;
		 double nonFoodTax;
       File           tapeFile;       
		 TaxCalculator tax;

       
       // 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;          
       }
       
     	 System.out.print("Enter the number of items: ");
		 index = requestInt();
		 
		 //arrays created
		 categories = new int[index];
		 prices  = new double[index]; 
 		
		 //for loop to run the program the appropriate amount of times
		 for(int i=0;i < index;i++)
		 {
		 	System.out.print
		 	("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
		 	categories[i]=requestCategory();
			System.out.print
			("Enter the price: ");
			prices[i]=requestDouble();
			
  		 }    
		 
		 
		 
		 tax = new TaxCalculator();
		 

		 //gets the total tax for each category
		 foodTax= tax.foodTax(prices,categories);
		 preparedFoodTax= tax.preparedfoodTax(prices,categories);
		 nonFoodTax= tax.nonfoodTax(prices,categories);
		 
		 //outputs the proper data
		 tape.printf("\n%10s\n %10s %8.4f\n %10s %8.4f\n"+
		 	" %10s %8.4f\n","Summary","Food:",foodTax,"Prepared:",
			preparedFoodTax,"Other:",nonFoodTax);

		 System.out.println("\nThe program has ended normally....GOODBYE!");
       


    }






    /**
     * 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 
     */
    private static int requestCategory()
    {
       int        category;
       int          number;
		 String     badValue;
		 
		//error checking 
		while(!keyboard.hasNextInt())
		{
			badValue =keyboard.next();
			System.out.print
				("Your entry is an invalid category, please re-enter: ");
		}				
		
		category = keyboard.nextInt(); 		
		
		//error checking
		 while(category != 0 && category != 1 && category != 2)
         {
				System.out.print
				("Your entry is an invalid category, please re-enter: ");
				category = keyboard.nextInt();
			}
         
		 //echoes input
		 System.out.println("OK. Category: "+category);      



       return category;
    }




    /**
     * Prompt the user to enter a double and then
     * read it in (using the Scanner named keyboard)
     *
     * @return         The value 
     */
    private static double requestDouble()
    {
		 boolean      valid;
       double       value;
		 String    badValue;
  
  		valid = false;
      value = -1;
			
			//error checking
			if(keyboard.hasNextDouble())
				{
					value  = keyboard.nextDouble();
				}
			else
			{
				badValue=keyboard.next();
				value = -1;	
			}	
			
		   // more error checking
			while(value < 0 )
			{
					System.out.print
					("Invalid entry please re-enter the price: ");
					
					if(!keyboard.hasNextDouble())
					{
						badValue=keyboard.next();
						value = -1;
					}//end if
					else
						value=keyboard.nextDouble();

			}//end else

		 // echoes input
		 System.out.println("OK. Price: $"+value);
  

       return value;
    }



    /**
     * Prompt the user to enter an int and then
     * read it in (using the Scanner named keyboard)
     *
     * @return         The value 
     */
    private static int requestInt()
    {
       int          value;
  		 String    badValue;
		   
	    while(!keyboard.hasNextInt())
		 {
		 	badValue = keyboard.next();
			System.out.println
			("Invalid int, please re-enter the number of items: ");
		 }
       
		 value = keyboard.nextInt();
       //echoes input
		 System.out.println("OK, "+value+" items.");


       return value;
    }

    
}
