   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  Britni Tourjee
 * @version 2.0
 */
    public class TaxOnomyV2
   {
      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 				 items;
         File           tapeFile;    
       
         items = requestInt("Enter the number of items:");
      	
      
      
       
       // 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
         int    foodCategory;
         double foodPrice;
         double tax;
         double nonfoodTax;
         double foodTax;
         double preparedfoodTax;
      
         int categories[] = new int[items];
      // 			System.out.println(categories.length);
         double prices[] = new double[items];
       
         foodCategory = 0;
         foodPrice = 0.0;
         tax = 0.0;
         nonfoodTax = 0.0;
         foodTax = 0.0;
         preparedfoodTax = 0.0;
      	
      	
      	// loops through price and categories arrays and
      	// assigns different types of taxes according
      	// to what category the user input by calling
      	// the different tax methods in TaxCalculator.   
         for (int i=0; i < categories.length; i++)
         {
            foodCategory = requestCategory();
            categories[i] = foodCategory;
          
            foodPrice = requestDouble("Enter the price:");
            prices[i] = foodPrice;
          
            if (foodCategory == 0)
            {
               nonfoodTax = TaxCalculator.nonfoodTax(prices, categories);
            }
            else if (foodCategory == 1)
            {
               foodTax = TaxCalculator.foodTax(prices, categories);
            }
            else if (foodCategory == 2)
            {
               preparedfoodTax = TaxCalculator.preparedfoodTax(prices, categories);
            }
         	
          
         }
      	
      
       // Generate the required output
       
         tape.printf("%10s", " Summary\n");
         tape.printf("%10s  %8.4f \n", "Food:", foodTax);
         tape.printf("%10s  %8.4f \n", "Prepared:", preparedfoodTax);
         tape.printf("%10s  %8.4f \n", "Other:", nonfoodTax);
      
      
      
      }
   
   
   
   
   
   
    /**
     * 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 	  input;
       
         category = -1;
      	       
         System.out.println("Enter 0 for non-food,1 for food,or 2 for prepared:");
         input = keyboard.nextLine();
      	
         category = Integer.parseInt(input);
      	
      	
      	// prompts the user to input a category
      	// repeatedly until they input a valid
      	// category
         while(category < 0 || category > 2)
         {
            System.out.println("Enter 0 for non-food,1 for food,or 2 for prepared:");
            input = keyboard.nextLine();
         	 
            category = Integer.parseInt(input);
         } 
      
         return category;
      }
   
    
   
   
    /**
     * 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;
         String 		 input;
       
         value = 0.0;
       
         System.out.println(prompt);
         input = keyboard.nextLine();
      	
         value = Double.parseDouble(input);
      
         return value;
      }
   
   
   
    /**
     * 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 		 input;
      	
         value = 0;
       
         System.out.println(prompt);
         input = keyboard.nextLine();
         value = Integer.parseInt(input);
      	
       
         return value;
      }
   
    
   }
