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  Matt Serone
 * @version 1.6 Date - Sept. 16, 2008
 */
public class TaxOnomy
{
    private static PrintStream    tape;
    private static Scanner        keyboard = new Scanner(System.in);
	private static int		      numItems;
	private static int[]          categories;
	private static double[]       prices;
	 
	private static double		  foodTax;
	private static double		  nonFoodTax;
	private static double		  preparedFoodTax;

    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args)
    {
       File           tapeFile;       
       int			  num0;
       int			  num1;
       int			  num2;
       double		  price0;
       double		  price1;
       double		  price2;
       
       num0 = 0;
       num1 = 0;
       num2 = 0;
       price0 = 0.0;
       price1 = 0.0;
       price2 = 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);
          }
          catch (IOException ioe)
          {
             tape  = System.out;          
          }
       }
       // Otherwise, use System.out
       else
       {
          tape = System.out;                   
       }
       
		 numItems = requestInt("Enter the number of items: ");
		 //prompts the user for the number of items
		 
		 categories = new int[numItems];
		 prices = new double[numItems];
		 //sets the size of the two arrays
		 
		 tape.print("The number of items you have is: ");
		 tape.println(categories.length);
		 //echo the number of items
      
		 for(int x = 0; x < categories.length; x++)
		 //for each value in the array
		 {
		    //sets the category and price in the respectable arrays
		    tape.println("Please enter the info for item #"
			    + (x+1));  
			 categories[x] = requestCategory();
				 
			 prices[x] = requestDouble(
			    "Enter the price: ");
		 }
		 
		 
       
       // The application logic
       //calculates the tax on the respectable types of items
       //using the values entered
	   foodTax = TaxCalculator.foodTax(prices, categories);
	   nonFoodTax = TaxCalculator.nonfoodTax(prices, categories);
	   preparedFoodTax = TaxCalculator.preparedfoodTax(prices, categories);
	   
	   for(int y = 0; y < categories.length; y++)
	   //sets the echo values of the users input
	   {
	   	   if(categories[y] == TaxCalculator.NON_FOOD)
	   	   {
	   	   	   num0++;
	   	   	   price0 += prices[y];
	   	   }
	   	   else if(categories[y] == TaxCalculator.FOOD)
	   	   {
	   	   	   num1++;
	   	   	   price1 += prices[y];
	   	   }
	   	   else if(categories[y] == TaxCalculator.PREPARED_FOOD)
	   	   {
	   	   	   num2++;
	   	   	   price2 += prices[y];
	   	   }
	   }
	   
	   //echo of the users input
	   tape.print("Summary of items: \n");
	   tape.print("The number of items that were not food: ");
	   tape.println(num0);
	   tape.print("The total amount in this category is: ");
	   tape.println(price0);
	   tape.print("The number of items that were unprepared food: ");
	   tape.println(num1);
	   tape.print("The total amount in this category is: ");
	   tape.println(price1);
	   tape.print("The number of items that were prepared food: ");
	   tape.println(num2);
	   tape.print("The total amount in this category is: ");
	   tape.println(price2);
	   
	   

       
	   // Generate the required output
	   //using the correct format, the tax on the items list 
	   //is printed out in thier respectable categories
	   tape.printf("%10s\n", "Summary");
	   tape.printf("%10s%8.4f\n", "Food:", foodTax);
	   tape.printf("%10s%8.4f\n", "Prepared:", preparedFoodTax);
	   tape.printf("%10s%8.4f\n\n", "Other:", nonFoodTax);
	   tape.printf("This program has ended normally.");
	   tape.flush();

    }//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
	   //promts the user for a category and if it is incorrect
	   //they will have to enter a new value
	   {
	   	  tape.print("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
	   	  category = keyboard.nextInt();
			  
			  if(!TaxCalculator.isValid(category))
			     tape.println(
				"That is not a requested number, please try again.");
	   }while(!TaxCalculator.isValid(category));
	   //end do-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)
    {
       double       value;
       
       value = 0.0;
       
	   tape.print(prompt);
	   //prompts the user for the price of the item
	   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;
       
		 tape.println(prompt);
		 //prompts the user for the integer representing
		 //the category of the item
		 value = keyboard.nextInt();

       return value;
    }//end requestInt

    
}//end TaxOnomy class
