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  Glenn Young
 * @version 1.0  Date: September 13, 2008
 */
public class TaxOnomy
{
    //create objects with global scope
    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            numberOfItems;
		 double         foodTax, nonFoodTax, preparedFoodTax;
		 String         output;
		 int[]          categories;
		 double[]       prices;

       
       // Code that handles the command-line arguments
       tape = null;

       numberOfItems = requestInt("Enter the number of items: ");
		 
		 //instantiate categories
		 categories = new int[numberOfItems];
		 //instantiate prices
		 prices = new double[numberOfItems];
		 
		 //for loop iterates once for each item
		 //gets the category and price for each item,
		 //and echoes the input
		 for(int i = 0; i < numberOfItems; i++)
		 {
		     categories[i] = requestCategory();
			  System.out.print("Category for item " + (i+1) + ": " + categories[i]);
			  System.out.println();
			  prices[i] = requestDouble
			  ("Enter the price: ");
			  System.out.println("Price of item " + (i+1) + ": " + prices[i]);
		 }
		 
       // 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 block attempts to instantiate a PrintSteam object
			 //so the output can be output into a file
          try
          {
             tape  = new PrintStream(tapeFile);
          }
			 //catch block reverts program to standard input
          catch (IOException ioe)
          {
             tape  = System.out;          
          }
       }
       // Otherwise, use System.out
       else
       {
          tape = System.out;          
       }
       
      
       
       // The application logic

       //assigns the amount of tax on the food items to foodTax
		 //using the foodTax() method from the TaxCalculator class
		 foodTax = TaxCalculator.foodTax(prices, categories);
		 //assigns the amount of tax on non-food items to nonFoodTax
		 //using the nonfoodTax() method from the TaxCalculator class
		 nonFoodTax = TaxCalculator.nonfoodTax(prices, categories);
		 //assigns the amount of tax on prepared food items to
		 //preparedFoodTax using the preparedfoodTax() method
		 //from the TaxCalculator class
		 preparedFoodTax = TaxCalculator.preparedfoodTax(prices, categories);
       



       // Generate the required output

       //assigns the String to be output to vairable 'output'
       output = String.format("%10s\n%10s%8.4f\n%10s%8.4f\n%10s%8.4f\n",
		  "Summary", "Food:", foodTax, "Prepared:",
		  preparedFoodTax, "Other:", nonFoodTax);
		 
		 //adds a "This program has ended normally." String to 'output'
		 output = output + "\nThis program has ended normally.";
		 
		 //outputs 'output'
		 tape.print(output);
		 tape.flush();
		 
    }//end main()






    /**
     * Prompt the user to enter a category and then
     * read it in (using the Scanner named keyboard).
     *
     * This method continues to prompt the user
     * until she/he enters a valid category
     *
     * @return         The value 
     */
    private static int requestCategory()
    {
       int        category;
       
		 //do-while loop prompts the user to input a item category
		 //until they input a valid category
		 do
		 {
		     category = requestInt
		     ("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
		 }while(!TaxCalculator.isValid(category));
       
       //returns the valid input category
       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;
       
		 //prints the prompt
		 System.out.print(prompt);
		 //assigns the input double to value
       value = keyboard.nextDouble();

       //returns the input double
       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;
       
		 //prints the prompt
		 System.out.print(prompt);
		 //assigns the input int to value
       value = keyboard.nextInt();

       //returns the input int
       return value;
    }//end requestInt()

    
}//end TaxOnomy
