Announcement

Collapse
No announcement yet.

Math problem in Java Program

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Math problem in Java Program

    Well It gives you the right monthly payment, but as you scroll down the amortization list, it gets all screwed up. I've been looking at this for the past couple hours and I've had it. Any pointers?

    Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.text.*;
    import javax.swing.*;
    import javax.swing.table.*;
    import java.io.*;
    
    
    public class mortgagewk4 extends JFrame implements ActionListener
    {
    
    	JPanel lpanel = new JPanel();
    	JPanel tpanel = new JPanel();
    	JPanel p1 = new JPanel();
    	JPanel pAmount = new JPanel();
    	JPanel pInterest = new JPanel();
    	JPanel pTermlngth = new JPanel();
    	JPanel pPymtresult = new JPanel();
    	JPanel pButton = new JPanel();
    
    	JLabel lAmount = new JLabel();
    	JLabel lTermlngth = new JLabel();
    	JLabel lRate = new JLabel();
    	JLabel lTitle = new JLabel();
    	JLabel lCombo = new JLabel();
    	JLabel lPymtresult = new JLabel();
    	JLabel lMain = new JLabel();
    	JLabel lMonthlypymt = new JLabel();
    	JLabel lBalance = new JLabel();
    	JLabel lInterest = new JLabel();
    
    	JButton bCalc = new JButton();
    	JButton bMonthlypymt = new JButton();
    	JButton bClear = new JButton();
    	JButton bQuit = new JButton();
    
    	JTextField tAmount = new JTextField(8);
    	JTextField tTermlngth = new JTextField();
    	JTextField tRate = new JTextField();
    	JTextField tMonthlypymt = new JTextField(8);
    	JTextField tPymtresult = new JTextField(8);
    	JTextField tCombo = new JTextField();
    	JTextField tBalance = new JTextField();
    	JTextField tInterest = new JTextField();
    
    	String sErrorMessage = "";
    	boolean bError = false;
    	String sPymtnumber = "######";
    	String sPercent = "###.###%";
    	String sAreaheader = " Payment\tPrinciple\tInterest\tBalance\n";
    
    	NumberFormat nfCurrency = NumberFormat.getCurrencyInstance();
    	NumberFormat nfDecimals = new DecimalFormat(sPymtnumber);
    	NumberFormat nfPercent = new DecimalFormat(sPercent);
    	DecimalFormat decimalPlaces=new DecimalFormat("0.00");
    
    	JComboBox cbSetup = new JComboBox();
    	JTextArea taArea = new JTextArea(20,40);
    	JScrollPane spScrollpanel = new JScrollPane(taArea);
    	FlowLayout lFlow = new FlowLayout(FlowLayout.LEFT,5,5);
    
    
    	double dIntpaid = 0;
    	double dAmountpaid = 0;
    	double dTotaldue = 0;
    	double dMonthlyapr = 0;
    	double dAmount = 0;
    	double dApr = 0;
    	double dAprconv = 0;
    	double dPymt = 0;
    	double dTotinterest = 0;
    	double dTotbalance = 0;
    	double dMonthlypymt = 0;
    	double dYearlyrate = 0;
    	double dTermlngthconv = 0;
    	double dTermlngth = 0;
    
    	int iCounter = 0;
    	int iTermlngth = 0;
    	int iTotmonths = 0;
    
    
    	//Initialize array
    	double[] xRate = {.0535,.0550,.0575};
    	int[] xTerm = {7,15,30};
    
    	public static void main(String[] args)
    {
    
    	new mortgagewk4();
    	}
    
    		public mortgagewk4()
    	{
    
    	Container main = getContentPane();
    
    	for(iCounter = 0; iCounter <= 2; iCounter++)
    	{
    
    	cbSetup.addItem(xTerm[iCounter] + " years - " + nfPercent.format(xRate[iCounter]));
    	}
    
    //Read text panel
    	lpanel.setLayout(new GridLayout(8,1));
    	lpanel.setMinimumSize(new java.awt.Dimension(150,150));
    	lpanel.setPreferredSize(new java.awt.Dimension(150,150));
    	lpanel.setMaximumSize(new java.awt.Dimension(Short.MAX_VALUE,Short.MAX_VALUE));
    	lpanel.add(lCombo);
    	lpanel.add(lAmount);
    	lpanel.add(lRate);
    	lpanel.add(lTermlngth);
    	lpanel.add(lMonthlypymt);
    	lpanel.add(lBalance);
    	lpanel.add(lInterest);
    	lCombo.setText("Preset Payback Option");
    	lAmount.setText("Amount");
    	lRate.setText("APR");
    	lTermlngth.setText("Length of Loan");
    	lMonthlypymt.setText("Monthly Payment");
    //Write text panel
    	tpanel.setLayout(new GridLayout(8,1));
    	tpanel.setMinimumSize(new java.awt.Dimension(125, 125));
    	tpanel.setPreferredSize(new java.awt.Dimension(125, 125));
    	tpanel.setMaximumSize(new java.awt.Dimension(Short.MAX_VALUE,Short.MAX_VALUE));
    	tpanel.add(cbSetup);
    	tpanel.add(tAmount);
    	tpanel.add(tRate);
    	tpanel.add(tTermlngth);
    	cbSetup.addActionListener(this);
    	tpanel.add(tMonthlypymt);
    
    
    //Create border layout
    	p1.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    	p1.setLayout(new BorderLayout());
    	p1.add(lpanel, "West");
    	p1.add(tpanel, "East");
    	main.add(p1, "West");
    
    
    //Buttons
    	pButton.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
    	pButton.setLayout(new BorderLayout());
    	bCalc.setText("Calculate Mortgage");
    	main.add(bCalc);
    	bMonthlypymt.setText("Amortization Table");
    	main.add(bMonthlypymt);
    	bClear.setText("Clear input");
    	main.add(bClear);
    	bQuit.setText("Quit");
    	main.add(bQuit);
    	main.add(pButton);
    
    
    //Scrollable
    	spScrollpanel.setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1));
    	main.add(spScrollpanel);
    	main.setLayout(lFlow);
    
    //Listeners
    	bCalc.addActionListener(this);
    	bMonthlypymt.addActionListener(this);
    	bQuit.addActionListener(this);
    	bClear.addActionListener(this);
    
    //Main title
    	this.setSize(750,600);
    	this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	this.setTitle("Scott's Mortgage Calculator");
    	this.setVisible(true);
    }
    public void actionPerformed(ActionEvent thisEvent)
    
    {
    
    	Object source = thisEvent.getSource();
    
    //Declare exceptions
    
    	if(source == bCalc)
    {
    	try
    {
    
    	iTermlngth = Integer.parseInt(tTermlngth.getText());
    	dAmount = Double.parseDouble(tAmount.getText());
    	dApr = Double.parseDouble(tRate.getText());
    }
    
    	catch(NumberFormatException e)
    {
    
    	sErrorMessage = sErrorMessage + "Invalid";
    	bError = true;
    }
    
    	if (iTermlngth <= 0 || dAmount <= 0 || dApr <= 0)
    {
    
    	sErrorMessage = sErrorMessage + "Invalid";
    	bError = true;
    }
    
    	if(bError == true)
    {
    
    	JOptionPane.showMessageDialog(this, sErrorMessage, "Invalid", JOptionPane.ERROR_MESSAGE);
    	tMonthlypymt.setText("");
    }
    
    
    //Math to solve Mortgages
    	else
    {
    
    	dTermlngthconv = iTermlngth / 12;
    	dAprconv = dApr / 100;
    	dMonthlypymt = (((dAmount * dAprconv) * dTermlngthconv) + dAmount) / iTermlngth;
    	dTotbalance = dMonthlypymt * iTermlngth;
    	dTotinterest = dTotbalance - dAmount;
    	tMonthlypymt.setText(nfCurrency.format(dMonthlypymt));
    	bMonthlypymt.setEnabled(true);
    }
    
    }
    
    	if(source == bMonthlypymt)
    {
    
    	dMonthlyapr = (dApr / 100) / 12;
    	dTotaldue = dAmount;
    	taArea.setText(sAreaheader);
    
    	for(int iCounter = 0; iCounter <= iTermlngth - 1; iCounter++)
    
    {
    
    	dIntpaid = dTotaldue * dMonthlyapr;
    
    	if(dIntpaid <= 0)
    	dIntpaid = 0;
    	dAmountpaid = dMonthlypymt - dIntpaid;
    	dTotaldue = dTotaldue - dAmountpaid;
    
    	taArea.append(" "
    	+nfDecimals.format(iCounter + 1) + "\t"
    	+nfCurrency.format(dAmountpaid) + "\t"
    	+nfCurrency.format(dIntpaid) + "\t"
    	+nfCurrency.format(dTotaldue) + "\n");
    	taArea.setCaretPosition(0);
    }
    }
    
    //Display all payments
    
    	if(source == cbSetup)
    
    {
    
    	int cbStart = cbSetup.getSelectedIndex();
    	iTotmonths = xTerm[cbStart]* 12;
    	dYearlyrate = xRate[cbStart]* 100;
    	tTermlngth.setText(Integer.toString(iTotmonths));
    	tRate.setText(Double.toString(dYearlyrate));
    	tAmount.requestFocus();
    }
    
    	if(source == bClear)
    
    {
    	tAmount.setText(null);
    	tRate.setText(null);
    	tTermlngth.setText(null);
    	tMonthlypymt.setText(null);
    }
    //exit program
    
    	if(source == bQuit)
    
    {
    
    	System.exit(0);
    
    }
    
    }
    
    }
    Why is it called tourist season, if we can't shoot at them?
Working...
X