// Clara Choi - 3/7/2005 - Lab 2
// The "InterestProgram" class.
import java.awt.*;
import hsa.Console;
import java.text.DecimalFormat;

public class InterestProgram
{
    static Console c = new Console ();           // The output console
    static DecimalFormat two = new DecimalFormat ("0.00");
    /* decimal format function that makes any number to a string with
    amount of decimal place defined */

    static double currentBalance, newBalance, interestRate = 0.0;

    public static void main (String[] args)
    {
	c = new Console ();

	c.println ("The Interest Calculator");

	c.println ();
	c.println ("What is your current balance?");

	// Calls the trapping method and returns a value of whatever is valid
	currentBalance = checkDouble (currentBalance);
	c.println ("Current Balance: $" + currentBalance);

	c.println ();
	c.println ("What is the interest rate annually? (%) ");
	interestRate = checkDouble (interestRate);

	if (interestRate > 1) //convert input to percent if not already done
	{
	    interestRate = interestRate / 100;
	}
	c.println ("Current Interest Rate: " + interestRate);

	interestRate = interestRate + 1;

	newBalance = currentBalance * interestRate; //calculate New balance at the end of the year

	c.println ();
	c.println ("Your balance after a year given the interest rate:");
		
	/* This converts newbalance from --> double -->
	string with two decimal place --> double */
	newBalance = Double.parseDouble (two.format (newBalance));
	c.print ("To the nearest cent: $");
	c.println (newBalance, 0, 3);

    } // 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;

    }
} // InterestProgram class


