Need help PLEASE!!!
I have the following calculator that I am in desperate need of help with...
I have to make the switch statement into a seperate file and have it read into the program.
Can someone PLEASE give me some guidance and maybe a little nudge in the right direction? Here is my code so far:
import java.awt.*;
import java.text.*;
import java.applet.*;
import java.awt.event.*;
import java.awt.Container;
import java.lang.Object;
import javax.swing.*;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
publicclass MortgageCalcWk4extends JAppletimplements ActionListener
{
//Declaring Labels, TexFields and Greeting
JLabel greetingLabel =new JLabel("Welcome to My Mortgage Calculator");
JLabel amountLabel =new JLabel("Amount:");
JLabel rateLabel =new JLabel("Interest Rate: ");
JLabel yearsLabel =new JLabel("Years: ");
JLabel termLabel =new JLabel("Select your Terms");
JLabel payLabel =new JLabel("You're Mortgage Payment is:");
JLabel scheduleLabel =new JLabel("Below is your Amortization Schedule:");
JButton calculateButton =new JButton("Calculate");
JTextField amountField =new JTextField("", 10);
JTextField paymentField =new JTextField("", 10);
JTextField rateField =new JTextField("", 10);
JTextField yearsField =new JTextField("",10);
JTextArea amortText;
JScrollPane amortScroll;
String sOut =null;
publicvoid init()
{
//Setting up the Layout Manager
SpringLayout layout =new SpringLayout();
getContentPane().setLayout(layout);
paymentField.setEditable(false);
rateField.setEditable(true);
yearsField.setEditable(true);
amortText=new JTextArea(25,60);
amortScroll=new JScrollPane(amortText);
//Adjusting constraints for labels
layout.putConstraint(SpringLayout.WEST, greetingLabel,
250,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, greetingLabel,
0,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, amountLabel,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, amountLabel,
50,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, termLabel,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, termLabel,
100,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, payLabel,
130,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, payLabel,
188,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, scheduleLabel,
250,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, scheduleLabel,
235,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, rateLabel,
260,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, rateLabel,
50,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, yearsLabel,
510,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, yearsLabel,
50,
SpringLayout.NORTH, getContentPane());
//Adjusting constraints for textfields
layout.putConstraint(SpringLayout.WEST, amountField,
75,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, amountField,
48,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, paymentField,
300,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, paymentField,
186,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, rateField,
340,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, rateField,
48,
SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, yearsField,
552,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, yearsField,
48,
SpringLayout.NORTH, getContentPane());
//Adjusting constraints for combo boxes
layout.putConstraint(SpringLayout.WEST, terms,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, terms,
125, SpringLayout.NORTH, getContentPane());
layout.putConstraint(SpringLayout.WEST, amortScroll,
20, SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, amortScroll,
255, SpringLayout.NORTH, getContentPane());
//Adjusting constraint for button
layout.putConstraint(SpringLayout.WEST, calculateButton,
25,
SpringLayout.WEST, getContentPane());
layout.putConstraint(SpringLayout.NORTH, calculateButton,
185,
SpringLayout.NORTH, getContentPane());
//Displays Labels, TextFields and Combo box in the Applet
getContentPane().add(greetingLabel);
getContentPane().add(amountLabel);
getContentPane().add(amountField);
getContentPane().add(termLabel);
getContentPane().add(terms);
getContentPane().add(amortScroll,BorderLayout.CENTER);
getContentPane().add(calculateButton);
getContentPane().add(payLabel);
getContentPane().add(paymentField);
getContentPane().add(scheduleLabel);
getContentPane().add(rateLabel);
getContentPane().add(rateField);
getContentPane().add(yearsLabel);
getContentPane().add(yearsField);
calculateButton.addActionListener(this);
terms.addActionListener(this);
}
//Terms for combo box menu
String[] termsArray ={"7 years at 5.35%","15 years at 5.5%","30 years at 5.75%"};
JComboBox terms =new JComboBox(termsArray);
publicvoid actionPerformed(ActionEvent thisEvent)
{
amortText.setText("");
paymentField.setText("");
//Establishing local variables
double amt= 0;
double rate = 0;
double months = 0;
Object src = thisEvent.getSource();
if(src.equals(terms)){
int menuNum = terms.getSelectedIndex();
switch(menuNum){
case 0:
rate = 5.35;
months = 84;
break;
case 1:
rate = 5.5;
months = 180;
break;
case 2:
rate = 5.75;
months = 360;
break;
default:
rate = 5.35;
months = 84;
break;
}
rateField.setText("" + rate);
yearsField.setText("" + (months / 12));
}else{// if src.equals(calculateButton)
try{
amt = Double.parseDouble(amountField.getText());
rate = Double.parseDouble(rateField.getText());
months = Double.parseDouble(yearsField.getText());
}catch(NumberFormatException nfe){
JOptionPane.showMessageDialog(
null,
"Enter numeric values for all fields",
"Input Error",
JOptionPane.WARNING_MESSAGE);
return;
}
double dMonthlyInterest;
double dMonthlyPrincipal;
double dPayment=0;
double dBalance = amt;
//Formulas for determining payment amount
double dMonthly = (rate /1200);
dPayment = (amt * dMonthly) / (1-Math.pow(1+dMonthly, -(months*12)));
dMonthlyInterest = (amt * dMonthly);
dMonthlyPrincipal = (dPayment - dMonthlyInterest);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
DecimalFormat format =new DecimalFormat("0.00");
String currencyOut = currencyFormatter.format(dPayment);
paymentField.setText(currencyOut);
sOut =null;
for(int i=1; i<=(months*12); i++){
// Reduces Principle each month
dMonthlyInterest = (dBalance * dMonthly);
dMonthlyPrincipal = (dPayment - dMonthlyInterest);
dBalance = (dBalance - dMonthlyPrincipal);
//Determining monthly payment
dPayment = (amt * dMonthly) / (1 - Math.pow ((1 + dMonthly), - (months*12)));
if(sOut ==null){
sOut ="Payment " + Integer.toString(i) +"\tInterest Paid: "
+ currencyFormatter.format(dMonthlyInterest) +"\tRemaining Balance: "
+ currencyFormatter.format(dBalance) +"\r\n";
}else{
sOut = sOut +"Payment "+ Integer.toString(i) +"\tInterest Paid: "
+ currencyFormatter.format(dMonthlyInterest) +"\tRemaining Balance: "
+ currencyFormatter.format(dBalance) +"\r\n";
}
amortText.setText(sOut);
}
}
}
}

