/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package mortcalc5;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.table.DefaultTableModel;
/**
*
* @author
*
*
*/
public class Main extends JFrame {
static String mortgage[] = { "Select Mortgage", "30 years at 5.75%", "15 years at 5.5%", "7 years at 5.35%", "Other" };
static String[] toReplace;
DecimalFormat money = new DecimalFormat("$###,###.00");
// Define the JPanel where we will draw and place all of our components.
JPanel contentPanel = null;
// GUI elements to display labels, fields, table, scroll pane for current
// information
JLabel main_title = null;
JLabel principal_label = null;
JTextField principal_text = null;
JLabel years_label = null;
JTextField years_text = null;
JLabel rate_label = null;
JTextField rate_text = null;
JLabel choose_label = null;
JComboBox choose_combo = null;
JRadioButton rbutton01 = null;
JRadioButton rbutton02 = null;
ButtonGroup buttongroup = null;
// GUI text area for results with scroll pane
JTextArea result = null;
JScrollPane scrolling_result = null;
// GUI for Table with scroll pane
MyJTable total_amounts = null;
JScrollPane scrolling_table = null;
MyDTableModel model;
// Creates the button for calculation of the mortgage payments
JButton btnCalculate = null;
// Creates a button for reset button
JButton btnReset = null;
// Declaration of variables
int numberOfLinesGenerated=1;
int number_years; //term mortgage
double principal; // amount borrowed
double interest_rate; // interest for mortgage
double monthly_payment; // monthly payment for mortgage
double monthly_interest_rate ; // monthly interest
int number_of_months; // number of months for mortgage payments
double interest_paid; //interest amount added for each month
double principal_paid;
// This is the class constructor - initialize the components
public Main() {
super();
initialize();
}
// Window size, JPanel, setTitle
public void initialize() {
this.setSize(720, 730);
this.setContentPane(getJPanel());
this.setTitle("McBride Financial");
}
public JPanel getJPanel() {
if (contentPanel == null) {
contentPanel = new JPanel();
contentPanel.setLayout(null);
contentPanel.setBackground(Color.blue);
// GUI elements to display labels and fields for current information
// to include Alignment, Boundary, Title, Font, Font Size, Color
// Main Title of the Calculator
main_title = new JLabel();
main_title.setHorizontalAlignment(SwingConstants.CENTER);
main_title.setBounds(130, 20, 400, 30);
main_title.setText("Loan Amortization Calculator");
main_title.setFont(new Font("Arial", Font.BOLD, 25));
main_title.setForeground(Color.white);
contentPanel.add(main_title);
// Principal Label
principal_label = new JLabel();
principal_label.setHorizontalAlignment(SwingConstants.RIGHT);
principal_label.setBounds(90, 65, 220, 25);
principal_label.setText("Mortgage Principle Amount : ");
principal_label.setFont(new Font("Arial", Font.BOLD, 13));
principal_label.setForeground(Color.white);
contentPanel.add(principal_label);
// Principal Text Field Features
principal_text = new JTextField();
principal_text.setBounds(350, 65, 160, 25);
contentPanel.add(principal_text);
// Mortgage Plan Label
choose_label = new JLabel();
choose_label.setHorizontalAlignment(SwingConstants.RIGHT);
choose_label.setBounds(90, 125, 220, 25);
choose_label.setText("Select Mortgage Plan : ");
choose_label.setFont(new Font("Arial", Font.BOLD, 13));
choose_label.setForeground(Color.white);
contentPanel.add(choose_label);
// String for Mortgage Variation
String mortgage[] = { "Select Mortgage", "30 years at 5.75%","15 years at 5.5%", "7 years at 5.35%", "Other" };
choose_combo = new JComboBox(mortgage);
choose_combo.setBounds(350, 125, 180, 30);
choose_combo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
setMortgage();
}
});
contentPanel.add(choose_combo);
// Text Area for result
result = new JTextArea(5, 20);
result.setBounds(170, 250, 350, 75);
scrolling_result = new JScrollPane(result);
contentPanel.add(result);
//Table for amortization calculations
total_amounts = new MyJTable();
scrolling_table = new JScrollPane (total_amounts);
scrolling_table.setBounds(25, 450, 650, 200);
contentPanel.add(scrolling_table);
// Number of Years Label
years_label = new JLabel();
years_label.setHorizontalAlignment(SwingConstants.RIGHT);
years_label.setBounds(90, 200, 220, 25);
years_label.setText("Mortgage Term : ");
years_label.setFont(new Font("Arial", Font.BOLD, 13));
years_label.setForeground(Color.white);
contentPanel.add(years_label);
// Number of Years Text Field
years_text = new JTextField();
years_text.setBounds(350, 205, 160, 25);
contentPanel.add(years_text);
// Annual Interest Rate Label
rate_label = new JLabel();
rate_label.setHorizontalAlignment(SwingConstants.RIGHT);
rate_label.setBounds(90, 165, 220, 25);
rate_label.setText("Annual Interest Rate(Decimal) : ");
rate_label.setFont(new Font("Arial", Font.BOLD, 13));
rate_label.setForeground(Color.white);
contentPanel.add(rate_label);
// Annual Interest Rate Text Field
rate_text = new JTextField();
rate_text.setBounds(350, 165, 160, 25);
contentPanel.add(rate_text);
// Button for reset button
btnReset = new JButton();
btnReset.setBounds(355, 350, 110, 30);
btnReset.setText("Reset");
contentPanel.add(btnReset);
// Button for calculation of the mortgage payments
btnCalculate = new JButton();
btnCalculate.setBounds(245, 350, 110, 30);
btnCalculate.setText("Calculate");
contentPanel.add(btnCalculate);
// Action listener to the reset button
btnReset.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onButtonreset();
}
});
btnCalculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
principal=getDouble(principal_text.getText());
calculate();
{
//Gets the values from the combo box
String str = (String)choose_combo.getSelectedItem();
if (str.equals("30 years at 5.75%")) {
interest_rate = 0.0575;
number_years = 30;
}
else
if(str.equals("15 years at 5.5%")) {
interest_rate = 0.055;
number_years = 15;
}
else
if(str.equals("7 years at 5.35%")) {
interest_rate = 0.0535;
number_years = 15;
}
else
if (str.equals("Other")){
interest_rate = "";
number_years = "";
}
}
//Calculates the number of months for mortgage
number_of_months = number_years * 12;
//Caluclates the monthly interest rate
monthly_interest_rate = interest_rate/12.0;
//Calculates the monthly payment for mortgage
monthly_payment = (principal* monthly_interest_rate)/(1 - Math.pow(1 + monthly_interest_rate, -number_of_months));
// Sets the result text
result.setText("Loan Amount = " + money.format(principal)
+"\nInterest Rate = " + (interest_rate * 100) +"%"+"\nLength of Loan = "
+ Math.round(number_years * 12.0) + " months"+"\nMonthly Payment = "
+ money.format(monthly_payment)); //calling the monthly_payment
// Have the necessary information for the model lets recalculate it.
if(total_amounts != null){
total_amounts.setModel(getModel());
}
}
});
//Adds the calculate button
contentPanel.add(btnCalculate);
//Adds the scroll pane
contentPanel.add(getScrollPane());
}
return contentPanel;
}
//setMortgage Calculation//
public void setMortgage() {
String str = (String) choose_combo.getSelectedItem();
if (str.equals("30 years at 5.75%")) {
rate_text.setText("0.0575");
years_text.setText("30");
}
else
if(str.equals("15 years at 5.5%")) {
rate_text.setText("0.055");
years_text.setText("15");
}
else
if(str.equals("7 years at 5.35%")) {
rate_text.setText("0.0535");
years_text.setText("7");
}
else
if(str.equals("Other")){
rate_text.setText("");
years_text.setText("");
}
}
// Information for Reset Button//
public void onButtonreset() {
principal_text.setText("");
result.setText("");
rate_text.setText("");
years_text.setText("");
}
// Calculate() call//
public void calculate() {
double principal = getDouble(principal_text.getText());
int number_of_years = Integer.parseInt(years_text.getText());
double interest_rate = getDouble(rate_text.getText());
if(interest_rate > 1.0)interest_rate = interest_rate/100.0;
// Calculates the number of months for mortgage so as to determine the number of payments to be made
int number_of_months = number_of_years * 12;
// Calulates the monthly interest rate
double monthly_interest_rate = interest_rate / 12.0;
// Equation that calculates the monthly payment for mortgage
double monthly_payment = (principal * monthly_interest_rate)/ (1 - Math.pow(1 + monthly_interest_rate, -number_of_months));
// Sets the result text
result.setText("Loan Amount = " + money.format(principal)
+ "\nInterest Rate = " + (interest_rate * 100) + "%"
+ "\nLength of Loan = " + Math.round(number_of_years * 12.0)
+ " months" + "\nMonthly Payment = "
+ money.format(monthly_payment)); // calling the monthly payment
}
// This the Scroll pane for the table - sets the boundaries//
public JScrollPane getScrollPane() {
if (scrolling_table == null) {
scrolling_table = new JScrollPane();
scrolling_table.setBounds(65, 225, 425, 120);
scrolling_table.setViewportView(getTable());
}
return scrolling_table;
}
public JTable getTable() {
if (total_amounts == null) {
total_amounts = new MyJTable(getModel());
}
return total_amounts;
}
//This will calculate the model for the table//
public MyDTableModel getModel() {
String[] columnNames = {"Payment #", "Starting Balance", "Monthly Payment","Interest Paid","Principal Paid", "Ending Balance"};
model = null;
model = new MyDTableModel();
for (int i = 0; i < 6; i++) {
model.addColumn(columnNames[i]);
}
// Data to calculate the table
if (principal > 0 && interest_rate != 0 && number_years != 0) {
double new_principal = principal;
NumberFormat nf = NumberFormat.getCurrencyInstance();
for (int i = 0; i < number_of_months; i++) {
Object data[] = new Object[6];
data[0] = Integer.toString(i + 1);
data[1] = nf.format(new_principal);
data[2] = nf.format(monthly_payment);
data[3] = nf.format(interest_paid = principal * monthly_interest_rate);
data[4] = nf.format(principal_paid = monthly_payment - interest_paid);
data[5] = nf.format(principal = principal - principal_paid);
new_principal = principal;
model.addRow(data);
}
}
return model;
}
public double getDouble(String val) {
double value = 00;
try {
// This tests to see if there is a dollar sign
if (val.indexOf('$') > -1) {
value = NumberFormat.getCurrencyInstance().parse(val)
.doubleValue();
} else {
value = NumberFormat.getNumberInstance().parse(val)
.doubleValue();
}
} catch (java.text.ParseException e) {
// Generates an error here
JOptionPane.showMessageDialog(this, "There is an error " + val
+ ". Please check your entry", "Data Entry Error",
JOptionPane.ERROR_MESSAGE);
}
return value;
}
public double getPercent(String val) {
boolean isPercent = false;
double value = 0;
try {
if (val.indexOf('%') > -1) {
value = NumberFormat.getPercentInstance().parse(val).doubleValue();
isPercent = true;
} else {
value = NumberFormat.getNumberInstance().parse(val).doubleValue();
}
} catch (java.text.ParseException e) {
JOptionPane.showMessageDialog(this, "There is an error " + val + ". Please check your entry", "Data Entry Error",
JOptionPane.ERROR_MESSAGE);
}
// Have a percentage already in decimal format
if (!isPercent)
value = value / 100.0;
return value;
}
// main method
public static void main(String[] args) {
// Using Nimbus "Look and Feel" to make the layout more appealing
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (UnsupportedLookAndFeelException e) {
// handle exception
} catch (ClassNotFoundException e) {
// handle exception
} catch (InstantiationException e) {
// handle exception
} catch (IllegalAccessException e) {
// handle exception
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main thisClass = new Main();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
class MyDTableModel extends DefaultTableModel {
public Class<? extends Object> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
}
//This class adds the table to the JFrame
class MyJTable extends JTable {
public MyJTable(){
super();
}
public MyJTable(DefaultTableModel m){
super(m);
}
}
}//End Program
Manual entry for mortgage calculator
Page 1 of 15 Replies - 363 Views - Last Post: 14 April 2012 - 01:13 PM
#1
Manual entry for mortgage calculator
Posted 14 April 2012 - 11:08 AM
What I am having to accomplish is some form of manual entry for this calculator, this way he user also has the option to enter in their own requirements of interest and length of the mortgage. In order to accomplish this I have added an option for "Other" from the drop down box allowing the user to manually enter in their own information but it seems that the program does no seem to like the blank entries for his selection and is throwing an error relating to incompatible types on lines 229 and 230 stating that a double is required, but when I try to simply put "double" in front of the lines then it throws an error on line 228. Any way of correcting this problem that I am missing?
Replies To: Manual entry for mortgage calculator
#2
Re: Manual entry for mortgage calculator
Posted 14 April 2012 - 11:43 AM
if (str.equals("Other")){
interest_rate = "";
number_years = "";
}
And:
int number_years; //term mortgage double interest_rate; // interest for mortgage
What you're trying to do, is assigning a string to an integer and a double variable. Obviously, this is not valid. As you probably want default value to be 0 (or some other value), change the given piece of code into
if (str.equals("Other")){
interest_rate = 0; //Or some other value
number_years = 0.0; //Or some other value
}
#3
Re: Manual entry for mortgage calculator
Posted 14 April 2012 - 12:00 PM
If I change the variables to equal 0, then when I try to change them to something else of my choosing I get this to display for a mortgage of $5,000:
Loan Amount = $5,000.00
Interest Rate = 0.0%
Length of Loan = 0 months
Monthly Payment = �
By defaulting the variables to zero then it does not allow me to manually enter in anything else even if I do alter the entry boxes to other numbers of my choice. I would assume that there is another way but nothing that I can really think of right off the top of my head.
Loan Amount = $5,000.00
Interest Rate = 0.0%
Length of Loan = 0 months
Monthly Payment = �
By defaulting the variables to zero then it does not allow me to manually enter in anything else even if I do alter the entry boxes to other numbers of my choice. I would assume that there is another way but nothing that I can really think of right off the top of my head.
#4
Re: Manual entry for mortgage calculator
Posted 14 April 2012 - 12:24 PM
Ah, now I see what you try to do. When you pick "other" as an option, you want the user to be able to fill in its own Interest Rate and the length of the loan (in months)
In that case, you need to retrieve the entry in the corresponding textboxes and parse them. This would result in:
This code will parse the user entered values in the according text fields and uses the values to set the corresponding variables.
This should do the trick for you
Edit: Sorry I misread the opening post
In that case, you need to retrieve the entry in the corresponding textboxes and parse them. This would result in:
if (str.equals("Other")){
interest_rate = Double.parseDouble(rate_text.getText());
number_years = Integer.parseInt(years_text.getText());
}
This code will parse the user entered values in the according text fields and uses the values to set the corresponding variables.
This should do the trick for you
Edit: Sorry I misread the opening post
This post has been edited by karabasf: 14 April 2012 - 12:30 PM
#5
Re: Manual entry for mortgage calculator
Posted 14 April 2012 - 01:09 PM
omgosh, thanks!!! I have spent 2 weeks trying to get this to work. Once I see your code it makes perfect sense to me, but for some reason I could not figure it out for the life of me why it would not work the way that I wanted it to. Now I can move on to complete this week's assignment ... wish me luck!
#6
Re: Manual entry for mortgage calculator
Posted 14 April 2012 - 01:13 PM
Glad I could help 
Good luck with the next assignment
Good luck with the next assignment
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|