Look but could not find the answer.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ghostrider
    New Member
    • Jan 2007
    • 34

    Look but could not find the answer.

    My program seems to slove for all the mortgages at once. And will not clear out the pane and accept another choice by the user. Can someone steer me in the right direction with this problem. My program is shown below!




    Code:
     import java.awt.*; 
    import javax.swing.*;
    import java.math.*;
    import java.awt.event.*;
    import java.text.*;
    import java.lang.*;
    import java.util.ArrayList.*;
     
     
    public class wk4_jrs extends JFrame implements ActionListener
    	{
    	public static void main(String[] args)
    		{
    			wk4_jrs calc = new wk4_jrs();
    			}
    	//labels and fields
    	JLabel amountLabel = new JLabel("Principle Loan");
    	JTextField amountField = new JTextField(7);
     
    	//array list created
    	String[] mortgageArray = {"Select A Loan", "7 years @ 5.35%", "15 Years @ 5.50%", "30 Years @ 5.75%"};
     
    	JComboBox mortgageMenu = new JComboBox(mortgageArray);
     
    	JButton calculateButton = new JButton("Calculate");
    	JButton myButton = new JButton("Clear");
    	JButton myButton1 = new JButton("Exit");
     
    	JFrame Frame = new JFrame();
    	JPanel con = new JPanel();
    	JTextArea outputArea = new JTextArea();
     
    DecimalFormat decimalPlaces = new DecimalFormat("0.00");
     
    	public wk4_jrs()
    	{
    	//set size of window
    	super("Joe's Mortgage Calculator");
    	setSize(600,900);
    	setVisible(true);
    	setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
     
    	FlowLayout layout = new FlowLayout();
    	Container pane = getContentPane();
    	pane.setLayout(layout);
     
    	//pane layout and action listeners
    	pane.add(amountLabel);
    	pane.add(amountField);
    	amountField.requestFocus();
    	amountField.addActionListener(this);
    	pane.add(mortgageMenu);
    	mortgageMenu.addActionListener(this);
    	pane.add(calculateButton);
    	calculateButton.addActionListener(this);
    	pane.add(myButton);
    	myButton.addActionListener(this);
    	pane.add(myButton1);
    	myButton1.addActionListener(this);
     
     
    	setContentPane(pane);
    	pane.setBackground(Color.red);
     
     
    	outputArea = new JTextArea(40,40);
    	outputArea.setLineWrap(true);
    	outputArea.setWrapStyleWord(true);
    	outputArea.setEditable(false);
     
    	JScrollPane scroller = new JScrollPane(outputArea,
    	ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
    	ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
     
    	pane.add(scroller);
    	setContentPane(pane);
    	}
    	public void actionPerformed(ActionEvent event)
    	{
    		//action events	
    		Object source = event.getSource();
     
     
    	if( source == myButton)
    		{
    			amountField.setText(null);
    			outputArea.setText(null);			
    		}
     
    	if( source == myButton1)
    		{
    			System.exit(0);
    		}
     
    		if( source == calculateButton)
    		{
    		String amount = amountField.getText();
     
    		}		
    			//declares variables
     
    		double payment1, payment2, payment3;
    		double interestmonthly1, interestmonthly2, interestmonthly3;
    		double amount1, amount2, amount3;
    		double principle1, principle2, principle3;
    		char userInput;
    		double interest1, interest2, interest3;
     
     
    			amount1 = 200000;
    			amount2 = 200000;
    			amount3 = 200000;
     
    			interest1 = 0.0535;
    			interest2 = 0.0550;
    			interest3 = 0.0575;
     
    		//formulas used in program
    		payment1 = (amount1*((0.0535/12)/(1-Math.pow((1+(0.0535/12)), -(30*12)))));
    		payment2 = (amount2*((0.0550/12)/(1-Math.pow((1+(0.0550/12)), -(15*12)))));
    		payment3 = (amount3*((0.0575/12)/(1-Math.pow((1+(0.0575/12)), -(7*12)))));
     
    		//sets decimal places
    		DecimalFormat decimalPlaces = new DecimalFormat("0.00");
     
     
    		//prints information to users
    		outputArea.append("Monthly payment loan 1$ "+decimalPlaces.format (payment1));
    		outputArea.append("Monthly payment loan 2$ "+decimalPlaces.format (payment2));
    	 outputArea.append("Monthly payment loan 3$ "+decimalPlaces.format (payment3));				
     
     
    		for(int i=1; i<361; i=i+1)
    		{
    			interestmonthly1=(amount1*(interest1/12));
    			principle1=(payment1-interest1);
    			amount1=(interestmonthly1+amount1)-payment1;	
     
    			interestmonthly2=(amount2*(interest2/12));
    			principle2=(payment2-interest2);
    			amount2=(interestmonthly2+amount2)-payment2;
     
    			interestmonthly3=(amount3*(interest3/12));
    			principle3=(payment3-interest3);
    			amount3=(interestmonthly3+amount3)-payment3;
     
    	//Array statments
    		double[] monthlypayment = {(payment1), (payment2), (payment3)};
    		double[] interestamount = {(interestmonthly1), (interestmonthly2), (interestmonthly3)};
    		double[] principle = {(principle1), (principle2), (principle3)};
    		double[] ammount = {(amount1), (amount2), (amount3)};
     
     
    		 outputArea.append("\nMonthly Payments are: "+i);
     
    		//Begin if & else loops
    		if(ammount[0]>=0)
    		{
    			outputArea.append("\nLoan 1 Payments:$"+decimalPlaces.format(monthlypayment[0])+"\tInterest Paid:$"+decimalPlaces.format(interestamount[0])+"\t Principle:$"+decimalPlaces.format(principle[0])+"\tNew Loan Balance:$"+decimalPlaces.format(ammount[0]));
    	}
     
    			{
    	outputArea.append("\nLoan will be paid in full in 360 payments.");
    			}
     
    		if(ammount[1]>=0)
    		{
    outputArea.append("\nLoan 2 Payments:$"+decimalPlaces.format(monthlypayment[1])+"\tInterest Paid:$"+decimalPlaces.format(interestamount[1])+"\t Principle:$"+decimalPlaces.format(principle[1])+"\tNew Loan Balance:$"+decimalPlaces.format(ammount[1]));
    	}
    				else
    			{
    	outputArea.append("\nLoan will be paid in full in 180 payments.");
    			}
     
    		if(ammount[2]>=0)
    		{
    outputArea.append("\nLoan 3 Payments:$"+decimalPlaces.format(monthlypayment[2])+"\tInterest Paid:$"+decimalPlaces.format(interestamount[2])+"\t Principle:$"+decimalPlaces.format(principle[2])+"\tNew Loan Balance:$"+decimalPlaces.format(ammount[2]));
    			}
    				else
    			{
    	outputArea.append("\nLoan will be paid in full in 84 payments.");
    			}
    	}
    		}
    			}
Working...