//======================================================
public class LoanEngine {

	//-------------------------------------------------------------------
	public static double payment(LoanData data) {
		return LoanEngine.payment(data.getAmount(), data.getInterestPercent(), data.getYears());
	}

	//-------------------------------------------------------------------
	public static double payment(double amountBorrowed,
								double annualInterestPercent,
								double years) {
		// H-P 67 Standard Pac, p 05-02.
		// amountBorrowed = Present Value
		double kMonthsPerYear = 12.0;
		double	n = kMonthsPerYear * years;
		double 	i = (annualInterestPercent/100.0) / kMonthsPerYear;
		double  factor = 1 -  Math.pow(1.0 + i, -n);
		return (i * amountBorrowed) / factor;
	}

	//-------------------------------------------------------------------
	public static double amount(double monthlyPayment,
									double annualInterestPercent,
									double years) {
		// H-P 67 Standard Pac, p 05-02.
		// amountBorrowed = Present Value
		double kMonthsPerYear = 12.0;
		double	n = kMonthsPerYear * years;
		double 	i = (annualInterestPercent/100.0) / kMonthsPerYear;
		double  factor = 1 - Math.pow(1.0 + i, -n);
		return (monthlyPayment / i) * factor;
	}

	//-------------------------------------------------------------------
	public static double months(double amountBorrowed,
									double monthlyPayment,
									double annualInterestPercent) {
		// H-P 67 Standard Pac, p 05-02.
		// amountBorrowed = Present Value
		double kMonthsPerYear = 12.0;
		double 	i = (annualInterestPercent/100.0) / kMonthsPerYear;
		double  factor1 = Math.log( 1.0 - (i * amountBorrowed/ monthlyPayment) );
		double  factor2 = Math.log( 1.0 + i );
		return  - factor1 / factor2;
	}

	//-------------------------------------------------------------------
	public static double balloonPayment(double amountBorrowed,
										double monthlyPayment,
										double annualInterestPercent,
										double years)
	{
		// H-P 67 Standard Pac, p 05-06.
		double kMonthsPerYear = 12.0;
		double 	i = (annualInterestPercent/100.0) / kMonthsPerYear;
		double	n = kMonthsPerYear * years;
		double  factor1 = Math.pow(1.0 + i, -n);
		double	factor2 = (monthlyPayment / i) * ( 1.0 - factor1);
		return  (amountBorrowed - factor2) / factor1;
	}

	//--------------------------------------------------
}

