This is my calculator. I am trying to add the user input option. The combo box works with the calculate button, however, the user input option does not work with the calculate button. I only get $oo as the output in the Payment text field. Could someone please give me some help in finding a solution? I am not asking to provide me with code, I just need an idea of where I went wrong. Here is my code.
import javax.swing.*;
import java.awt.*;
import java.text.*;
import java.awt.event.*;
public class Week4B extends JFrame implements ActionListener, ItemListener
{
double PV = 0.0;//Principle Value
double NPV = 0.00;//New Principal Value
double IR= 0.0;//Interest Rate
double YIR[] = {5.35, 5.5, 5.75};//Yearly Interest
double IRM=0;//Interest Monthly
double NI = 0.00;//New Interest
double NP= 0.0;//Note Period or Term
double MoL = 0.0;//Months of Loan
double PMT = 0.00;//Payment
double Bal = 0;//Balance
int MO=0;//Month
int YRS[] = {7, 15, 30};//Years
double Years = 0;
String ComboBx[] = {"Select Term","7 years at 5.35%","15 years at 5.50%","30 years at 5.75%"};//Combo Box Data
DecimalFormat Payment = new DecimalFormat ("$###,##0.00");;//Sets Decimal Format
JPanel row1 = new JPanel();//Creates Row 1
//GUI
// row 1 labels and Fields
JLabel PVlbl = new JLabel(" Principal Value:", JLabel.LEFT);//Principal Value Label
JTextField PVtxt = new JTextField(10);//Principal Value Field
JLabel YRSlbl = new JLabel ("Years");//Years of loan
JTextField YRStxt = new JTextField(3);//years value field
JLabel IRlbl = new JLabel ("Interest");//Years of loan
JTextField IRtxt = new JTextField(4);//years value field
JLabel ComboBxlbl = new JLabel ("Loan Terms");//Combo Box Label
JComboBox ComboBxTerms = new JComboBox(ComboBx);//Combo Box
JLabel PMTlbl= new JLabel("Payment:");//Payment Label
JTextField PMTtxt = new JTextField(10);//Payment Field
//row 2 Labels and Fields
JPanel row2 = new JPanel(new GridLayout(1,3,75,1));
JButton calculateButton = new JButton("Calculate");
JButton clearButton = new JButton("Clear");
JButton exitButton = new JButton("Exit");
// row3 Labels and Fields
JPanel row3= new JPanel(new BorderLayout());
String pad = " ";//Padding white space
JLabel Resultslbl = new JLabel("Payment#"+pad+"Payment:"+pad+"Interest"+pad+"Balance");//head for results field
JTextArea Resultstxt = new JTextArea(10, 53);//Results field
JScrollPane textPane = new JScrollPane(Resultstxt);
//Build GUI
public Week4B()
{
super("Debs Mortgage Calculator Week 4");
setBounds(350, 250, 600, 250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Row 1 objects
row1.add(PVlbl);
row1.add(PVtxt);
row1.add(YRSlbl);
row1.add(YRStxt);
row1.add(IRlbl);
row1.add(IRtxt);
row1.add(ComboBxlbl);
row1.add(ComboBxTerms);
row1.add(PMTlbl);
row1.add(PMTtxt);
PMTtxt.setEditable(false);//Sets Payment field so user does not input data
getContentPane().add(row1,BorderLayout.NORTH);//Gets the content of Row 1 and positions it at top of container.
//Row 2
row2.add(exitButton);
row2.add(clearButton);
row2.add(calculateButton);
getContentPane().add(row2,BorderLayout.CENTER);//Gets content of Row 2 positions it in the center of container
//Row 3
row3.add(Resultslbl,BorderLayout.NORTH);//Lays out Header of row 3
row3.add(textPane,BorderLayout.SOUTH);//Lays out field of Row 3
getContentPane().add(row3,BorderLayout.SOUTH);//Gets content of Row 3 and positions it at bottom of container
//Create Calculate button and add listener
PVtxt.addActionListener(this);
YRStxt.addActionListener(this);
IRtxt.addActionListener(this);
ComboBxTerms.addItemListener(this);
calculateButton.addActionListener(this);
clearButton.addActionListener(this);
exitButton.addActionListener(this);
pack(); //Packs up Components
}
//Combo Box Actions
public void itemStateChanged(ItemEvent event)
{
Object select = event.getItem();
String selection = select.toString();
if (selection == ComboBx[0])
{
}
if (selection == ComboBx[1])
{
IR= YIR[0];
NP = YRS[0];
}
if (selection == ComboBx[2])
{
IR=YIR[1];
NP = YRS [1];
}
if (selection == ComboBx[3])
{
IR=YIR[2];
NP = YRS [2];
}
}
//CButtons
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == calculateButton)
{
PV = Double.parseDouble(PVtxt.getText());
Years = Double.parseDouble(YRStxt.getText());
IR = Double.parseDouble(IRtxt.getText());
Calculate() ;
PMTtxt.setText( Payment.format( PMT)+" ");
repaint();
MoL = NP * 12;
Bal = PV;
for (MO= 1; MO <= MoL; MO++)
{
//monthly interest calculation
IRM = IR/(1200.00);
//current months interest amount calculation
NI = Bal * IRM;
//current months principale amount calculation
NPV = PMT - NI;
//current months interest minus loan amount
//provides new principle amount
Bal = Bal - NPV;
Resultstxt.append(" " + MO + "\t\t " + Payment.format(PMT)
+ "\t\t" + Payment.format(NI)
+ "\t\t"
+ Payment.format(Bal) + "\n");
}
}
else if (e.getSource() == clearButton)
{
//clear fields
PVtxt.setText ("");
PMTtxt.setText("");
YRStxt.setText("");
IRtxt.setText("");
PV = 0.0;
Resultstxt.setText("");
}
else if (e.getSource() == exitButton)
{
System.exit(0);
}
}
//Mortgage Payment Calculations
double Calculate()
{
return PMT = (PV * ((1 + (IR*.01)/12)-1) / (1-(Math.pow(1 + (IR/1200),-NP*12))));
}
public static void main(String[] args)
{
new Week4B().setVisible(true);
}
}