I am having problems setting up drop down menue for a application I am trying to do for a class. Can someone please give me some pointers.
Here is the class assignment.
Write the program in Java (with a graphical user interface) 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%
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. Allow the user to loop back and enter a new amount and make a new selection or quit. Please insert comments in the program to document the program.
/*
Week 3 assignment
author: Chris Butler
Date: April 3, 2010
*/
import java.text.DecimalFormat;
import javax.swing.*;
import javax.swing.JTextField.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class PaymentCalculatorwk3 extends JFrame implements ActionListener
{
//Using Decimal format to display money
DecimalFormat money = new DecimalFormat ("$000,000.00");
DecimalFormat percent = new DecimalFormat ("0.0");
//Panel for user input
JPanel firstRow = new JPanel();
JPanel secondRow = new JPanel();
JPanel thirdRow = new JPanel();
JPanel fourthRow = new JPanel();
JPanel fifthRow = new JPanel();
JPanel sixthRow = new JPanel();
//Panel for Buttons
JPanel fieldPanel = new JPanel();
JPanel buttonPanel = new JPanel();
//Labels with descriptions
JLabel loanLabel = new JLabel("Principle amount");
JTextField p = new JTextField(8);
JLabel paymentLabel = new JLabel("Monthly Payment");
JLabel displaypayment = new JLabel();
JLabel interestLabel = new JLabel("Interest Payment");
JLabel displayinterest = new JLabel();
JLabel balanceLabel = new JLabel("Balance");
JLabel displaybalance = new JLabel();
//Create arrays for months and interest
public void actionPerformed(ActionEvent e) {
Months.addActionListener(this);
Interest.addActionListener(this);{
String[] Months = { "84", "180", "360"};
String[] Interest = { "5.35", "5.5", "5.75"};
JComboBox petList = new JComboBox(MonthsStrings);
Months.setSelectedIndex(4);
Months.addActionListener(this);
JComboBox InterestList = new JComboBox(InterestStrings);
InterestList.setSelectedIndex(5);
Interest.addActionListener(this);
JComboBox PaymentCalculatorwk3 = (JComboBox)e.getSource();
String Months = (String).getSelectedIndex(4);
String Interest = (String).getSelectedIndex(5);
updateLabel(Months);
updateLabel(Interest);
}
}
String[] Months = { "84", "180", "360"};
String[] Interest = { "5.35", "5.5", "5.75"};
JComboBox petList = new JComboBox(MonthsStrings);
Months.setSelectedIndex(4);
Months.addActionListener(this);
JComboBox InterestList = new JComboBox(InterestStrings);
InterestList.setSelectedIndex(4);
Interest.addActionListener(this);
//Create Buttons
JButton calculateButton = new JButton("Payment");
JButton exitButton = new JButton("Exit");
public static void main(String[] args)
{
PaymentCalculatorwk3 f = new PaymentCalculatorwk3();
f.setSize(450,350);
f.setTitle("Mortgage Calculator");
f.setLocation(300,350);
f.setVisible(true);
}
public PaymentCalculatorwk3()
{
Container c = getContentPane();
c.setLayout((new BorderLayout()));
fieldPanel.setLayout(new GridLayout(8,1));
FlowLayout rowSetup = new FlowLayout(FlowLayout.LEFT,5,3);
firstRow.setLayout(rowSetup);
secondRow.setLayout(rowSetup);
thirdRow.setLayout(rowSetup);
fourthRow.setLayout(rowSetup);
fifthRow.setLayout(rowSetup);
sixthRow.setLayout(rowSetup);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
//add fields to rows
firstRow.add(loanLabel);
firstRow.add(p);
secondRow.add(MonthsLabel);
secondRow.add(Q);
thirdRow.add(InterestLabel);
thirdRow.add(R);
fourthRow.add(paymentLabel);
fourthRow.add(displaypayment);
fifthRow.add(interestLabel);
fifthRow.add(displayinterest);
sixthRow.add(balanceLabel);
sixthRow.add(displaybalance);
//Add rows to panel
fieldPanel.add(firstRow);
fieldPanel.add(secondRow);
fieldPanel.add(thirdRow);
fieldPanel.add(fourthRow);
fieldPanel.add(fifthRow);
fieldPanel.add(sixthRow);
//add button to panel
buttonPanel.add(calculateButton);
buttonPanel.add(exitButton);
//Add panels to frame
c.add(fieldPanel, BorderLayout.CENTER);
c.add(buttonPanel, BorderLayout.SOUTH);
//add fuctionality to the button
calculateButton.addActionListener(this);
exitButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//Calculations for user input
PaymentCalculator();
}
public void PaymentCalculator()
{
//Calculate monthly payment of loan
double principle = Double.parseDouble(p.getText()); // Principle Loan Amount
double [] Months = Double.parseDouble(Q.getText()); // Number of years in months
double [] AnnualInterest = Double.parseDouble(R.getText()); // Annual Interest Amaount
double MonthlyInterest = 0;
double Balance = 0;
double PaidPerMonth = 0;
double CurrentMonthlyInterest = 0;
double NewBalance = 0;
double MonthlyPayment = 0;
MonthlyPayment = principle * (((AnnualInterest)/1200) / (1 - (Math.pow(1 + ((AnnualInterest)/1200), -(Months)))));
String displayMonthlyPayment = money.format (MonthlyPayment);
String displayMonthlyInterest = percent.format (MonthlyInterest);
String displayBalance = money.format (Balance);
paymentLabel.setText("" + MonthlyPayment);
interestLabel.setText(""+ MonthlyInterest);
balanceLabel.setText(""+ Balance);
}
}
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:72: <identifier> expected
Months.setSelectedIndex(4);
^
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:72: illegal start of type
Months.setSelectedIndex(4);
^
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:73: <identifier> expected
Months.addActionListener(this);
^
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:73: illegal start of type
Months.addActionListener(this);
^
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:75: <identifier> expected
InterestList.setSelectedIndex(4);
^
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:75: illegal start of type
InterestList.setSelectedIndex(4);
^
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:76: <identifier> expected
Interest.addActionListener(this);
^
F:\School\PRG421\Week 3\PaymentCalculatorwk3.java:76: illegal start of type
Interest.addActionListener(this);
^
8 errors
Tool completed with exit code 1
Please help. I am new to programming so any help would be nice.