Good morning all. I've run into a slight stumbling block with some code I'm writing. I'm sure you've all seen the numerous Mortgage Calculator threads so I hope you're not too sick of them yet.
Basically, I need my code to first ask the user for a decision, then based upon that decision display one of two windows. I've already got one window that I need from the previous code. My problem is creating the second window. I want it to look and act almost identically to the first window. The difference being where the combo box is in the first, there would instead be text fields for the user to input that information. From there it would act exactly the same.
Your help is sincerely appreciated.
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.awt.event.*;
public class PRG421Week4 extends JFrame implements ActionListener, ItemListener {
private static boolean test;
double loanBalance; //Initialize loanBalance Variable
double userAmount ; //Initialize userAmount Variable
double userInterest; //Initialize userInterest Variable
double userTerm; //Initialize userTerm Variable
int months; //Initialize months Variable
double annualInterestRate [] = {5.35, 5.5, 5.75}; //Initialize APR Array
int years [] = {7, 15, 30}; //Initialize Term Array
double monthlyInterest; //Initialize monthlyInterest Variable
double loanMonths; //Initialize loanMonths Variable
double monthlyPayment; //Initialize monthlyPayments Variable
double currentMonthInterest; //Initialize currentMonthInterest Variable
double currentMonthPrinciple; //Initialize currentMonthPrinciple Variable
int answer;
DecimalFormat eachPayment = new DecimalFormat ("$###,##0.00");
//Define Row 1 Components
JPanel row1 = new JPanel();
JLabel lAmount = new JLabel(" Mortgage: $", JLabel.LEFT);
JTextField tAmount = new JTextField(10);
//Define Combo for Interest and Term
JLabel lcombo = new JLabel ("Term/Interest");
String cboData[] = {"Select Term","7 years at 5.35%","15 years at 5.50%","30 years at 5.75%"};
JComboBox comboTermInterest = new JComboBox(cboData);
//Define Payment Text Area
JLabel lPayment = new JLabel("Payment:");
JTextField tPayment = new JTextField(10);
//Define Row 2 Components
JPanel row2= new JPanel(new BorderLayout());
String pad = " ";
JLabel lHeader = new JLabel("Payment #"+pad+"Payment:"+pad+"Interest Paid:"+pad+"New Balance:");
JTextArea tResults = new JTextArea(10, 53);
JScrollPane tPane = new JScrollPane(tResults);
//Define Row 3 Components
JPanel row3 = new JPanel(new GridLayout(1,3,75,1));
JButton calculateButton = new JButton("Calculate");
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
//Define GUI Window
public PRG421Week4 (){
super("McBride Mortgage Calculator");
JFrame usercalculation = new JFrame();
usercalculation.setTitle("McBride Mortgage Calculator");
usercalculation.setSize(300,300);
usercalculation.setDefaultCloseOperation(usercalculation.EXIT_ON_CLOSE);
usercalculation.setVisible(false);
setBounds(350, 250, 600, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
row1.add(lAmount);
row1.add(tAmount);
row1.add(lcombo);
row1.add(comboTermInterest);
row1.add(lPayment);
row1.add(tPayment);
tPayment.setEditable(false);
tPane.setPreferredSize(new Dimension(610,200));
row2.add(lHeader,BorderLayout.NORTH);
row2.add(tPane,BorderLayout.CENTER);
//Create Calculate Button
comboTermInterest.addItemListener(this);
calculateButton.addActionListener(this);
clearButton.addActionListener(this);
exitButton.addActionListener(this);
row3.add(exitButton);
row3.add(clearButton);
row3.add(calculateButton);
getContentPane().add(row1,BorderLayout.NORTH);
getContentPane().add(row2,BorderLayout.CENTER);
getContentPane().add(row3,BorderLayout.SOUTH);
pack(); }
//Events for Combo Box
public void itemStateChanged(ItemEvent event) {
Object select = event.getItem();
String selection = select.toString();
if (selection == cboData[0]) {
}
if (selection == cboData[1]) {
userInterest = annualInterestRate [0];
userTerm = years [0];
}
if (selection == cboData[2]) {
userInterest = annualInterestRate [1];
userTerm = years [1];
}
if (selection == cboData[3]) {
userInterest = annualInterestRate [2];
userTerm = years [2];
}
}
//Define Events for Buttons
public void actionPerformed(ActionEvent e) {
if (e.getSource() == calculateButton)
{
userAmount = Double.parseDouble(tAmount.getText());
CalculateMonthlyPayment() ;
tPayment.setText( eachPayment.format( monthlyPayment)+" ");
repaint();
loanMonths = userTerm * 12; //Calculate # of Payments
loanBalance = userAmount;
for (months = 1; months <= loanMonths; months++) {
monthlyInterest = userInterest / (1200.00); //Monthly Interest
currentMonthInterest = loanBalance * monthlyInterest; //Calculate Interest Payment
currentMonthPrinciple = monthlyPayment - currentMonthInterest; //Calculate Principle Payment
loanBalance = loanBalance - currentMonthPrinciple; //Calculate Balance
//Display Results
tResults.append("" + months + "\t\t" + eachPayment.format(monthlyPayment)
+ "\t\t" + eachPayment.format(currentMonthInterest)
+ "\t\t"
+ eachPayment.format(loanBalance) + "\n"); }
}
else if (e.getSource() == clearButton) {
//clear fields
tAmount.setText ("");
tPayment.setText("");
userAmount = 0;
tPayment.setText("");
tResults.setText("");
}
else if (e.getSource() == exitButton) {
System.exit(0);
}
}
//Calculate the Ammortization
double CalculateMonthlyPayment(){
return monthlyPayment = (userAmount * ((1 + (userInterest*.01)/12)-1) / (1-(Math.pow(1 + (userInterest/1200),-(userTerm*12)))));
}
public static void main(String[] args){
test = true;
while(test){
String question = JOptionPane.showInputDialog("To cacluate a loan using unspecified Interest and Term type, '1'. To calculate a loan using specifed loan options, type '2'");
int answer = Integer.parseInt(question);
if (answer == 1){
//???????????????
}
else{
new PRG421Week4().setVisible(true);
test = false;
}
}
}
}