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  Ryan Johnson
 * @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)
    {
	    File           tapeFile;       
		 int				 number;
		 int 				 quality = 0;
		 double			 cost; 
		 double 			 taxOnFood = 0.0;
		 double         taxOnNonFood = 0.0;
		 double         taxOnPrepared = 0.0;
		 

       // 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)
          {
          	 System.out.print("The file could not be found");          
          }
       }
       // Otherwise, use System.out
       
		 else
       {
          System.out.println("File not found...");          
       }
       

       
       // The application logic

		 System.out.print("Enter the number of items: ");
		 number = keyboard.nextInt();
		 
		 double[] price = new double[number];
		 int[] category = new int[number];
		 for (int i = 0; i < number; i ++)
		 {
		 
			quality = requestCategory();
			category[i] = quality;
		 	cost = requestDouble("Enter the price: ");	
			price[i] = cost;
			
		 }
		 
		 // Calculates the tax on the different kinds of foods
		 for (int j = 0; j < price.length; j ++)
		 {  
		 	if (quality == 0)
		 	{
				taxOnNonFood = TaxCalculator.nonfoodTax(price, category);
		 	}
		 	else if (quality == 1)
		 	{
				taxOnFood = TaxCalculator.foodTax(price, category);
		 	}
		 	else if (quality == 2)
		 	{
				taxOnPrepared = TaxCalculator.preparedfoodTax(price, category);
			}
		 }			





       // Generate the required output

		 
		 tape.print("Summary ");
		 tape.flush();
		 tape.print("\n");
		 tape.flush();
		 tape.print("Food: ");
		 tape.flush();
		 
		 // Checks the arrays for items under 
		 // the food category and outputs the tax calculation
		 for ( int j = 0; j < price.length; j++)
		 {
		     if (category[j] == 1)
			 {
			     tape.print(taxOnFood);
			     tape.flush();
			 }
		 }
		
		 tape.print("\n");
		 tape.flush();
		 tape.print("Prepared: ");
		 tape.flush();
		 
		 // The loop checks each index of the array
		 for ( int j = 0; j < price.length; j++)
		 {
		 	if (category[j] == 2)
			{
				tape.print(taxOnPrepared);
				tape.flush();
			}
    	 }   	 
		
		 tape.print("\n");
		 tape.flush();
		 tape.print("Other: ");
		 tape.flush();
		 
		 
		 for ( int j = 0; j < price.length; j ++)
		 {
		 	if (category[j] == 0)
			{
				tape.print(taxOnNonFood);
				tape.flush();
			}
		 }
 		 
		 tape.print("\n");
		 tape.flush();
 
 }






    /**
     * 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;
       
       category = -1;
       
		 System.out.print("Enter 0 for non-food, 1 for food, or 2 for prepared: "); 
		 category = keyboard.nextInt();
		 
		 // This method will repeatedly prompt the user to enter an
		 // appropriate value
		 while (category < 0 || category > 2)
		 {
		 	System.out.print("Please re-enter 0 for non-food, 1 for food, or 2 for prepared: "); 
		 	category = keyboard.nextInt();
		 }
       

       return category;
    }




    /**
     * Prompt the user to enter a double and then
     * read it in (using the Scanner named keyboard)
     *
     * @param prompt   The custom portion of the prompt
     * @return         The value 
     */
    private static double requestDouble(String prompt)
    {
       double       value;
       
       value = 0.0;
		 
		 System.out.print("Enter the price: ");
   	 // the next double entered is stored as the price in the array    
		 value = keyboard.nextDouble();

		 return value;
    }



    
    
}
