Java Mortgage tables and month increments

Need help with Tables and Month Increments

Page 1 of 1

3 Replies - 856 Views - Last Post: 06 July 2008 - 03:55 PM Rate Topic: -----

#1 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Java Mortgage tables and month increments

Posted 06 July 2008 - 03:18 PM

I need help adding tables to my mortgage results
and my months displayed do not start with month #1
instead it ends with month 1



import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import java.text.NumberFormat;

//Setup lables and buttons
public class Mortgage120test extends JFrame implements ActionListener{


	private JLabel labelAmount, labelYears, labelInterest, labelMpayment;
	private JTextField textAmount, textYears,textInterest,textMpayment;
	private JButton buttonCalc, buttonClear, buttonstop;
	private GridLayout grid1;
	private Container c;
	
	private JLabel labelTopic;
	private JTextField textTopic;
	private JTextArea area1;

	private GridBagLayout grid2;
	private Container container;
	private GridBagConstraints grid2Constraints;

	public Mortgage120test(){
	

	
		super("MORTGAGE CALCULATOR");
		grid1 = new GridLayout(1,10,5,5); //(rows, columns, vertical space, hortizontal space)
		c = getContentPane();
		c.setLayout(grid1); //layer the format
		
		//addition of Menu bar
		JMenuBar bar = new JMenuBar();
		setJMenuBar(bar);
		
		//addition of main menu bar item
		JMenu calMenu = new JMenu("CALCULATE");
		calMenu.setMnemonic('L'); //Hot Key
		bar.add(calMenu);

		//addition of first item for File
		JMenuItem thirtyMenu = new JMenuItem("1-30 Years");
		thirtyMenu.setMnemonic('1');
			thirtyMenu.addActionListener(
				new ActionListener(){  //Listen to the Keyboard
					public void actionPerformed(ActionEvent e){ //Set to 30 Years and 5.75% 
																								String S_Amount="200000";
						String S_Year = "30";
						String S_Percent = "5.75";
																								textAmount.setText(S_Amount);
						textYears.setText(S_Year);
						textInterest.setText(S_Percent);
					}
				}
			);
		calMenu.add(thirtyMenu);

		//addition of second item for File
		JMenuItem fifteenMenu = new JMenuItem("2-15 Years");
		fifteenMenu.setMnemonic('2');
			fifteenMenu.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){ //Set to 15 Years and 5.5% 
						String S_Year = "15";
						String S_Percent = "5.5";
						textYears.setText(S_Year);
						textInterest.setText(S_Percent);
					}
				}
			);
		calMenu.add(fifteenMenu);

		//addition of second item for File
		JMenuItem sevenMenu = new JMenuItem("3-7 Years"); //Set to 7 Years and 5.35% 
		sevenMenu.setMnemonic('3');
			sevenMenu.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						String S_Year = "7";
						String S_Percent = "5.35";
						textYears.setText(S_Year);
						textInterest.setText(S_Percent);
					}
				}
			);
		calMenu.add(sevenMenu);
		
		
		
		//addition of second main menu bar 
		JMenuItem clearMenu = new JMenuItem("CLEAR");
		clearMenu.setMnemonic('R');
			clearMenu.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						textMpayment.setText(null);
						textInterest.setText(null);
						textYears.setText(null);
						area1.setText(null);
						textAmount.setText(null);//clear program;
					}
				}
			);
		bar.add(clearMenu);		

		
		//addition of third main menu bar 
		JMenuItem exitMenu = new JMenuItem("EXIT");
		exitMenu.setMnemonic('E');
			exitMenu.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent e){
						System.exit(0);
					}
				}
			);
			
		bar.add(exitMenu);
		
		//Text Labels
		labelAmount = new JLabel("  Amount ");
		labelYears = new JLabel("  Years ");
		labelInterest = new JLabel("  Interest ");
		labelMpayment = new JLabel("  Monthly Payment ");

		//Text Boxes 5 wide
		textAmount = new JTextField(10);
		textYears = new JTextField(2);
		textInterest = new JTextField(3);
		textMpayment = new JTextField(10);

		//Calculate Button
		buttonCalc = new JButton("Calculate");
				
		//Add Buttons  and Lables
		c.add(labelAmount);
		c.add(textAmount);

		c.add(labelYears);
		c.add(textYears);

		c.add(labelInterest);
		c.add(textInterest);

		c.add(labelMpayment);
		c.add(textMpayment);

		c.add(buttonCalc);
		
		//Action Listeners
		textAmount.addActionListener(this);
		textYears.addActionListener(this);
		textInterest.addActionListener(this);
		buttonCalc.addActionListener(this);
		
		//Area Box
		container = getContentPane();
		grid2 = new GridBagLayout();
		container.setLayout(grid2);
		grid2Constraints = new GridBagConstraints();
		
		area1 = new JTextArea(" ");
		//labelTopic = new JLabel("	 Menu Selection");
		textTopic = new JTextField(20);
		
		//Area1
		grid2Constraints.fill = GridBagConstraints.BOTH;
		//grid2Constraints.anchor = GridBagConstraints.CENTER; //2nd row
		grid2Constraints.weightx = 120;
		grid2Constraints.weighty = 60;
		addComponent(area1, 8, 0,9,9);
		
		//additoin of Horizontal Scroll bar to area1 Text Area
		Box b = Box.createHorizontalBox();
		JScrollPane scrollPane = new JScrollPane(area1);
		addComponent ((new JScrollPane(area1)), 8, 0,9,9);	
			
		//Size of the Window
		setSize (820, 480);
		show();

	}

