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  Kyle Ames
 * @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;       


       
       // 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;          
       }
       
  		//VARIABLES
		int 		numItems;    
      double[] prices;
		int[]		categories;
		String	getDoublePrompt;
		String	getIntPrompt;
		double		food, prepared, other;
		
		getIntPrompt	 = "Enter the number of items: ";
		getDoublePrompt = "Enter the Price: ";
				 
       // The application logic

		
		//GET numItems AND DECLARE TWO ARRAYS OF SIZE numItems
		numItems = requestInt(getIntPrompt);
		prices = new double[numItems];
		categories = new int[numItems];
      
		
		//STORE THE DATA IN TWO PARALLEL ARRAYS (categories AND prices)
		for (int i = 0; i < numItems; i++)
			{
				categories[i] = requestCategory(); 
				prices[i] = requestDouble(getDoublePrompt);
			}

		//CALCULATE THE TAXES
		food = TaxCalculator.foodTax(prices, categories);
		other = TaxCalculator.nonfoodTax(prices, categories);
		prepared = TaxCalculator.preparedfoodTax(prices, categories);
		
		
		//DISPLAY THE OUTPUT TO THE USER
		tape.println("Summary\n");
		tape.println("Food: " + food + "\n");
		tape.println("Prepared: " + prepared + "\n");
		tape.println("Other: " + other + "\n");
		tape.flush(); 

		System.out.println();
		System.out.println("The program has ended normally!");

    }






    /**
     * 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;
     	 boolean		validCategory;
		 	 
		 validCategory = false;			
       category = 0;
		 
		 while (!validCategory)
		 	{
				System.out.println("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
		 		category = keyboard.nextInt();
				validCategory = TaxCalculator.isValid(category);     			
			} //end while         

       return category;
		 
    }//end requestCategory




    /**
     * 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;
       boolean		  validValue;
		 
		 validValue = false;
		 value = 0;
		 while(!validValue)
		 	{
		 		System.out.println(prompt);
		 	   value = keyboard.nextDouble();
				if (value >= 0.0)
					validValue = true;
			}//end while

       return value;
    }//end requestDouble



    /**
     * Prompt the user to enter an int and then
     * read it in (using the Scanner named keyboard)
     *
     * @param prompt   The custom portion of the prompt
     * @return         The value 
     */
    private static int requestInt(String prompt)
    {
       int          value;
       
		 System.out.println(prompt);
		 
       value = keyboard.nextInt();
       


       return value;
    }

    
}
