Explore BrainMass

Explore BrainMass

    Creating a Mortgage Calculator in Java

    Not what you're looking for? Search our solutions OR ask your own Custom question.

    This content was COPIED from BrainMass.com - View the original, and get the already-completed solution here!

    Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage and the user's selection from a menu of available mortgage loans:

    - 7 years at 5.35%
    - 15 years at 5.5%
    - 30 years at 5.75%

    Use an array for the mortgage data for the different loans. Display the mortgage payment amount followed by the loan balance and interest paid for each payment over the term of the loan. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program. Once again it needs to be able to compile from the command line.

    © BrainMass Inc. brainmass.com March 4, 2021, 11:47 pm ad1c9bdddf


    Solution Preview

    I added a new method, createJMenuBar(), in the mcFrame class to add the main menu bar with a menu called Available Mortgages. When a user clicks on one of the available mortgages, the year JTextField and the rate JTextField will be filled with the available choices. Then the user must enter the loan amount and click Calculate button to see the result. I tested the source code on Win 7 machine and it works ok. Let me know if you have any problems.

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.text.DecimalFormat;
    import java.util.Scanner;
    import javax.swing.*;

    public class mortgageCalculatorGui2 {

    public static void main(String[] args) {
    //Use GUI to calculate mortgages
    mcFrame mc = new mcFrame();

    class MortgageCalculator {

    private int years; //number of years on the loan
    private double loan; // amount of loan
    private double interest;// percentage of interest
    private double monthlyPayment;// amount ofmonthly payment
    private double totalPayment; // this is the loan amount plus interest
    private double interestPaid;//monthly interst payment
    private double balanceLoan;// loan balance
    private double balancePrinciple;// principle balance
    //display variables
    private String str;// header,
    private int displayCount;//display counter
    private int scroll;//scroll counter
    private Scanner input;
    //display for gui
    private StringBuffer strForGui; //string buffer for gui display

    public MortgageCalculator(int years, double loan, double interest) {
    this.years = years;
    this.loan = loan;
    this.interest = interest;
    input = new Scanner(System.in);
    //get monthly payment
    //interest is in percentage, so for monthly interest, interest = (interset/100)/12=interest/1200..
    monthlyPayment = loan * (interest / 1200) * (1 + 1 / (Math.pow(1 + interest / 1200, years * 12) - 1));
    totalPayment = monthlyPayment * years * 12;//the total payment is the sum of loan amount and the
    //total of the interest paid over the course of the loan
    balanceLoan = totalPayment;
    balancePrinciple = this.loan;
    //use to display schedule on console

    public void displaySchedule() {
    //display schedule
    while (displayCount < years * 12) {
    //prepare the heading
    str = (String.format("%10s %10s %10s %10s", "month", "payment", "interest", "balance"));// returns the next element in the literation, 10s means reserve 10 ...

    Solution Summary

    The following posting helps with creating a mortgage calculator in Java.