//imports required packages
import java.awt.*; //to create buttons
import javax.swing.*; //imports java swing
import java.awt.event.*; //cover any action or item events and listeners
import java.text.DecimalFormat; //to import decimal format for money
class mortgageCalculatorMcCabe3 extends JFrame implements ActionListener, ItemListener {
//Fields for user input
JTextField principalAmount = new JTextField(15);
JTextField yearlyInterestRate = new JTextField(15);
JTextField numberOfYears = new JTextField(15);
JTextField monthlyPayments = new JTextField(15);
JLabel validateLabel = new JLabel();
// public JComboBox MortIntRate;
/*Declaring and initializing variables*/
int term = 0;
double principleAmount = 0;
String ArrayTerm [] = {"7", "15", "30"};
String ArrayInterestRate [] = {"5.35", "5.5", "5.75"};
// create the comboboxes for class-wide use.
JComboBox termList = new JComboBox(ArrayTerm);
JComboBox interestRateList = new JComboBox(ArrayInterestRate);
mortgageCalculatorMcCabe3() {
//Sets the title at the top of program
super("Ryan's Mortgage Calculator");
// Panel comboBox1 = new Panel();
JComboBox interestRateList = null;
// Panel comboBox2 = new Panel();
JComboBox termList = null;
//Sets the size of the Frame
setSize(350, 225);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
FlowLayout flow = new FlowLayout(FlowLayout.RIGHT);
pane.setLayout(flow);
//Creates Labels and Fields for row 1
JPanel row1 = new JPanel();
JLabel principalLabel = new JLabel("Loan Amount:");
row1.add(principalLabel);
row1.add(principalAmount);
pane.add(row1);
//Creates Labels and Fields for row 2
JPanel row2 = new JPanel();
JLabel numYearsLabel = new JLabel("Term of Loan(Years):");
row2.add(numYearsLabel);
row2.add(numberOfYears);
termList.setSelectedIndex(0); // MOVED statements here
termList.addItemListener(this);
row2.add(termList); // Add combo to row2 (temporarily)
pane.add(row2);
////Creates Labels and Fields for row 3
JPanel row3 = new JPanel();
JLabel yearIntrateLabel = new JLabel("Interest Rate:");
row3.add(yearIntrateLabel);
row3.add(yearlyInterestRate);
interestRateList.setSelectedIndex(0); // MOVED statements here
interestRateList.addItemListener(this);
row3.add(interestRateList); // Add combo to row3
pane.add(row3);
////Creates Labels and Fields for row 4
JPanel row4 = new JPanel();
JLabel monthlyPaymentsLabel = new JLabel("Total Monthly Payment:");
row4.add(monthlyPaymentsLabel);
row4.add(monthlyPayments);
pane.add(row4);
setContentPane(pane);
setVisible(true);
//Creates Labels and Fields for row 5
JPanel row5 = new JPanel();
JButton calculate = new JButton("Calculate");
calculate.addActionListener(this);
row5.add(calculate);
pane.add(row5);
JButton reset = new JButton("Clear");
reset.addActionListener(this);
row5.add(reset);
pane.add(row5);
JButton exit = new JButton("Exit");
exit.addActionListener(this);
row5.add(exit);
pane.add(row5);
termList = new JComboBox(ArrayTerm);
termList.setSelectedIndex(0);
termList.addItemListener(this);
// interestRateList = new JComboBox(ArrayInterestRate);
// interestRateList.setSelectedIndex(0);
// interestRateList.addItemListener(this);
/*Setup list*/
// comboBox.add(loanTerm);
// comboBox.add(termList);
// comboBox.add(loanInterest);
// comboBox.add(interestRateList);
}
public void itemStateChanged( ItemEvent event )
{
if( event.getSource() == termList
&& event.getStateChange() == ItemEvent.SELECTED )
{
termList.setSelectedIndex( termList.getSelectedIndex() );
}
else if( event.getSource() == interestRateList
&& event.getStateChange() == ItemEvent.SELECTED )
{
interestRateList.setSelectedIndex( interestRateList.getSelectedIndex() );
}
}
//This parts performs the Actions for the Calculate, Clear & Exit buttons
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command == "Calculate")
compute();
if (command == "Clear")
clearAllFields();
if (command == "Exit")
exit();}
void compute()
{
try{
validateLabel.setText("");
validateLabel.validate();
Double yearIntrate = new Double (yearlyInterestRate.getText());
Double principal = new Double (principalAmount.getText());
Integer numYears = new Integer (numberOfYears.getText());
double payFreq = 12;
double interestRateMonths;
double monthlyPayment;
double numPayments;
//results and formula's
interestRateMonths = (yearIntrate.doubleValue() / 100) / payFreq;
numPayments = payFreq * numYears.intValue();
double temp = Math.pow(1 + interestRateMonths, (-1*numPayments));
monthlyPayment = principal.doubleValue() * interestRateMonths / (1 - (temp));
//Format used to set two decimal places.
DecimalFormat currency = new DecimalFormat("$0.00");
monthlyPayments.setText(currency.format(monthlyPayment));
//if user submit non valid information it provides an error.
} catch(Exception error)
{
validateLabel.setText("Invalid Entry! Try again.");
}
}
//to clear the fields back to beginning
void clearAllFields(){
principalAmount.setText("");
yearlyInterestRate.setText("");
numberOfYears.setText("");
monthlyPayments.setText("");
}
//exit program
void exit(){
System.exit(0);
}
public static void main(String[] arguments)
{
mortgageCalculatorMcCabe3 mortgageCalculatorMcCabe3 = new mortgageCalculatorMcCabe3 ();
}
}//end program
*Edited to add the [ code] [ /code] tags
And where are you RadioButtons ?
Please
it makes our life a lot easier
This post has been edited by pbl: 01 March 2009 - 07:37 PM

New Topic/Question
Reply




MultiQuote






|