   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  Jake Carey
 * @version 1.0, September 16, 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)
      {
			double			total, foodTotal, preparedTotal, nonTotal;
         File           tapeFile;
         int				food,items, other, prepared;
		   int[]				category;
         double[]			price;
			String			foodTotalString, preparedTotalString, nonTotalString;
			
			foodTotalString 		= "Food:";
			preparedTotalString 	= "Prepared:";
			nonTotalString 		= "Other:";
      
			items				= 0;
      	total 			= 0.0;
			foodTotal 		= 0.0;
			preparedTotal 	= 0.0;
			nonTotal 		= 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


       // Beginning of the output.  Creates arrays in length
		 //  based on the number of items the user input.

         items 	= requestInt("Enter the number of items: ");
			category = new int[items]; 
			price 	= new double[items];
      
		 // This loops for however many items the user input.
		 //  It sets the category and prices of each item the
		 //  user purchased and puts them into the appropriate
		 //  array values.
   		for (int i=0; i < items; i++)
			{
				category[i] = requestCategory();
				price[i] 	= requestDouble("Enter the price: ");
			}//end for   	
			
			nonTotal 		= TaxCalculator.nonfoodTax(price, category);
			foodTotal 		= TaxCalculator.foodTax(price, category);
			preparedTotal 	= TaxCalculator.preparedfoodTax(price, category);
			
			System.out.printf("\nSummary\n");
			System.out.printf("%10s%8.4f\n", foodTotalString, foodTotal);
			System.out.printf("%10s%8.4f\n", preparedTotalString, preparedTotal);
			System.out.printf("%10s%8.4f\n", nonTotalString, nonTotal);
			System.out.printf("\n\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;
       
         do
         {
            System.out.printf("Enter 0 for non-food, 1 for food, " + 
										 "or 2 for prepared: ");
            category = keyboard.nextInt();
         }//end do
         while ((category < 0) || (category > 2));
      
         return category;
      }//end requestCategory
   
    /**
     * Prompts 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;
       
         System.out.printf("%s", prompt);
         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;
       
         System.out.printf("%s", prompt);
         value = keyboard.nextInt();
      
         return value;
      }//end requestInt    
   }//end TaxOnomy