// Clara Choi - Lab 3 - 3/10/2005
// The "Taxation" class.
import java.awt.*;
import hsa.Console;

public class Taxation
{
    static Console c = new Console ();           // The output console
    static double tax, income = 0;

    final static int range1 = 29590; // tax ranges for different rate
    final static int range2 = 29590 * 2;

    final static double rate1 = 0.17; // tax rates
    final static double rate2 = 0.26;
    final static double rate3 = 0.29;

    public static void main (String[] args)
    {

	c.println ("Tax Caculator");
	c.println ("");

	while (income >= 0) // program runs till negative income is detected here
	{
	    c.println ("Please enter your income, end program with negative income");
	    c.print ("$");
	    income = checkDouble (income); // trap user for incorrect input

	    // calculates tax given the tax rates and range
	    if (income <= range1) // if income less than range1
	    {
		tax = income * rate1;
	    }
	    else if (income > range1 && income <= range2) // if income between range1 and range2
	    {
		tax = range1 * rate1 + (income - range1) * rate2;
	    }
	    else if (income > range2) // if income greater than range2
	    {
		tax = range1 * rate1 + range1 * rate2 + (income - range2) * rate3;
	    }

	    if (income > 0) // display tax if income is greater than zero
	    {
		c.print ("Tax: $");
		c.println (tax, 0, 2);
	    }
	    c.println ();
	}

	c.println ("PROGRAM TERMINATED");

    } // main method


    static double checkDouble (double a)
	//trap user for incorrect type of input
    {
	String trap;

	while (true)
	{
	    try // try to convert string to double
	    {
		trap = c.readLine ();
		a = Double.parseDouble (trap);

		break;
	    }

	    catch (NumberFormatException e)  // if can't, display error message
	    {
		c.println ("Incorrect input, please enter again");
	    }

	}

	return a;

    }
} // Taxation class


