   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  Hunter McMillen
 * @version 1.0 - 9/8/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)
      {
         File           tapeFile;       
         int 				items; //number of items in the array
         int 	         tempCategory; //holds category
         int[]				categories; //category array
			
         double[]			values;//price array
			double 			tempValue; //holds double value
         double 			nonFoodTotal = 0;
         double 			foodTotal = 0;
         double 			preparedTotal = 0;
			double 			nonFoodTax = 0;
			double 			foodTax = 0;
			double 			preparedTax = 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);
            }
                catch (IOException ioe)
               {
                  tape  = System.out;          
               }
         }
         // Otherwise, use System.out
         else
         {
            tape = System.out;          
         }//end if
       
      
       
       // The application logic
       
       //prompt the user then read the number of items into
       //a variable
         System.out.println("Enter the number of items: ");
         items = keyboard.nextInt();
      
      //initializes the array size to the amount 
      //of items the user inputed
         categories = new int[items];
         values = new double[items];
      
      //echo the values of the array
         System.out.println("CATEGORIES: " + categories.length);
         System.out.println("VALUES: " + values.length + "\n");
      
      
      //prompt the user for the type of entry
      //and its value
      //then assign it to elements in the arrays
         for(int ii=0;ii<items;ii++)
         {
				//gets a category and price from user
            tempCategory = requestCategory();
            tempValue = requestDouble();
         	
				//assigns them to the arrays
            categories[ii] = tempCategory;
            values[ii] = tempValue;
         }//end for
      
            
         for(int jj = 0; jj < items; jj++)
         {
            //non food
            if(categories[jj] == 0)
            {
					//accumulates total for all non food
               nonFoodTotal = nonFoodTotal + TaxCalculator.total(values,categories, 0);
					//accumulates tax on all non food
               nonFoodTax = nonFoodTax + TaxCalculator.nonfoodTax(values, categories);		
            }//end if
         
            //food
            if(categories[jj] == 1)
            {
					//accumulates total for all food
            	foodTotal = foodTotal + TaxCalculator.total(values,categories,1);
					//accumulates tax for all food
					foodTax = foodTax + TaxCalculator.foodTax(values,categories);
            }//end if
         
            //prepared food
            if(categories[jj] == 2)
            {
					//accumulates total for all prepared food
            	preparedTotal = preparedTotal + TaxCalculator.total(values,categories,2);
					//accumulates tax for all prepared food
					preparedTax = preparedTax + TaxCalculator.preparedfoodTax(values,categories);
            }//end if
         }//end for
            
			//non food total
				System.out.println("NONFOODTOTAL: " + nonFoodTotal);
				//non food tax
				System.out.println("NONFOODTAX: " + nonFoodTax	);
				
				//food total
				System.out.println("\nFOODTOTAL: " + foodTotal);
				//food tax
				System.out.println("FOODTAX: " + foodTax);
				
				//prepared total
				System.out.println("\nPREPAREDTOTAL: " + preparedTotal);
				//prepared tax
				System.out.println("PREPAREDTAX: " + preparedTax);
				
				//BLANK LINES
				System.out.println();
				System.out.println();
				System.out.println();
				
				// Generate the required output
				tape.printf("%10s\n","Summary");
				//printf for Food
				tape.printf("%10s" + "%8.4f\n","Food:", foodTax);
				//printf for prepared food
				tape.printf("%10s" + "%8.4f\n","Prepared:", preparedTax);
				//prinf for non food
				tape.printf("%10s" + "%8.4f\n","Other:", nonFoodTax);
				
				//end program
				System.out.println("\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;
       
       //prompt the user for their category
         System.out.println("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
       
       //while the user doesnt enter an integer
       //reprompt for a new category
         while(!keyboard.hasNextInt())
         {
            System.out.println("ERROR: your entry " + keyboard.next() + 
               " was either not a number or it wasn't a valid category " +
               "0, 1, or 2. Re enter a valid category.");
            System.out.println("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
         }//end while
         category = keyboard.nextInt(); //reads in an integer
      	
      	//makes sure the integer is correct
      	// eg ( in range 0 - 2 ) 
         while(category < 0 || category > 2)
         {
            System.out.println("ERROR: your entry " + category + 
               " was either not a number or it wasn't a valid category " +
               "0, 1, or 2. Re enter a valid category.");
            System.out.println("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
            category = keyboard.nextInt(); //reads new integer
         }//end while
      	
         return category;
      }//end requestCategory()
   
   
   
   
    /**
     * Prompt the user to enter a double and then
     * read it in (using the Scanner named keyboard)
     *
     * @return         The value 
     */
       private static double requestDouble()
      {
         double  value;
         value = 0.0;
       
       //while the user doesnt enter an integer
       //reprompt for a new double
         System.out.println("Enter the price: ");
      
         while(!keyboard.hasNextDouble())
         {
            System.out.println("ERROR: your entry " + keyboard.next() + 
               " was either not a number or not a positive price. " +
               " Re enter a valid price.");
         
            System.out.println("Enter the price: ");
         }//end while
         value = keyboard.nextDouble();
      
         return value;
      }//end requestDouble()
   
   
   
    /**
     * Prompt the user to enter an int and then
     * read it in (using the Scanner named keyboard)
     *
     * @return         The value 
     */
       private static int requestInt()
      {
         int value;
         value = 0;
       
      //while the user doesnt enter an integer
       //reprompt for a new int
         System.out.println("Enter the price: ");
      
         while(!keyboard.hasNextInt())
         {
            System.out.println("ERROR: your entry " + keyboard.next() + 
               " was either not a number or not a positive price. " +
               " Re enter a valid price.");
         
            System.out.println("Enter the price: ");
         }//end while
         value = keyboard.nextInt();
      
         return value;
      }//end requestInt()
   }//end TaxOnomy
