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  Mark Koskinen
 * @version 1.0, Date: September 15, 2008
 */
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;          
       }
       
		 
       // The application logic

			// create variables to keep running totals
    		int numberOfItems;
			int numOfNonFood = 0;
			int numOfFood = 0;
			int numOfPrepared = 0;
		 
			numberOfItems = requestInt("Enter the number of items: ");
			// create two arrays one for each item's categories and
			// one for each item's prices
			int[] categories       = new int[numberOfItems];
			double[] prices     = new double[numberOfItems];
			
			for(int i=0; i<numberOfItems; i++)
			{ //open loop

			categories[i] = requestCategory();
			prices[i]     = requestDouble("Enter the price: ");
			
			
			// create if statements to echo how many of each item was entered

			if (categories[i]==0) numOfNonFood ++;
			if (categories[i]==1) numOfFood ++;
			if (categories[i]==2) numOfPrepared ++;
						
			
			} //close loop


       // Generate the required output
		 

			// echo inputs
		   tape.println();	
			tape.println();
		 	tape.println("You have entered " + numberOfItems + " items");
			tape.println
					(numOfNonFood + " are non-food items, with a total of " 
					+ TaxCalculator.total(prices, categories, 0));
			tape.println
					(numOfFood + " are unprepared food items, with a total of "
					+ TaxCalculator.total(prices, categories, 1));
			tape.println
					(numOfPrepared + " are prepared food times, with a total of "
					+ TaxCalculator.total(prices, categories, 2));
			tape.println();
		 
			// print tax totals
			tape.printf("\n%10s\n", "Summary");
			
			tape.printf("%10s", "Food:");
			tape.printf("%8.4f\n", TaxCalculator.foodTax(prices, categories));

			tape.printf("%10s", "Prepared:");
			tape.printf
				("%8.4f\n", TaxCalculator.preparedfoodTax(prices, categories));
			
			tape.printf("%10s", "Other:");
			tape.printf("%8.4f\n", TaxCalculator.nonfoodTax(prices, categories));
			
			tape.println("\nthis program has ended normally");

    } // close main method 

    /**
     * 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()
    { //open method
       int        category;
       
       category = -1;
      
		while (!TaxCalculator.isValid(category))
		{ //open loop
			System.out.print
				("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
			category = keyboard.nextInt();
		} //close loop
      return category;
    } //close method

    /**
     * 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)
    { //open method
       double       value;
       
       value = 0.0;
	  	 tape.print(prompt);
		 value = keyboard.nextDouble();
       return value;
    } //close method

    /**
     * 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)
    { //open method
       int          value;
       
       value = 0;
       
		tape.print(prompt);
		value = keyboard.nextInt();

       return value;
    } //close method
} //close class
