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  Sandeep Bhambhani [Sanju]
 * @version 1.0 16th September 2008
 */
public class TaxOnomy
 
{
	// Declaring Variables.
	private static PrintStream    tape;
	private static Scanner        keyboard = new Scanner(System.in);
	
	public static int totalValue = 0; // Total items being purchased.
	public static int[] categories; // Int Array of Category Type
	public static double[] prices;  // Double Array of Price(s) of item(s)


/**
  * The entry point of the application
  *
  * @param args   The command-line arguments
  */
	public static void main(String[] args)
	{
		File tapeFile;
		TaxCalculator taxcal;      
  
		// 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
 
 		// Opening Statement
		// Finding out total numbers of itmes being purchased.
		// Cannot be Zero.
		System.out.printf("Welcome To TaxOnomy.\n\n");
 		totalValue = requestInt("Enter the number of items: ");
 
		// Categories & Prices Array. ( of number of items being purchased! )
		categories = new int[totalValue];
		prices = new double[totalValue];
 
		// Assigning categories & prices to items being purchased!
		//( before calculating for tax! )
		for(int i=0; i<totalValue; i++)
		{
			categories[i] = requestCategory();
			prices[i] = requestDouble("Enter the price: ");
		}
		
		// Calling TaxCalculator's Explicit Value Constructor.
		// with information received from user.
 		taxcal = new TaxCalculator(prices, categories);
 
 		// Generate the required output
		
		// Summary of Tax after Calculations
 		System.out.println();
		System.out.printf("\n%10s\n","Summary");
		System.out.printf("%10s %8.4f ","Food:",taxcal.foodTax(prices, categories));
		System.out.printf("\n");
		System.out.printf("%10s %8.4f ","Prepared:",taxcal.preparedfoodTax(prices, categories));
		System.out.printf("\n");		 
		System.out.printf("%10s %8.4f","Other:",taxcal.nonfoodTax(prices, categories));
		System.out.printf("\n");
		// Closing Statement
		System.out.println();
		System.out.printf("The 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 result;
		result = -1;

		// ONLY ACCPETS 0 - NON FOOD / 1 - FOOD / 2 - PREPARED FOOD.
		while((result<0) || (result>2))
		{
			System.out.printf("Enter 0 for non-food, 1 for food, 2 for prepared: ");
			result = keyboard.nextInt();
		}
		
		return result;
	}// 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 result;
		result = -1;
		// ONLY ACCPETS POSTIVE VALUES.
		while(result<=0)
		{
		System.out.printf(prompt);
		result = keyboard.nextDouble();
		}
		
	return result;
	}// 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 result;
		result = -1;
		// ONLY ACCPETS POSTIVE VALUES.
		while(result<1)
		{
			System.out.printf(prompt);
			result = keyboard.nextInt();
		}
		
		return result;
	}// End requestInt

 
}// End TaxOnomy
