When I press the 7 year with 5.35% the amort table goes across the screen instead of down. When I press the 30 year with 5.75% it exit the program. I cannot figure out what I did wrong, will someone please check and help quick. Here are my codes. -
//Set up and initialize buttons for GUI
-
bCalc7 = new JButton ("7 Year, 5.32%");
-
bCalc7.setActionCommand ("Calculate7");
-
bCalc15 = new JButton ("15 Years, 5.50%");
-
bCalc15.setActionCommand ("Calculate15");
-
bCalc30 = new JButton ("30 Years, 5.75%");
-
bCalc30.setActionCommand ("Calculate30");
-
bClear = new JButton ("Clear All Fields");
-
bClear.setActionCommand ("Clear");
-
bExit = new JButton ("Exit Program");
-
bExit.setActionCommand ("Exit");
-
-
//Set up labels and field sizes for GUI
-
labelTitle = new JLabel ("McBride Mortgage Calculator V3.5");
-
labelInstructions = new JLabel ("Enter the amount of the Loan and then choose the term/rate of the loan.");
-
labelPayment = new JLabel ("Monthly Payment:");
-
labelLoanAmount = new JLabel ("Amount of Loan:");
-
fieldPayment = new JTextField ("", 12);
-
fieldLoanAmount = new JTextField ("", 10);
-
labelAmortTable = new JLabel ("Amortization Table");
-
areaAmortTable = new JTextArea (10, 300);
-
-
JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
-
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
-
-
//Set up listerners for each button
-
bCalc7.addActionListener (this);
-
bCalc15.addActionListener (this);
-
bCalc30.addActionListener (this);
-
bClear.addActionListener (this);
-
bExit.addActionListener (this);
-
-
//Construct GUI and set layout
-
JPanel calu = new JPanel();
-
calu.setLayout (null);
-
-
calu.add (labelTitle);
-
labelTitle.setBounds (110,30,700,15);
-
-
calu.add (labelInstructions);
-
labelInstructions.setBounds(30, 70, 450, 15);
-
-
calu.add (labelLoanAmount);
-
labelLoanAmount.setBounds(130, 110, 100, 25);
-
-
calu.add (fieldLoanAmount);
-
fieldLoanAmount.setBounds(240, 110, 100, 25);
-
-
calu.add (bCalc7);
-
bCalc7.setBounds(40, 150, 125, 30);
-
-
calu.add (bCalc15);
-
bCalc15.setBounds(180, 150, 125, 30);
-
-
calu.add (bCalc30);
-
bCalc30.setBounds(320, 150, 125, 30);
-
-
calu.add (labelPayment);
-
labelPayment.setBounds(130, 200, 100, 25);
-
-
calu.add (fieldPayment);
-
fieldPayment.setBounds(240, 200, 100, 25);
-
fieldPayment.setEditable(false);
-
-
calu.add (labelAmortTable);
-
labelAmortTable.setBounds(180, 250, 300, 25);
-
-
calu.add (scrollPane);
-
scrollPane.setBounds(50, 280, 400, 270);
-
areaAmortTable.setEditable(false);
-
-
calu.add (bClear);
-
bClear.setBounds(110, 570, 125, 30);
-
-
calu.add (bExit);
-
bExit.setBounds(250, 570, 125, 30);
-
-
this.setContentPane(calu);
-
this.pack();
-
this.setTitle("Mortgage Calculator5");
-
-
//Set windwo size
-
Dimension screenSize = Toolkit.getDefaultToolkit() .getScreenSize();
-
setSize (600, 700);
-
-
}
-
public double loanAmount ()
-
{
-
double loanAmount = Double.parseDouble (fieldLoanAmount.getText());
-
return loanAmount;
-
-
}
-
-
-
}
-
bCalc30.setActionCommand ("Calculate30");
else if ("calculate30".equals(e.getActionCommand()))
The case of C on "calculate30" differs.
Don't use exit as the default case. Do an explicit else if test for it. Then have the default case be some sort of error alert, eg ERROR button is calling command .... but it's not found.
8 1805
Where is the code for the actionCommands?
I am using the actionListener, it runs but those two buttons are not doing what they should, do I need to post my hold program. Thanks for any help you can give, I am to turn this in tonight.
The GUI code is useless for debugging eg:
calu.add (labelAmortTable);
There doesn't appear to be an error with the code posted so far.
Here is the the whole set up
package mortgagecalculator5; -
/**
-
*
-
*
-
*/
-
-
import java.text.*;
-
import java.math.*;
-
import java.io.*;
-
import java.awt.*;
-
import java.awt.event.*;
-
import javax.swing.*;
-
import java.lang.String;
-
import javax.swing.JLabel;
-
-
-
public class Main extends JFrame {
-
-
/**
-
* @param args the command line arguments
-
*/
-
public class MortgageCalculator5 {
-
}
-
{
-
-
}
-
public static void main(String[] args) {
-
JFrame window = new MortCalcu5GUI();
-
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
window.setVisible(true);
-
-
}
-
}
-
class MortCalcu5GUI extends JFrame implements ActionListener {
-
-
// Declare GUI function variables
-
protected JButton bCalc7, bCalc15, bCalc30, bClear, bExit;
-
protected JLabel labelPayment, labelLoanAmount, labelTitle, labelInstructions, labelAmortTable;
-
protected JTextField fieldPayment, fieldLoanAmount;
-
protected JTextArea areaAmortTable;
-
-
//Declare variables and loan array and initialize with values
-
double InterestPaid, PrinciplePaid, Balance, monthlyPay, loanAmount;
-
int[] loanTerm = {7, 15, 30}; //loan term for 7 years, 15 years, and 30 years
-
double[] intRate = {5.35, 5.50, 5.75};//interest rates for 7 years, 15 years, and 30 years
-
double loanAmt = loanAmount;
-
-
-
public void actionPerformed (ActionEvent e) {
-
-
//Perform actions based upon which button is pressed (provides looping function)
-
if ("Calculate7".equals(e.getActionCommand())) {
-
-
DecimalFormat decimalPlaces = new DecimalFormat ("0.00");
-
NumberFormat currency = NumberFormat.getCurrencyInstance();
-
double paymentAmount = CalculatePayment7();
-
fieldPayment.setText ("" + (currency.format(paymentAmount)));
-
areaAmortTable.append("Payment # \t Remaining Balance \t Interest Paid\n");
-
loanAmt =
-
Double.parseDouble (fieldLoanAmount.getText());
-
for (int x = 1; x<=(loanTerm[0]*12); x++)//Loop through all monthly payments for each loan
-
{
-
Balance = loanAmt;//Set Balance to Loan Amount
-
//Calculations on monthly payment
-
InterestPaid = (((intRate[0]/100)/12)*Balance);//Monthly Interest paid
-
PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
-
loanAmt = (Balance-PrinciplePaid);//loan balance after payment
-
areaAmortTable.append (x + "\t" + currency.format (loanAmt) + " \t\t" + currency.format(InterestPaid) + "n");
-
}
-
if (loanAmt <= 0)//ending statement, balance at $0.00
-
{
-
areaAmortTable.append ("\tRemaining balance = $0.00");
-
//paymentNumber = 0;
-
//InterestedPaid = 0.0;
-
}
-
-
}
-
else if ("Calculate15".equals(e.getActionCommand())){
-
-
DecimalFormat decimalPlaces = new DecimalFormat ("0.00");
-
NumberFormat currency = NumberFormat.getCurrencyInstance();
-
double paymentAmount = CalculatePayment15();
-
fieldPayment.setText ("" + (currency.format(paymentAmount)));
-
areaAmortTable.append ("Payment # \t Remaining Balance \t Interest Paid\n");
-
loanAmt = Double.parseDouble(fieldLoanAmount.getText());
-
for (int x = 1; x<=(loanTerm[1]*12); x++)//Loop through all monthly payment for each loan
-
{
-
Balance = loanAmt;
-
//Calculation on monthly payment
-
InterestPaid = ((intRate[1]/100/12)*Balance);//Monthly Interest paid
-
PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
-
loanAmt = ((Balance-PrinciplePaid));//loan balance after payment
-
areaAmortTable.append (x + '\t' + currency.format (loanAmt) + "\t\t" + currency.format (InterestPaid)+ "\n");
-
}
-
if (loanAmt <= 0)//ending statement, blance at $0.00
-
{
-
areaAmortTable.append ("\tRemaining balance = $0.00");
-
//paymentNumber =0;
-
//InterestPaid = 0.0;
-
}
-
}
-
else if ("calculate30".equals(e.getActionCommand()))
-
{
-
DecimalFormat decimalPlaces = new DecimalFormat("0.00");
-
NumberFormat currency = NumberFormat.getCurrencyInstance();
-
double paymentAmount = CalculatePayment30();
-
fieldPayment.setText ("" + (currency.format (paymentAmount)));
-
areaAmortTable.append ("payment # \t Remaining Balance \t Interest Paid\n");
-
loanAmt = Double.parseDouble (fieldLoanAmount.getText());
-
for (int x = 1; x <=(loanTerm[2]*12); x++)//Loop through all monthly payments for each loan
-
{
-
Balance = loanAmt;
-
//Calulations on monthly payments
-
InterestPaid = ((intRate[2]/100/12)*Balance);//Monthly Interest Paid
-
PrinciplePaid = (paymentAmount-InterestPaid);//Applied toward principle
-
loanAmt = ((Balance-PrinciplePaid));//loan balance after payment
-
areaAmortTable.append (x + "\t" + currency.format(loanAmt) + "\t\t" + currency.format(InterestPaid)+"\n");
-
}
-
if (loanAmt <= 0)//ending statement, balance at $0.00
-
{
-
areaAmortTable.append ("\tRemaining balance = $0.00");
-
//paymentNumber = 0;
-
//InterstPaid =0.0;
-
}
-
-
}
-
else if ("Clear".equals(e.getActionCommand()))
-
{
-
ClearFields();
-
}
-
else
-
{
-
//end and close application
-
System.exit(0);
-
}
-
}
-
public double CalculatePayment7 ()
-
{
-
//Check for numeric input
-
try
-
{
-
//Perform payment calculations if input is valid
-
fieldPayment.setText ("");
-
double Payment = (loanAmount()* (intRate[0]/100/12)) / (1- Math.pow(1/(1 + (intRate[0]/100/12)), loanTerm[0]*12));
-
return Payment;
-
}
-
catch (NumberFormatException event)
-
{
-
//Display error message if input is invalid
-
JOptionPane.showMessageDialog(null, "Invalid Entry!\nPlease enter only numeric values!!","ERROR",
-
JOptionPane.ERROR_MESSAGE);
-
return 0;
-
}
-
}
-
public double CalculatePayment15()
-
{
-
//Check for valid input
-
try
-
{
-
//Perform payment calculations if input is valid
-
fieldPayment.setText ("");
-
double Payment = (loanAmount( )* (intRate[1]/100/12)) / (1 - Math.pow(1/(1 +(intRate[1]/100/12)),loanTerm[1]*12));
-
return Payment;
-
}
-
catch (NumberFormatException event)
-
{
-
//Display erroe message if input is invalid
-
JOptionPane.showMessageDialog(null, "Invalid entry!\nPlease enter only numeric values!!",
-
"ERROR", JOptionPane.ERROR_MESSAGE);
-
return 0;
-
}
-
}
-
public double CalculatePayment30()
-
{
-
//Check for vilid numeric input
-
try
-
{
-
//Perform payment calculation if input in valid
-
fieldPayment.setText ("");
-
double Payment = (loanAmount()* (intRate[2]/100/12))/(1 - Math.pow (1/(1 + (intRate[2]/100/12)), loanTerm[2]*12));
-
return Payment;
-
}
-
catch (NumberFormatException event)
-
{
-
//Display error message if input is invalid
-
JOptionPane.showMessageDialog(null, "Invalid Entry!\nPlease enter only numeric values!!",
-
"ERROR", JOptionPane.ERROR_MESSAGE);
-
return 0;
-
}
-
}
-
public void ClearFields()
-
{
-
//Set all text to blank
-
fieldPayment.setText ("");
-
fieldLoanAmount.setText ("");
-
areaAmortTable.setText ("");
-
}
-
public MortCalcu5GUI ()
-
{
-
//Set up and initialize buttons for GUI
-
bCalc7 = new JButton ("7 Year, 5.32%");
-
bCalc7.setActionCommand ("Calculate7");
-
bCalc15 = new JButton ("15 Years, 5.50%");
-
bCalc15.setActionCommand ("Calculate15");
-
bCalc30 = new JButton ("30 Years, 5.75%");
-
bCalc30.setActionCommand ("Calculate30");
-
bClear = new JButton ("Clear All Fields");
-
bClear.setActionCommand ("Clear");
-
bExit = new JButton ("Exit Program");
-
bExit.setActionCommand ("Exit");
-
-
//Set up labels and field sizes for GUI
-
labelTitle = new JLabel ("McBride Mortgage Calculator V3.5");
-
labelInstructions = new JLabel ("Enter the amount of the Loan and then choose the term/rate of the loan.");
-
labelPayment = new JLabel ("Monthly Payment:");
-
labelLoanAmount = new JLabel ("Amount of Loan:");
-
fieldPayment = new JTextField ("", 12);
-
fieldLoanAmount = new JTextField ("", 10);
-
labelAmortTable = new JLabel ("Amortization Table");
-
areaAmortTable = new JTextArea (10, 300);
-
-
JScrollPane scrollPane = new JScrollPane (areaAmortTable, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
-
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
-
-
//Set up listerners for each button
-
bCalc7.addActionListener (this);
-
bCalc15.addActionListener (this);
-
bCalc30.addActionListener (this);
-
bClear.addActionListener (this);
-
bExit.addActionListener (this);
-
-
//Construct GUI and set layout
-
JPanel calu = new JPanel();
-
calu.setLayout (null);
-
-
calu.add (labelTitle);
-
labelTitle.setBounds (110,30,700,15);
-
-
calu.add (labelInstructions);
-
labelInstructions.setBounds(30, 70, 450, 15);
-
-
calu.add (labelLoanAmount);
-
labelLoanAmount.setBounds(130, 110, 100, 25);
-
-
calu.add (fieldLoanAmount);
-
fieldLoanAmount.setBounds(240, 110, 100, 25);
-
-
calu.add (bCalc7);
-
bCalc7.setBounds(40, 150, 125, 30);
-
-
calu.add (bCalc15);
-
bCalc15.setBounds(180, 150, 125, 30);
-
-
calu.add (bCalc30);
-
bCalc30.setBounds(320, 150, 125, 30);
-
-
calu.add (labelPayment);
-
labelPayment.setBounds(130, 200, 100, 25);
-
-
calu.add (fieldPayment);
-
fieldPayment.setBounds(240, 200, 100, 25);
-
fieldPayment.setEditable(false);
-
-
calu.add (labelAmortTable);
-
labelAmortTable.setBounds(180, 250, 300, 25);
-
-
calu.add (scrollPane);
-
scrollPane.setBounds(125, 280, 400, 270);
-
areaAmortTable.setEditable(false);
-
-
calu.add (bClear);
-
bClear.setBounds(110, 570, 125, 30);
-
-
calu.add (bExit);
-
bExit.setBounds(250, 570, 125, 30);
-
-
this.setContentPane(calu);
-
this.pack();
-
this.setTitle("Mortgage Calculator5");
-
-
//Set windwo size
-
Dimension screenSize = Toolkit.getDefaultToolkit() .getScreenSize();
-
setSize (675, 700);
-
-
}
-
public double loanAmount ()
-
{
-
double loanAmount = Double.parseDouble (fieldLoanAmount.getText());
-
return loanAmount;
-
-
}
-
-
-
}
-
bCalc30.setActionCommand ("Calculate30");
else if ("calculate30".equals(e.getActionCommand()))
The case of C on "calculate30" differs.
Don't use exit as the default case. Do an explicit else if test for it. Then have the default case be some sort of error alert, eg ERROR button is calling command .... but it's not found.
Thank you that fixed that field it works, but the Calculate7 still scrolls across the page instead of down like the others and it has an n. here is what it look like
$8,828.92n3 ------->1,940,629.66, and so on the arrow line is not there just to show you the direction it is going. It is set up the same as the others. I found one other error there corrected it but it still does the same thing.
You noticed the n?
You put "n" instead of "\n", like in the other lines.
I notice you have a lot of repeated code.
You might want to consider merging the code together, simply changing the variables that differ...
eg: -
if ("Calculate".equals(e.getActionCommand().Substring(0,9)){
-
...
-
double interestRate;
-
if ("Calculate7".equals(e.getActionCommand().Substring(0,9)){
-
double paymentAmount = CalculatePayment7();
-
interestRate = intRate[0];
-
} else if ("Calculate15".equals(e.getActionCommand().Substring(0,9)){
-
double paymentAmount = CalculatePayment15();
-
interestRate = intRate[1];
-
} else if ....
-
...
-
}
-
Thank you, you must been doing this a long time. Again Thanks
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ralph Freshour |
last post by:
Is it possible to inhibit the browser Back/Fwd buttons via PHP?
Thanks...
|
by: Matt |
last post by:
The ASP page has multiple buttons, and when the user clicks different
buttons, it will submit the form data to different URLs.
My first approach was to use BUTTON type, and triggers javascript...
|
by: longtim |
last post by:
I have been trying to implement navigation buttons for a number of
forms in my application.
Creating the 'Next' 'Previous' buttons has been fine but setting the
enabled properties doesn't work...
|
by: timm.wong |
last post by:
Hi,
I have a form where i have a thread working in the background that
tests a connection to a server. If the connection is not
successful,then I want it to display 2 buttons on the form. I...
|
by: AA Arens |
last post by:
During my database construction, I left the names of the buttons,
comboboxes named by default like Combo77, Command43. I can rename them
with Properties Other Name, but them I start getting...
|
by: promiscuoustx |
last post by:
The problem is that my code below used to run wonderfully, until the instructor decided that he wants to use characters instead of integers, and wants my code to trap the bad and have the program...
|
by: cameron |
last post by:
Hi I am new here in this forum: I am writing a C++ program to calculate a Montly Mortgage Payment where the loan amount is 200,000.00 with a 5.75% interest rate with a term of 30 years. My program...
|
by: phjones |
last post by:
Need help programming mortagage calculator for 3 different loans 7 year, 15 year and 30 year. using java array I am a beginner with Java, This is what I have so far. Need to know if I am off the...
|
by: phjones |
last post by:
please help resolve some error messages code is compling with errors
see below the code. I am new at this please help!
/**
* @(#)3 Mortgage loans.java
*
* 3 Mortgage loans application
*
...
|
by: phjones |
last post by:
This is not a class project.The program below is to display mortgage interest paid for each payment over the term of the loan and loan balance.It is program using array. However, I am receiving the...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |