Mine is certainly not pretty, but all functionality is working now.

I was so tired by the time I finished, that I left off the "Total Payments" function. The line of code for interest free:
Code: Select all
monthPayment = principalBalance / loanTerm; //For an interest free loan
has already been converted to months earlier in the program (I should have mentioned that

):
Code: Select all
double loanTerm = Double.parseDouble(termText.getText()) * 12;
AssignTerm(loanTerm);
Could you take a look at the "finished" code for me, and maybe give me some help to get it looking more like your GUI?

To save space, I will collapse the code in a spoiler box.
Code: Select all
/**
*@author Administrator
*
* This application is a program for calculating loan payments.
* The user inputs the loan amount, the interest rate, and the loan term.
** The program returns the monthly payment for the user's variables.*/
//Imports for application
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
// Creates the mortgage calculator class
public class MortgageCalculator extends JFrame {
//Define the instance variables
private double amount;
private double rate;
private double term;
private double monthRate;
private double monthPay;
DecimalFormat Dollar = new DecimalFormat("$#,###.00");
// Create GUI components
private final JLabel loanLabel = new JLabel("Loan Amount");
private JTextField loanText = new JTextField(12);
private final JLabel intLabel = new JLabel("Interest Rate");
private JTextField intText = new JTextField(12);
private final JLabel termLabel = new JLabel("Loan Term");
private JTextField termText = new JTextField(12);
private final JLabel monthPayLabel = new JLabel("Monthly Payment");
private JTextField result = new JTextField(12);
public MortgageCalculator() {
//Define the calculate button
final JButton button = new JButton("Calculate");
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
calcButton();
}
});
//Define the reset button
final JButton buttonreset = new JButton("Reset");
buttonreset.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
resetButton();
}
});
//Creates and defines the panel, panel components, and layout
final JPanel panel = new JPanel();
setLayout(new BorderLayout());
add(panel, BorderLayout.SOUTH);
panel.setLayout(new GridLayout(6, 3, 20, 15));
panel.add(loanLabel);
panel.add(loanText);
panel.add(intLabel);
panel.add(intText);
panel.add(termLabel);
panel.add(termText);
panel.add(monthPayLabel);
panel.add(result);
panel.add(button);
panel.add(buttonreset);
setTitle("Mortgage Calculator");
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack(); //Set frame to size needed
}
public static void main(String[] args) {
MortgageCalculator calc = new MortgageCalculator();
}
//Set information for calulate button (amount,rate,term, and montly payment)
public void calcButton() {
double loanAmt = Double.parseDouble(loanText.getText());
AssignLoanAmount(loanAmt);
double loanRate = Double.parseDouble(intText.getText());
AssignLoanRate(loanRate);
double loanTerm = Double.parseDouble(termText.getText()) * 12;
AssignTerm(loanTerm);
CalculateMonthPay();
result.setText(Dollar.format(monthPay));
}
//Set information for reset button
public void resetButton() {
loanText.setText("");
intText.setText("");
termText.setText("");
result.setText("");
}
//Methods for value assignments (amount, rate, term)
public void AssignLoanAmount(double loanAmount) {
amount = loanAmount;
}
public void AssignLoanRate(double interestAmount) {
rate = interestAmount;
}
public void AssignTerm(double termAmount) {
term = termAmount;
}
public void CalculateMonthPay() {
//For a standard loan with interest
if (rate > 0) {
monthRate = rate / (100.00 * 12.00);
monthPay = amount * ((monthRate * (Math.pow((1 + monthRate), term)))
/ (Math.pow((1 + monthRate), term) - 1));
} else {
//If no interest rate is applicable
monthPay = amount / term;
}
}
}
If you see anything (syntax, structure, format, anything) that I should change for better programming, please let me know. I want, very much, to learn these concepts the best way possible. Thank you * 1,000,000!
