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  Jonn Callahan
 * @version 1.0 9/14/08
 */
public class TaxOnomy
{
    private static PrintStream    tape;
    private static Scanner        keyboard = new Scanner(System.in);
    private static Scanner        fileScanner;

    /**
     * The entry point of the application
     *
     * @param args   The command-line arguments
     */
    public static void main(String[] args) throws FileNotFoundException
    {
       File           tapeFile;       
       int            numItems;
       int[]          categories;
       double         tax,foodTax,preparedFoodTax,totalSales,totalFood,
                      totalPreparedFood,totalPrice;
       double[]       prices;

       prices=new double[0];
       categories=new int[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]);
          fileScanner = new Scanner(tapeFile);
          try
          {
             tape  = new PrintStream(tapeFile);
             numItems=requestInt("");
             categories=new int[numItems];
             prices=new double[numItems];

             for(int x=0; x<numItems; x++)
             {
             	categories[x]=requestInt("");
             	prices[x]=requestDouble("");
             }//end for
          }//end try
          catch (/*IOException*/ArrayIndexOutOfBoundsException ioe)
          {
             tape  = System.out;          
          }//end cath
       }//end if
       // Otherwise, use System.out
       else
       {
          tape = System.out;
          numItems=requestInt("Enter the number of items: \n");
          System.out.println("You chose to enter "+numItems+" items.");
          categories=new int[numItems];
          prices=new double[numItems];
          
          for(int x=0; x<numItems; x++)
          {
             categories[x]=requestCategory();	
             prices[x]=requestDouble("Enter the price: \n");
             System.out.println("Item category of "+categories[x]+
             	" which costs "+prices[x]);
          }//end for       
       }//end else
       
      
       
       // The application logic
       tax=TaxCalculator.nonfoodTax(prices,categories);
       foodTax=TaxCalculator.foodTax(prices,categories);
       preparedFoodTax=TaxCalculator.preparedfoodTax(prices,categories);

       // Generate the required output
       tape.println("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:",tax);
       tape.println("\nThe 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;
       
       category=requestInt("Enter 0 for non-food, 1 for "+
       	  "food, or 2 for prepared: \n");
       while(!TaxCalculator.isValid(category))
          category=requestInt("Enter 0 for non-food, 1 "+
          	"for food, or 2 for prepared: \n");	 

       return category;
    }//end method




    /**
     * 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);
       value=keyboard.nextDouble();
       
       return value;
    }//end method



    /**
     * 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.print(prompt);
       value=keyboard.nextInt();

       return value;
    }//end method
}//end class
