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  Mike Stanley
 * @version 1.0 - 9/14/08
 */
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 numberOfItems;
		 double nonFoodTotal;
		 double foodTotal;
		 double preparedFoodTotal;
		 double tax;
       File tapeFile;       

		 int[] items;
		 double[] values;
		 
		 //initializing totals
		 nonFoodTotal      = 0.0;
		 foodTotal         = 0.0;
		 preparedFoodTotal = 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);
          } //end try
          catch (IOException ioe)
          {
             tape  = System.out;          
          } //end catch
       } //end if
       // Otherwise, use System.out
       else
       {
          tape = System.out;          
       } //end else
       
		 //prompt for the number of items and save the value
		 numberOfItems = requestInt("Enter the number of items: ");
		 //echo user input
		 System.out.println("You are purchasing "+numberOfItems+
		 							" items.");
		 
		 //create two arrays to hold the items category
		 //and their value using the user entered
		 //number of items.
		 items = new int[numberOfItems];
		 values = new double[numberOfItems];
		 
		 //loop through to get the category and value of
		 //each item entered by the user.
		 for(int i=0; i<items.length; i++)
		 {
		   //prompt for the category of item i
		 	items[i] = requestCategory();
			//echo user input
			System.out.println("You have entered "+items[i]+
								" as the category.");
			//prompt for the price of the item i
		 	values[i] = requestDouble("Enter the price: ");
			//echo user input
			System.out.println("You have entered "+values[i]+
								" as the price.");
			
			//stores the total for each category by taking
			//the arrays of values and items and selecting
			//the category to calculate.
			nonFoodTotal = 
			 TaxCalculator.total
			 (values, items, TaxCalculator.NON_FOOD);
			foodTotal = 
			 TaxCalculator.total
			 (values, items, TaxCalculator.FOOD);
			preparedFoodTotal = 
			 TaxCalculator.total
			 (values, items, TaxCalculator.PREPARED_FOOD);
			
			//Debugging Test
			//System.out.println("Non-food: "+nonFoodTotal);
			//System.out.println("Food: "+foodTotal);
			//System.out.println("Prepared food: "+
			//				preparedFoodTotal);
			
		 } //end for

		 //Print tape
		 tape.printf("%10s\n", "Summary");
		 tax = StateTaxes.foodTax(foodTotal);
		 tape.printf("%10s%8.4f\n", "Food:", tax);
		 tax = LocalTaxes.preparedFoodTax(preparedFoodTotal);
		 tape.printf("%10s%8.4f\n", "Prepared:", tax);
		 tax = StateTaxes.salesTax(nonFoodTotal);
		 tape.printf("%10s%8.4f\n", "Other:", tax);
		 
		 //program ended normally message
		 System.out.print("\nThis program has ended normally!");

    } //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;
       
       category = -1;
       
		 //loop until a valid category is entered
		 while(!(TaxCalculator.isValid(category)))
		 {
		 	System.out.print("Enter 0 for non-food, 1 for food,"+
		 					" or 2 for prepared: ");
		 	category = keyboard.nextInt();
		 } //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;
       
       value = 0.0;
       
		 //print the prompt
		 System.out.print(prompt);
		 //get users value and store it
		 value = keyboard.nextDouble();
		 						
       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;
       
		 //print the prompt
		 System.out.print(prompt);
		 //get user entered value and store it
		 value = keyboard.nextInt();

       return value;
    } //end requestInt()
   
} //end TaxOnomy