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  DAT NGUYEN
 * @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)
	{
		//local variables
		File tapeFile;     
		int numberOfItems;
		double[] prices;  
		int[] categories;
       
		// 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);
          	} //end try
          	catch (IOException ioe)
          	{
             tape  = System.out;          
          	} //end catch
       	} //end if
       	
		// Otherwise, use System.out
		else
		{
			tape = System.out;          
		} //end else
      		       
		// The application logic
		numberOfItems = requestInt("Enter the number of items:");
		 
		//instantiate the array's sizes of prices and categories
		prices = new double[numberOfItems];
		categories = new int[numberOfItems];
		
		//for loop to loop through the number of items
		for (int i = 0; i < numberOfItems; i++)
		{
			categories[i] = requestCategory();
		 	prices[i] = requestDouble("Enter the price: ");			
		}
		
		// Generate the required output
		System.out.println("Summary");
		System.out.printf("       Food: %6.4f",  TaxCalculator.foodTax(prices, categories));
		System.out.printf("\n   Prepared: %6.4f", TaxCalculator.preparedFoodTax(prices, categories));
		System.out.printf("\n     Others: %6.4f", TaxCalculator.nonFoodTax(prices, categories));
		System.out.println("\nThis program has ended normally.");
	} //end main method

	/**
	 * 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()
	{
		//local variable
		int category;
		boolean found;
       
		//initialize variables
      category = -1;
		found = false;
		
		//do-while loop to continue asking for the correct input required
		do
		{
				System.out.println("Enter 0 for non-food, 1 for food"
		 			+ ", or 2 for prepared food: ");

				if(keyboard.hasNextInt())			
		 		{	
					category = keyboard.nextInt();
					System.out.println("You entered: " + category + " as number of category" );
				
					//if condition to check for valid value
					if (TaxCalculator.isValid(category))
						found = true;
					else
						System.out.println(category + " is not a valid input! Try again.");
				}
				else
				{
					keyboard.next();
					System.out.println("The input is invalid!  Try again.");
				}
				
		}while (!found);	//end do-while loop
		
		//returns the checkedinteger category
 		return category;
	} //end requestCategory 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)
	{
		//local variables
	 	double value;
		boolean found;
       
		//initialize variables
		value = 0.0;
		found = false;
		
		//do-while loop to continue asking for the correct input required
		do
		{
		 		System.out.println(prompt);
				if(keyboard.hasNextDouble())			
		 		{	
					value = keyboard.nextDouble();
					System.out.println("You entered: " + value + " as number of price" );
				
					//if condition to check for valid value
					if (value > 0)
						found = true;
					else
						System.out.println(value + " is not a valid input! Try again.");
				}
				else
				{
					keyboard.next();
					System.out.println("The input is invalid!  Try again.");;
				}
				
		}while (!found);	//end do-while loop

		//returns the checked double value
		return value;
	} //end requestDouble 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)
	{
		//local variables
		int value;
		boolean found;
       
		 //initialize variables
		value = 0;
		found = false;
		
		//introduce the program to the user
		System.out.println("Welcome to the Taxonomy program!\nThis program "
			+ "will calculates and print out the summary of tax "
			+ "\non non_food item, non_prepared food item and prepared food item.\n"); 
		//do-while loop to continue asking for the correct input required
		do
		{
		 		System.out.println(prompt);
				if(keyboard.hasNextInt())			
		 		{	
					value = keyboard.nextInt();
					System.out.println("You entered: " + value + " as number of items" );
				
					//if condition to check for valid value
					if (value > 0)
						found = true;
					else
						System.out.println(value + " is not a valid input! Try again.");
				}
				else
				{
					keyboard.next();
					System.out.println("The input is invalid!  Try again.");
				}
				
		}while (!found);	//end do-while loop

		//returns the checked integer value
		return value;
    } //end requestInt method
 } //end class

