/**
 * A utility class that can be used to calculate sales
 * taxes on a collection of items
 *
 *
 * This work complies with the JMU Honor Code.
 *
 * @author  Sandeep Bhambhani [Sanju]
 * @version 1.0
 */
public class TaxCalculator
{
	// Delcaring Variables.
	public static int NON_FOOD        = 0;
	public static int FOOD            = 1;
	public static int PREPARED_FOOD   = 2;
 
	public static double[] prices;
	public static double[] categories;
 
	public static int categoryNumber;

/**
 * Explicit Value Constructor for TaxCalculator Class
 * 
*/
	public TaxCalculator(double[] prices, int[] categories)
	{
		// Assigning local variables to incoming values.	
		prices = prices;
		categories = categories;

		// Calling TaxCalculator's Methods to run Calculations

		foodTax(prices, categories); //foodTax
		nonfoodTax(prices, categories); //nonfoodTax
		preparedfoodTax(prices, categories); //preparedfoodTax
		categoryNumber = -1; // Refreshing categoryNumber
		total(prices, categories, categoryNumber); //total		
	 }

/**
  * Calculate the tax on the (non-prepared) food items
  *
  * @param prices      The prices of all items purchases
  * @param categories  The corresponding categories of all items purchases
  * @return            The tax
  */
	public static double foodTax(double[] prices, int[] categories)
	{
		//Local Variables
		double tax = 0;
		double temptax = 0;
		StateTaxes taxcal;
		taxcal = new StateTaxes();
		// Setting CategoryNumber to FOOD.
		categoryNumber = FOOD;

		//FOR LOOP ending values to StateTaxes (where category is (non-prepared food))
		for(int i=0; i<prices.length; i++)
		{
			if(isValid(categories[i]))
			{
				tax = tax +taxcal.foodTax(prices[i]);
			}
		}
		return tax;     
	 }// End foodTax Calculation Method
 
/**
  * Calculate the tax on the non-food items
  *
  * @param prices      The prices of all items purchases
  * @param categories  The corresponding categories of all items purchases
  * @return            The tax
  */
	public static double nonfoodTax(double[] prices, int[] categories)
	{
		// Local Variables.
		double tax = 0;
		StateTaxes taxcal;
		taxcal = new StateTaxes();
		// Setting CategoryNumber to NON_FOOD
		categoryNumber = NON_FOOD;

		//FOR LOOP ending values to StateTaxes (where category is (non-food))		
		for(int i=0; i<prices.length; i++)
		{
			if(isValid(categories[i]))
			{
				tax = tax + taxcal.salesTax(prices[i]);
			}
		}
		
		return tax;     
	}// End nonfoodTax Calculation Method

/**
  * Calculate the tax on the prepared food items
  *
  * @param prices      The prices of all items purchases
  * @param categories  The corresponding categories of all items purchases
  * @return            The tax
  */
	public static double preparedfoodTax(double[] prices, int[] categories)
	{
		//Local Variables
		double tax = 0;
		LocalTaxes taxcal;
		taxcal = new LocalTaxes();
		// Setting CategoryNumber to PREPARED_FOOD
		categoryNumber = PREPARED_FOOD;
		
		//FOR LOOP ending values to LocalTaxes (where category is (prepared food))
		for(int i=0; i<prices.length; i++)
		{
			if(isValid(categories[i]))
			{
				tax = tax + taxcal.preparedFoodTax(prices[i]);
			}
		}
		
		return tax;     
	}// End preparedFoodTax Calculation Method

/**
  * Calculate the total value of all items purchased in
  * particular category
  *
  * @param prices      The prices of all items purchases
  * @param categories  The corresponding categories of all items purchases
  * @param categoryToInclude  The category of interest
  * @return            The total value of items in the given category
  */
	public static double total(double[] prices, int[] categories,
	int categoryToInclude)
	{

	double value = 0;
	
	if (categoryToInclude == NON_FOOD)
		value = prices[categoryToInclude];
	else if (categoryToInclude == FOOD)
		value = prices[categoryToInclude];
	else if (categoryToInclude == PREPARED_FOOD)
		value = prices[categoryToInclude];
	else
		value = 0;	
		
	return value;
	
	}// End total Calculation Method
  
/**
  * Determin if the given category is valid
  *
  * @param category   The category of interest
  * @return           true if the category is valid; false otherwise
  */
	public static boolean isValid(int category)
	{
		boolean result;

		if(category<=2 && category>=0)
		{
			result = true;
			
			if(category == categoryNumber)
				result = true;
			else
				result = false;
		}
		else
			result = false;
		

		return result;
	}// End isValid Method
 
}// End TaxCalculator Class
