/**
 * 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  Mike Stanley
 * @version 1.0 - 9/14/08
 */
public class TaxCalculator
{
	 //set item categories
    public static int NON_FOOD        = 0;
    public static int FOOD            = 1;
    public static int PREPARED_FOOD   = 2;
    
    /**
     * 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)
    {
       double tax;
       
       tax = 0.0;
		 
		 //loop through all categories and calculate the tax
		 //on food items
		 for(int i=0; i<prices.length; i++)
		 {
		 	if(categories[i] == FOOD) 
			{
				tax += StateTaxes.foodTax(prices[i]);
			} //end if
		 } //end for
       
       return tax;       
    } //end foodTax()

    /**
     * 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)
    {
       double tax;
       
       tax = 0.0;
       
		 //loop through the categories and calculate
		 //the non food tax
		 for(int i=0; i<prices.length; i++)
		 {
		 	if(categories[i] == NON_FOOD) 
			{
				tax += StateTaxes.salesTax(prices[i]);
			} //end if
		 } //end for

       return tax;       
    } //end nonfoodTax
    
    /**
     * 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)
    {
       double tax;
       
       tax = 0.0;
       
		 //loop through the categories and calculate
		 //the tax on all prepared food.
		 for(int i=0; i<prices.length; i++)
		 {
		 	if(categories[i] == PREPARED_FOOD)
			{
				tax += LocalTaxes.preparedFoodTax(prices[i]);
			} //end if
		 } //end for

       return tax;       
    } //end preparedfoodTax()

    /**
     * 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     total;
       
       total = 0.00;
       
		 //check if the category is valid
		 if(isValid(categoryToInclude))
		 {
		 	//loop through all the items
		 	for(int i=0; i<prices.length; i++)
			{
				//if the categories are the same calculate
				if(categories[i] == categoryToInclude)
				{
					//calculate for non food
					if(categories[i] == NON_FOOD)
					{
						//accumulate total price
						total += prices[i];
					} //end if
					//calculate for food
					else if(categories[i] == FOOD)
					{
						//accumulate total price
						total += prices[i];
					} //end else if
					//calculate for prepared food
					else total += prices[i]; //accumulate total
				} //end if
			} //end for
		 } //end if
		 
       return total;       
    } //end total
       
    /**
     * Determine 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;
       
       result = false; //defaulting result
		 
		 //if category exists set to true
		 if(category == NON_FOOD) result = true;
		 else if (category == FOOD) result = true;
		 else if(category == PREPARED_FOOD) result = true; 
       
       return result;       
    } //end isValid()
    
} //end TaxCalculator