   import java.io.*;
   import java.util.Scanner;
	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  Yanitsa Staleva
 * @version 1.0 date: 09/13/08
 */
    public class TaxOnomy
   {
    private static PrintStream    tape;
    private static Scanner keyboard = new Scanner(System.in);
    private static int []       category;
    private static double []    price;    
   
   
   
    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
       public static void main(String[] args)
      {
         File           tapeFile; 
         int            numItems;
         int []         category;
         double []      price; 
         double         sumNonPrepFood;
         double         sumPrepFood;
         double         sumNonFood; 
      
      
       
       // 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;          
         }
       
         numItems = requestInt
			("\nEnter the number of items: ");
      
         category = new int[numItems];
         price = new double[numItems];  
      
      
         for(int i = 0; i < numItems; i++)
         {
            category[i] = requestCategory();
            price[i] = requestDouble("\nEnter the price: ");
         }
			System.out.println();
      //echo
		for(int i = 0; i < numItems; i++)
		{
		System.out.println("Category is: " + category[i]);
		System.out.println("Price is: " + price[i]);
		}
		
      
         sumNonFood = TaxCalculator.total(price, category,0);
         sumNonPrepFood =TaxCalculator.total(price,category,1);
         sumPrepFood = TaxCalculator.total(price,category,2);
			
			//echo
			System.out.println
			("The sum of the non Food is: " + sumNonFood);
			System.out.println
			("The sum of the non Prep Food is: " + sumNonPrepFood);
			System.out.println
			("The sum of the Prep Food is: " + sumPrepFood);
			
			System.out.println();
         System.out.print("Summary\n");
      
         System.out.printf
       ("%10s%8.4f\n","Food:", 
		 StateTaxes.foodTax(sumNonPrepFood));
      
         System.out.printf
    ("%10s%8.4f\n","Prepared:",
	 LocalTaxes.preparedFoodTax(sumPrepFood));
	 
         System.out.printf
    ("%10s%8.4f\n","Other:",StateTaxes.salesTax(sumNonFood));
      
         System.out.println("\nThis program has ended normally");
      }//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;
         boolean    flag;
       
         flag = false;
         category = 0;  
       
         while(!flag)
         {
            category = requestInt
	("\nEnter 0 for non-food, 1 for food, or 2 for prepared: ");
            flag = TaxCalculator.isValid(category);
            if(!flag)
            {
               System.out.print
					("\nYou did not enter a valid category.");
            }
         }//while
      		 
       	return category;
      }//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;
         boolean         flag;
       
         flag = false;
         value = 0;
      
      while(!flag)
         {
            System.out.print(prompt);
				
            try
           {
               value = keyboard.nextDouble();
               flag = true;
           }
                catch(InputMismatchException ime)
               {
                 System.out.print
					  ("\nYou did not enter a double. ");
					  keyboard.next();
              }
         }//while
       
         return value;
      }//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;
         boolean      flag;
       
         flag = false;
         value = 0;
      
      
       
         while(!flag)
         {
            System.out.print(prompt);
            try
            {
               value = keyboard.nextInt();
               flag = true;
            }
               catch(InputMismatchException ime)
              {
                  System.out.print
						("\nYou did not enter an integer. ");
						keyboard.next();
              }
         }//while
       
         return value;
      }//requestInt
   
    
   }// TaxOnomy
