I am over my head in my current class and could use help or pointers changing the program I have written. It is a mortgage calculator and I have to modify it. My class texts seem over my head and I have to drive 20 minutes and use public internet so tutorial online time is limited. My original program works but I am struggling to implement needed changes. My original program is:
import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MortgageCalc extends JFrame { //This creates all the buttons and windows
JLabel firstNumLabel = new JLabel("Mortgage Principal:");
JTextField firstNumField = new JTextField("0");
JLabel secondNumLabel = new JLabel("Mortgage Terms (in years):");
JTextField secondNumField = new JTextField("0");
JLabel thirdNumLabel = new JLabel("Annual Percentage Rate (APR)");
JTextField thirdNumField = new JTextField("0");
JLabel resultLabel = new JLabel("Payment Amount:");
JTextField resultField = new JTextField("0");
JButton calcButton = new JButton("Calculate Payment");
JButton closeButton = new JButton("Close");
JButton resetButton = new JButton("Reset Calculator");
public int pymtAmt = 0;
public int intRate = 0;
public int monMorTerms = 0;
public MortgageCalc() { //this creates the GUI
super("Mortgage Calculator");
setLocation(500, 200);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new GridLayout(6, 2, 10, 10));
add(firstNumLabel);
add(firstNumField);
add(secondNumLabel);
add(secondNumField);
add(thirdNumLabel);
add(thirdNumField);
add(resultLabel);
add(resultField);
add(calcButton);
add(closeButton);
add(resetButton);
resultField.setEditable(false);
closeButton.addActionListener( //defines what the close button does
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
calcButton.addActionListener( //defines what the calculate button does
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String firstNumText = firstNumField.getText();
String secondNumText = secondNumField.getText();
String thirdNumText = thirdNumField.getText();
double firstNum = Double.parseDouble(firstNumText);
double secondNum = Double.parseDouble(secondNumText);
double thirdNum = Double.parseDouble(thirdNumText);
double intRate = thirdNum / 100 / 12;
double monMorTerms = secondNum * 12;
double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
NumberFormat nf = NumberFormat.getCurrencyInstance();
resultField.setText("" + nf.format(result));
}
});
resetButton.addActionListener(new ActionListener() { //defines what the reset button does
public void actionPerformed(ActionEvent e) {
firstNumField.setText("0");
secondNumField.setText("0");
thirdNumField.setText("0");
resultField.setText("");
}
});
pack();
setVisible(true);
}
public static void main(String[] args) { //creates a new instance of the MortgageCalc class
MortgageCalc app = new MortgageCalc();
}
}
I need to change it so that the user still inputs the loan amount, but the terms are listed in a drop down box. The instructions say to use an array for the mortgage data for the different loans and then 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. I have finally gotten it to create a box with the layout I like, but making it all function is what I am struggling with. Here is my new code for the changes so far:
import java.awt.*;
import java.text.NumberFormat;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MortgageCalc extends JFrame { //This creates all the buttons and windows
JLabel firstNumLabel = new JLabel("Mortgage Principal:");
JTextField firstNumField = new JTextField("0");
JLabel resultLabel = new JLabel("Payment Amount:");
JTextField resultField = new JTextField("0");
JLabel comboOptions = new JLabel("Term Options");
JButton calcButton = new JButton("Calculate Payment");
JButton closeButton = new JButton("Close");
JButton resetButton = new JButton("Reset Calculator");
public int pymtAmt = 0;
public int intRate = 0;
public int monMorTerms = 0;
public int secondNumField;
public double thirdNumField;
String[] loanTerms = { "7 years at 5.35%", "15 years at 5.5%", "30 years at 5.75%" };
JComboBox mortgageTerms = new JComboBox(loanTerms);
public MortgageCalc() { //this creates an instance of the MortgageCalc class
super("Mortgage Calculator"); //this makes a title for the window
setLocation(500, 200); //this stes the location of the window
this.setDefaultCloseOperation(EXIT_ON_CLOSE); //this states the program will end when the window is closed
setLayout(new GridLayout(6, 2, 10, 10)); //this defines the layout of the window
add(firstNumLabel); //these lines add the elements to the window
add(firstNumField);
add(comboOptions);
add(mortgageTerms);
add(resultLabel);
add(resultField);
add(calcButton);
add(closeButton);
add(resetButton);
resultField.setEditable(false); //this indicates the result field can not be edited by the user
closeButton.addActionListener( //defines what the close button does
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
calcButton.addActionListener( //defines what the calculate button does
new ActionListener() {
public void actionPerformed(ActionEvent evt) {
String firstNumText = firstNumField.getText();
double firstNum = Double.parseDouble(firstNumText);
double intRate = 0;
double monMorTerms = 0;
double result = (firstNum * intRate) / (1 - Math.pow(1 + intRate, -monMorTerms));
NumberFormat nf = NumberFormat.getCurrencyInstance();
resultField.setText("" + nf.format(result));
}
});
resetButton.addActionListener(new ActionListener() { //defines what the reset button does
public void actionPerformed(ActionEvent e) {
firstNumField.setText("0");
resultField.setText("");
}
});
pack();
setVisible(true);
}
public static void main(String[] args) { //creates a new instance of the MortgageCalc class
MortgageCalc app = new MortgageCalc();
}
}
Any assistance or pointers, even if it is just to downloadable tutorials, would be appreciated. The assignment is most likely going to be late but I am not really concerned about that, the whole assignment is only worth 10 points. My main concerning is actually learning the material and so far the class texts are written above my comprehension level.