import java.io.*;
import java.util.Scanner;

/**
 * PA1
 * 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  Daniel Heck
 * @version 1.1 9/15/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;       
	   String		  sCategory;	//String for category prompt
	   String		  sDouble;		//String for double prompt
	   String		  sInt;			//String for int prompt
	   double[]		  arrayPrices;	//Array of prices
	   int[]		  arrayCategory;//Array of coresponding categories
	   int			  items;		//number of items bought
	   int			  cat; 			// category
	   double		  price;		//price of the item
	   double		  taxNonFood;	//tax on items
	   double		  taxFood;		//tax on food
	   double		  taxPreparedFood;	//tax on prepared food
       
       // 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
		//Create Strings for output
		sInt	  = "Enter the number of items: \n";
        sDouble	  = "Enter the price: \n";
        //Send the number of items to RequestInt method
        items = requestInt(sInt); 
        	
        arrayCategory = new int[items];
        arrayPrices = new double[items];
        
        //loop until the limit number in the arrays are full
        for(int ii = 0; ii < items; ii++)
        {
			cat = requestCategory(); //set cat (category) by calling requestCategory method
			arrayCategory[ii] = cat; //set cat = to current index value of the arrayCategory
			price = requestDouble(sDouble); //set price by calling requestDouble method
			arrayPrices[ii] = price; //set price = to current index value of the arrayPrices
        }//end for
        
        //Initialize these values for later formating ouput by calling their corresponding classes + methods
        taxNonFood 	= TaxCalculator.nonfoodTax(arrayPrices, arrayCategory);
        taxFood   	= TaxCalculator.foodTax(arrayPrices, arrayCategory);
        taxPreparedFood = TaxCalculator.preparedfoodTax(arrayPrices, arrayCategory);

       // Generate the required output
       //All outputed will be print formated for alligned output
       tape.printf("%10s\n", "Summary");
       tape.printf("%10s%8.4f\n", "Food:", taxFood);
       tape.printf("%10s%8.4f\n", "Prepared:", taxPreparedFood);
       tape.printf("%10s%8.4f\n", "Other:", taxNonFood);

    }//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; //declare local int variable for holding category value
         
       //Run this code at least once  
       do
       {
       	  //Set category by first calling the requestInt method while Requesting input
       	  category = requestInt("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
       }while(!TaxCalculator.isValid(category)); //While the category is invalid repeat this code

       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)
    {
       //Create local double variable for return
       double       value;
              
       //Print out prompt string
       System.out.println(prompt);
       //Set local variable value to next inputed double
       value = keyboard.nextDouble();
       
	   //Return the inputed double value
       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)
    {
    	//Declare a local variable for return
       int          value;	

		//Print out the string prompt
       System.out.print(prompt);
       //Set Value to the next inputed Int
       value = keyboard.nextInt();
 		//Return the input
       return value;
    }//end requestInt
}//end Class TaxOnomy
