   import java.io.*;
   import java.util.*;

/**
 * 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  Kurt Dowswell
 * @version 9/16/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)
      {
         double 			foodTax, 	// variables to hold tax calculations
            				preparedFoodTax, 
            				nonFoodTax;
         String 			summary, 	// Strings for printf output
            				food, 
            				prepared, 
            				other;
         File           tapeFile; 	// File variable for I/O 
         int				items;		// int variable for array size 
         double[] 		prices;		// arrays for item information
         int[]    		categories;
      
       
       // 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;          
         }
       
      
      
      	//prompt for items
         items = requestInt("Enter the number of items: ");
      
      
      	//instantiate arrays to correct size according to items
         prices = new double[items];
         categories = new int[items];
      
      
      	//request the categories and prices for the items
         for (int i = 0; i < items; i++)
         {
            categories[i] = requestCategory();
            prices[i]	  = requestDouble("Enter the price: ");
         }//end for
      
      
      
      	//calculate the tax on the three different kinds of items
         nonFoodTax = TaxCalculator.nonfoodTax(prices, categories);
         foodTax = TaxCalculator.foodTax(prices, categories);
         preparedFoodTax = TaxCalculator.preparedfoodTax(prices, categories);
      			
         
        
      	//assign variables to be printed
         summary = "Summary";
         food 	  = "Food:";
         prepared= "Prepared:";
         other	  = "Other:";
      
      	//print the summary of the calculations
         tape.printf("%11s\n %10s %8.4f\n %10s %8.4f\n %10s %8.4f\n", 
            summary, food, foodTax, prepared,  
            preparedFoodTax, other, nonFoodTax);
      	
      	//confirm program has ended normally
         System.out.println("This 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;
         String 	junk;
         boolean 	done;
      
         done 		= false;
         category = -1;
       
         //prompt for category until correct response is given
         while (!TaxCalculator.isValid(category))
         {
            try
            {
               System.out.print
                  ("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
             	//assign user input to category
            	category = keyboard.nextInt();
            
               done = true;
            }//end try
                catch (InputMismatchException ime)
               {
               	//clear buffer
                  junk = keyboard.next();
               }//end catch	
         }//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)
      {
         int 		value;
         String 	junk;
         boolean 	done;
      
         done 	= false;
         value = 0;
      
         while (!done)
         {
            try
            {
            	//prompt for items / int
               System.out.print(prompt);
             	//asign user input to value
               value = keyboard.nextInt();
            
               done = true;
            }//end try
                catch (InputMismatchException ime)
               {
               	//clear buffer
                  junk = keyboard.next();
               }//end catch
         } //end while
      
         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;
         String junk;
         boolean done;
      
         done = false;
         value = 0;
      
         while (!done)
         {
            try
            {
            	//prompt for items / int
               System.out.print(prompt);
             	//asign user input to value
               value = keyboard.nextInt();
            
               done = true;
            }//end try
                catch (InputMismatchException ime)
               {
               	//clear buffer
                  junk = keyboard.next();
               }//end catch
         } //end while
      	
         return value;
      }//end requestInt
    
   }//end TaxOnomy