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  Jeff Wyman
 * @version 2.0
 */
public class TaxOnomy
{

	 static int totalItems = 0;
	 static int[] categories;
	 static double[] prices;
 	 static double foodTax;
	 static double preparedTax;
	 static double otherTax;	 
    private static PrintStream    tape;//output
	 //keybaord input
    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);
          }//end try
          catch (IOException ioe)
          {
             tape  = System.out;         
          }//end catch
       }//end if
       // Otherwise, use System.out
       else
       {
          tape = System.out;        
       }//end else
       
 
      
       // The application logic
		
		
		//gathers data
		tape.printf("Enter the number of items: ");
		totalItems = keyboard.nextInt();	
		//checks to make sure the input is positive
		while (totalItems > 0)
		{
			tape.printf("Enter the number of items: ");
			totalItems = keyboard.nextInt();
		}//end while
		tape.printf(totalItems + " Items will be used. \n"); //echo totalItems
		categories = new int[totalItems];
		prices = new double[totalItems];
		
		tape.printf("Please begin entering your purchases: \n");
		int i = 0;
		while(i < totalItems)
		{
			categories[i] = requestCategory();
			prices[i] = requestDouble();
			i++;
		}//end while
		
		//calculates the taxes
		foodTax = TaxCalculator.foodTax(prices, categories);
		preparedTax = TaxCalculator.preparedfoodTax(prices, categories);
		otherTax = TaxCalculator.nonfoodTax(prices, categories);    



       // Generate the required output 
		tape.printf("Summary:\n");
		tape.printf("Food:\t %8.4f\n", foodTax);
		tape.printf("Prepared:\t %8.4f\n", preparedTax);
		tape.printf("Other:\t %8.4f\n", otherTax);
		
		tape.printf("The program has ended normally. \n");


    }//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 
     */
    private static int requestCategory()
    {
       int        category;
       
    	 System.out.printf("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
		 category = keyboard.nextInt();
		 tape.printf(category + " Was Input. \n"); //echo category
		 if (TaxCalculator.isValid(category) == false)
		 	category = requestCategory();
			
		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()
    {
       double       value;
       
       value = 0.0;
		 
		 System.out.printf("Enter the price: ");
		 value = keyboard.nextDouble();
 		tape.printf(value + " Was Input. \n"); //echo value
       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;
       
       value = 0;
    	 System.out.println("Please enter an integer: ");
		 value = keyboard.nextInt();       


       return value;
    }//end requestInt

    
}//end TaxOnomy
