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  Glen Newman
 * @version 1.0
 */
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)
    {    
		 double	taxNonfood, taxFood, taxPrepared, totalNonfood, totalFood, totalPrepared;
		 double[]	prices;
		 File           tapeFile;
		 int	numItems;
		 int[]	categories;
		 String	summary, food, prepared, other;


       
       // 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;          
       }
		 
		 //get number of items
		 do
		 {
		 	numItems = requestInt("Enter the number of items: ");
		 } while (numItems < 0);
		 
		 //create price (double) and category (int) arrays with number of items as length
		 categories = new int[numItems];
		 prices = new double[numItems];
		 
		 //enter purchase(s)
		 for (int i = 0; i < prices.length; i++)
		 {
		 	categories[i] = requestCategory();
			prices [i] = requestDouble("Enter the price: ");
		 }
		 
		 //get total value of prices (before tax) THIS IS UNNECESSARY
		 //totalNonfood = TaxCalculator.total(prices, categories, TaxCalculator.NON_FOOD);
		 //totalFood = TaxCalculator.total(prices, categories, TaxCalculator.FOOD);
		 //totalPrepared = TaxCalculator.total(prices, categories, TaxCalculator.PREPARED_FOOD);
		 
		 //get tax on each type
		 taxNonfood = TaxCalculator.nonfoodTax(prices, categories);
		 taxFood = TaxCalculator.foodTax(prices, categories);
		 taxPrepared = TaxCalculator.preparedfoodTax(prices, categories);
		 
		 
		 
       // Generate the required output
		 summary = new String("Summary");
		 food = new String("Food:");
		 prepared = new String("Prepared:");
		 other = new String("Other:");
		 tape.printf("%10s\n", summary);
		 tape.printf("%10s%8.4f\n", food, taxFood);
		 tape.printf("%10s%8.4f\n", prepared, taxPrepared);
		 tape.printf("%10s%8.4f\n", other, taxNonfood);

		 
    }






    /**
     * Prompt the user to enter a category and then
     * read it in (using the Scanner named keyboard).
     *
     * This method will continue to prompt until the
	  * user enters a valid category
     *
     * @return         The value 
     */
    private static int requestCategory()
    {
       int        category;
       
       category = -1;
       
		 do
		 {
		 	System.out.print("Enter 0 for non-food, 1 for food, or 2 for prepared: ");
			category = keyboard.nextInt();
		 } while ((category < 0) || (category > 2));

       return category;
    }




    /**
     * 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;
		 
		 System.out.print(prompt);
		 value = keyboard.nextDouble();
		 
       return value;
    }



    /**
     * 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;
       
		 System.out.print(prompt);
		 value = keyboard.nextInt();
       
       return value;
    }

    
}
