import javax.swing.JOptionPane; // for showInputDialog /** * A class to represent a "payment calculator" which computes the "equal * monthly payment amount" for a fixed-rate loan based on the amount borrowed, * the annual interest rate, and the number of years in which the loan will be * repaid.. */ public class PaymentCalculator { // instance vars int principal; // the amount borrowed double rate; // annual interest rate, as a percent int years; // term of loan in years /** * Create a payment calculator object. * @param amount the principal (amount borrowed) * @param interestRate the annual rate of interest, as a per cent (APR) * @param term the number of years to repay the loan */ public PaymentCalculator(int amount, double interestRate, int term) { principal = amount; rate = interestRate; years = term; } /** * Computes and returns the equal monthly payment amount on a loan * * -N * uses formula: Payment = AR / ( 1 - (1 + R) ) * * where A is principal (amount borrowed), * R is MONTHLY interest rate, as a DECIMAL * N is term of loan in MONTHS. * * @return the equal monthly payment amount */ public double computeMonthlyPayment() { int months = years * 12; // convert years to months // convert rate from annual percent to monthly decimal // (divide by 12 for monthly, by 100 for decimal) double mRate = rate / 1200; // TO DO: in the following statement, use the formula given above to // compute the "equal monthly payment" amount and assign it to // variable "payment," instead of 0.0. double payment = 0.0 ; return payment ; } /** * Computes and returns the total amount paid over the life of the loan * * @return the total of all the monthly payments */ public double computeTotalPayments() { // get the equal monthly payment amount by calling computePayment double monthlyPayment = this.computeMonthlyPayment() ; // total payments = monthly payment amount * term of loan in years * 12 double totalPayments = monthlyPayment * years * 12 ; return totalPayments ; } /** * Return a string containing the loan data. * @return the principal, annual interest rate, and term, as a string. */ public String toString() { String output = "\nLoan amount:\t$" + principal + "\nInterest rate:\t" + rate + "%" + "\nTerm of Loan:\t" + years + " years\n"; return output; } } // end of PaymentCalculator class definition