public static double Formula (double Amount, double Years, double Interest){

	//method to calculate  payment
	double Interest_Rate = (Interest/1200);
	double average = Amount * (Interest_Rate/(1 - Math.pow ((1 + Interest_Rate), -1 * (Years * 12))));
	return average;
		
	}

//Stop program
public void Stop(){
	System.exit(0);
	}
	
//Fill Area
public void FillTextArea(String myMessage){
		area1.append("\t" + myMessage + "\n");

	}

//Aera Box
private void addComponent(Component c, int row, int col, int width, int height){
		grid2Constraints.gridx = col;
		grid2Constraints.gridy = row;
		grid2Constraints.gridwidth = width;
		grid2Constraints.gridheight = height;
		grid2.setConstraints(c, grid2Constraints);

		container.add(c);
	}

public void actionPerformed (ActionEvent evt){

	Object source = evt.getSource();
	//Mortgage Variables	
	double Mpayment = 0;		
	double Interest = 0;
	double Amount = 0;
	int Years = 0;
	double Month = 0;
	double Interest_Rate = 0;
	double Monthly_Balance = 0;
	double Monthly_Principal = 0;
	double Monthly_Interest = 0;

		//Print  Monthly Payment
		if (source==buttonCalc){
			double rootAmount = Double.parseDouble(textAmount.getText());
			double rootYears = Double.parseDouble(textYears.getText());
				Month = rootYears * 12;
			double rootInterest = Double.parseDouble(textInterest.getText());
			Mpayment = Mortgage120test.Formula(rootAmount, rootYears, rootInterest);
			String MpaymentString = NumberFormat.getCurrencyInstance().format(Mpayment); //Output monthly payment in $$
			textMpayment.setText(MpaymentString);
			area1.setText(null);
			
					
	//Calculate  Monthly Payment
	double Interest_Rate_2 = (rootInterest/1200);
	double Monthly_Payment = rootAmount * (Interest_Rate_2/(1 - Math.pow ((1 + Interest_Rate_2), -1 * (rootYears * 12))));
	Monthly_Interest = (Interest_Rate_2 * rootAmount);
	Monthly_Principal = (Monthly_Payment - Monthly_Interest);
	Monthly_Balance = (rootAmount - Monthly_Principal);
	
	//Print in Area1
	area1.append("\t" + "Month" + "\t\t" + "Balance" + "\t\t" + "Principal" + "\t\t" + "Interest" +"\n");
	
	//Loop
	while (Month > 0){
	
	//Format in Dollar Amount
	String MPricipleString = NumberFormat.getCurrencyInstance().format(Monthly_Principal);
	String MInterestString = NumberFormat.getCurrencyInstance().format(Monthly_Interest);
	String MBalanceString = NumberFormat.getCurrencyInstance().format(Monthly_Balance);
	
	//Monthly Balance
	area1.append("\t" + Month + "\t\t" + MBalanceString + "\t\t" + MPricipleString + "\t\t" + MInterestString +"\n"); 
	
	//De-increment line
	Month--;
	
	//Calculate New line
	Monthly_Interest = (Interest_Rate_2 * Monthly_Balance);
	Monthly_Principal = (Monthly_Payment - Monthly_Interest);
	Monthly_Balance = (Monthly_Balance - Monthly_Principal);
	
	}
		}
}

//Call Mortgage120test 
public static void main(String arguments[]){
	Mortgage120test call1 = new Mortgage120test();		
		call1.addWindowListener(
		new WindowAdapter(){
		public void windowClosing(WindowEvent e){
		System.exit(0);			
		}
		}
		);
	}
}

	
			



Is This A Good Question/Topic? 0
  • +

Replies To: Java Mortgage tables and month increments

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java Mortgage tables and month increments

Posted 06 July 2008 - 03:21 PM

Please avoid double posting :angry:
You had your answers in another post... DIC spent time answering these answers
Was This Post Helpful? 0
  • +
  • -

#3 mysong  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 18-May 08

Re: Java Mortgage tables and month increments

Posted 06 July 2008 - 03:37 PM

View Postpbl, on 6 Jul, 2008 - 03:21 PM, said:

Please avoid double posting :angry:
You had your answers in another post... DIC spent time answering these answers

this is a new code that I am asking about JTables
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Java Mortgage tables and month increments

Posted 06 July 2008 - 03:55 PM

I guess it is just to add:

int TotalMonth = Month;
ans then display

(TotalMonth - Month) + 1; instead of Month
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1