ComputeMortgage

Updated Dani 0 Tallied Votes 177 Views Share

The JOptionPane class is used to give the CompMortgage.java program a front-end GUI interface. The class is divided into numerous functions.

package computemortgage;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
// The following program gives the previous computeMortgage program a front-end
// GUI interface. I wrote the functions from bottom to top because I wasn't
// sure if they needed to be defined before they are used (as is necessary
// in C++.) Also, what about scope identifiers? (e.g. functions which are
// members of a class are usually defined outside of the class declaration
// in C++.)
public class ComputeMortgage {
    // pre: string asking for a value is passed in
    // post: displays msgbox asking user for double and returns what user enters
    static double getDouble(String askFor) {
        return Double.parseDouble(JOptionPane.showInputDialog(null, askFor));
    }
    // pre: string asking for a value is passed in
    // post: displays msgbox asking user for integer and returns what user enters
    static int getInt(String askFor) {
        return Integer.parseInt(JOptionPane.showInputDialog(null, askFor));
    }
    // pre: interest rate, number of years for loan, and loan amount passed in
    // post: returns monthly loan payment
    // used as helper function for void ShowPayment(double, int, double)
    static double monthlyPayment(double rate, int years, double amount) {
        return amount * rate / (1 - (Math.pow(1 / (1 + rate), years * 12)));
    }
    // pre: monthly loan payment and number of years for loan passed in
    // post: returns total payment after loan + interest is payed off
    // used as helper function for void totalPayment(double, int)
    static double totalPayment(double monthly, int years) {
        return monthly * years * 12;
    }
    // pre: interest rate, number of years for loan, and loan amount passed in
    // post: calculates and displays monthly and annual payment
    static void showPayment(double annualRate, int numYears, double loanAmount) {
        double m = monthlyPayment(annualRate, numYears, loanAmount);
        double a = totalPayment(m, numYears);
        JOptionPane.showMessageDialog(null, "Monthly Payment = $" + m);
        JOptionPane.showMessageDialog(null, "Total Payment = $" + a);
    }
    // pre: none
    // post: computes how much $$$ is required to pay off a loan
    public static void main(String[] args) {
        double annualRate = getDouble("Enter Annual Interest Rate") / 100.0;
        int numYears = getInt("Enter number of Years");
        double loanAmount = getDouble("Enter loan Amount");
        showPayment(annualRate, numYears, loanAmount);
    }
}
diamonddog 0 Newbie Poster

Thank you so much for this sample code. I am a spatial learner and do well with examples. Thank you again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.