|
Java GUI Mortgage Program
Hi Everyone I am having trouble with my mortgage program that I want to create using a GUI and I was wondering if I can get some assistance as I am stuck and can't figure out a solution. I want to write a program in Java (with a GUI) 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%
I also want to 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. Allowing the user to loop back and enter a new amount and make a new selection or quit. The situation that I am having currently is that I have two seperate programs one is the GUI portion of the program and the other one is my actual program that will do the calculating. I am trying to integrate the two together to make one program that will do all of the features mentioned above.
Here is the Java code for the program with the GUI:
JAVA Code:
public class MortgageCalculatorwk3 extends JFrame
{
// Variable declaration
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JTextField jTextField2;
private JTextField jTextField3;
private JTextField jTextField4;
private JComboBox jComboBox1;
private JTextArea jTextArea2;
private JScrollPane jScrollPane5;
private JButton jButton1;
private JPanel contentPane;
// End of variable declaration
public MortgageCalculatorwk3()
{
super();
initializeComponent();
//
// TODO: Add any constructor code after initializeComponent call
//
this.setVisible(true);
}
private void initializeComponent()
{
jLabel1 = new JLabel();
jLabel2 = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jTextField2 = new JTextField();
jTextField3 = new JTextField();
jTextField4 = new JTextField();
jComboBox1 = new JComboBox();
jTextArea2 = new JTextArea();
jScrollPane5 = new JScrollPane();
jButton1 = new JButton();
contentPane = (JPanel)this.getContentPane();
//
// jLabel1
//
jLabel1.setText("jLoan Amount:");
//
// jLabel2
//
jLabel2.setText("jLoan Rate:");
//
// jLabel3
//
jLabel3.setText("Mortgage Payment Amount");
//
// jLabel4
//
jLabel4.setText("jLoan Balance:");
//
// jLabel5
//
jLabel5.setText("jInterest Paid for each payment over term of Loan");
//
// jTextField2
//
jTextField2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField2_actionPerformed(e);
}
});
//
// jTextField3
//
jTextField3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField3_actionPerformed(e);
}
});
//
// jTextField4
//
jTextField4.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jTextField4_actionPerformed(e);
}
});
//
// jComboBox1
//
jComboBox1.addItem("7 Years at 5.35%");
jComboBox1.addItem("15 Years at 5.5%");
jComboBox1.addItem("30 Years at 5.75%");
jComboBox1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jComboBox1_actionPerformed(e);
}
});
//
// jTextArea2
//
//
// jScrollPane5
//
jScrollPane5.setViewportView(jTextArea2);
//
// jButton1
//
jButton1.setText("Clear");
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}
});
//
// contentPane
//
contentPane.setLayout(null);
addComponent(contentPane, jLabel1, 32,21,77,18);
addComponent(contentPane, jLabel2, 35,67,60,18);
addComponent(contentPane, jLabel3, 33,117,143,18);
addComponent(contentPane, jLabel4, 41,163,89,18);
addComponent(contentPane, jLabel5, 6,206,244,18);
addComponent(contentPane, jTextField2, 181,115,127,22);
addComponent(contentPane, jTextField3, 188,164,100,22);
addComponent(contentPane, jTextField4, 272,203,100,22);
addComponent(contentPane, jComboBox1, 183,65,126,22);
addComponent(contentPane, jScrollPane5, 180,18,124,23);
addComponent(contentPane, jButton1, 36,240,83,28);
//
// MortgageCalculator
//
this.setTitle("MortgageCalculatorwk3 - extends JFrame");
this.setLocation(new Point(0, 0));
this.setSize(new Dimension(390, 300));
}
/** Add Component Without a Layout Manager (Absolute Positioning) */
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
}
//
// TODO: Add any appropriate code in the following Event Handling Methods
//
private void jTextField2_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jTextField3_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField3_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jTextField4_actionPerformed(ActionEvent e)
{
System.out.println("\njTextField4_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
private void jComboBox1_actionPerformed(ActionEvent e)
{
System.out.println("\njComboBox1_actionPerformed(ActionEvent e) called.");
Object o = jComboBox1.getSelectedItem();
System.out.println(">>" + ((o==null)? "null" : o.toString()) + " is selected.");
// TODO: Add any handling code here for the particular object being selected
}
private void jButton1_actionPerformed(ActionEvent e)
{
System.out.println("\njButton1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here
}
//
// TODO: Add any method code to meet your needs in following area
//
Here is my program that does the calculations:
JAVA Code:
import java.lang.Math.*;
import java.text.*;
import java.io.*;
public class MortgageCalculator
{
// (Main) method= displays all of the output in the program.
public static void main(String[] args)
{
// I am defining all of the variables I want to use.
double principle = 200000; // Amount to be borrowed for mortgage
int termArray[] = {84, 180, 360}; // Different loan terms in months.
double rateArray[] = {0.0535, 0.055, 0.0575};// Different interest rates for the loan.
double payment; // Monthly payment
double balance = 200000; // Current balance of mortgage (fluctuates with payments made).
double interestPaid; // Amount of interest paid for monthly payments.
int i = 0;
int i2 = 1;
int i3 = 1;
String pause; //Pause
// Should find the size of the array.
int size = rateArray.length;
// Correct Currency format(makes it easier for user to understand currency amount).
DecimalFormat decimalAmount = new DecimalFormat("#,##0.00");
for (i=0; i<size; i++)
{
// Finds the payments.
payment = CalculatePayment(principle, termArray[i], rateArray[i]);
// Shows the header.
System.out.println("\t\t\t\t Welcome to Harout's Mortgage Calculator");
System.out.println("\t\t\t\t Thank you for using my Mortgage program");
System.out.println("\t\t\t\t and Don't forget to have a nice day");
System.out.println(); //inserted a blank line to seperate and help space everything out.
// Should show correctly the principle term and the interest rate.
System.out.println();
System.out.println("Principle Amount for Mortgage:\t$" + decimalAmount.format(principle));
System.out.println("Interest Rate:\t\t" + rateArray[i] * 100 + "%");
System.out.println("Loan Term:\t\t" + termArray[i] / 12 + " Years");
System.out.println("Payment Amount:\t$" + decimalAmount.format(payment));
System.out.println(); //Blank lines make it easier for the user to distinguish between everything in the Dos screen
System.out.println(); //makes it easier to read for them.
// Pauses the screen before next mortgage values shown.
System.out.println("Press ENTER to view the amount of interest paid and balance remaining for each month");
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
pause = reader.readLine();
}
catch (IOException ioe) {}
// Will Cycle through each month until remaining balance is at exactly 0 amount.
while (balance > 0.01)
{
// Should find the interest for this month.
interestPaid = CalculateMonthlyInterest(balance, rateArray[i]);
// Will give an accurate update of the balance.
balance = balance - (payment - interestPaid);
System.out.println("Month: " + i2 + "\t Interest Rate: $" + decimalAmount.format(interestPaid) + "\tBalance Amount: $" + decimalAmount.format(balance));
i2++;
// Pauses the screen for every 18 months to make it easier to read(user-friendly feature).
if (i3 == 18)
{
// Pauses screen before next mortgage values shown. User Presses "Enter" key to continue on.
System.out.println("Press ENTER to continue");
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
pause = reader.readLine();
}
catch (IOException ioe) {}
i3=0;
}
i3++;
}
// Pauses screen before next mortgage values shown. User Presses "Enter" key to continue on.
System.out.println("Press ENTER to continue");
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
pause = reader.readLine();
}
catch (IOException ioe) {}
// Should reset the balance
balance = principle;
// Should reset the month counter
i2 = 1;
}
}
// Method responsible for calculating payment,principle,rate,and term given.
public static double CalculatePayment(double principle, int term, double rate)
{
// Does all the calculations and returns an amount for the payment.
return(principle * Math.pow(1+(rate/12), term) * rate/12) / (Math.pow(1 + rate/12, term) - 1);
}
// Calculate the interst paid for the current payment
public static double CalculateMonthlyInterest(double balance, double rate)
{
return(balance * (rate / 12)); //returns amount.
}
}
|