Java Mortgage Calculator w/ Graph

Can someone please help me. The program compiles and runs but does not

Page 1 of 1

2 Replies - 2250 Views - Last Post: 17 October 2009 - 07:30 AM Rate Topic: -----

#1 whattha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-October 09

Java Mortgage Calculator w/ Graph

Posted 16 October 2009 - 02:42 PM

import java.awt.*;
import java.text.NumberFormat;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;


public class MortCalc extends JFrame implements ActionListener
{
// Declare Labels
JLabel AmountLabel = new JLabel( "Principal Amount:$ " );
JLabel PaymentLabel = new JLabel( "Monthly Payment: " );
JLabel InterestLabel = new JLabel( "Interest Rate %: " );
JLabel TermLabel = new JLabel( "Length of Loan of Loan in Years: " );

// Declare Text Fields
JTextField mortgageAmount = new JTextField(7);
JTextField Payment = new JTextField(7);
JTextField InterestRate = new JTextField(3);
JTextField Term = new JTextField(3);


// Declare Buttons
JButton Loan7 = new JButton( "7 years at 5.35%" );
JButton Loan15 = new JButton( "15 years at 5.50%" );
JButton Loan30 = new JButton( "30 years at 5.75%" );
JButton ExitButton = new JButton( "Exit" );
JButton ClearButton = new JButton( "Clear All" );
JButton CalculateButton = new JButton( "Calculate Loan" );


// Declares Text Area and ScrollPane
JTextArea LoanPayments = new JTextArea(20,50);
JTextArea GraphArea = new JTextArea(19,50);
JScrollPane scroll = new JScrollPane(LoanPayments);


public MortCalc()
{
//Frame, Panel, and Layout set up
super("Mortgage Calculator w/ Graph");
setSize(900, 800);
setLocation(200, 0);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel(new GridLayout(3,1));

//Setup container and contents
Container grid = getContentPane();
grid.setLayout(new GridLayout(4,0,4,4));

pane.add(grid);
pane.add(scroll);
grid.add(AmountLabel);
grid.add(mortgageAmount);
grid.add(InterestLabel);
grid.add(InterestRate);
grid.add(TermLabel);
grid.add(Term);
grid.add(PaymentLabel);
grid.add(Payment);
grid.add(Loan7);
grid.add(Loan15);
grid.add(Loan30);
grid.add(CalculateButton);
grid.add(ClearButton);
grid.add(ExitButton);
Payment.setEditable(false);
setContentPane(pane);
setContentPane(pane);
setVisible(true);

//Adds Action Listeners
ExitButton.addActionListener(this);
ClearButton.addActionListener(this);
Loan7.addActionListener(this);
Loan15.addActionListener(this);
Loan30.addActionListener(this);
mortgageAmount.addActionListener(this);
InterestRate.addActionListener(this);
Term.addActionListener(this);
Payment.addActionListener(this);
CalculateButton.addActionListener(this);

}
public static void main(String[] args)
{
MortCalc frame= new MortCalc();

}

public void actionPerformed(ActionEvent e) {
Object command = e.getSource();
if (command == ExitButton) {
System.exit(0); }
else if (command == Loan7) {
calcLoan(7, 5.35); }
else if (command == Loan15) {
calcLoan(15, 5.50); }
else if (command == Loan30) {
calcLoan(30, 5.75); }
else if (command == CalculateButton ) {
double terms = 0;
double rates = 0;
try {
terms = Double.parseDouble(Term.getText());
rates = Double.parseDouble(InterestRate.getText());
} catch (Exception ex) {
LoanPayments.setText("Invalid term or rate Amount");
return;

}

calcLoan(terms, rates); }
else if (command == ClearButton) {
mortgageAmount.setText("");
Payment.setText("");
InterestRate.setText("");
Term.setText("");
LoanPayments.setText("");

}

}

private void calcLoan(double terms, double rates) {
Term.setText(String.valueOf(terms) );
InterestRate.setText(String.valueOf(rates));
double amount = 0;
try {
amount = Double.parseDouble(mortgageAmount.getText());
//Parse text to
} catch (Exception ex) { LoanPayments.setText
("Invalid mortgage Amount");
return; }
double interestRate = rates;

// Sets interestRate amount
double intRate = (interestRate / 100) / 12;

// Calculates Interest Rate
// double loanTermMonths = terms;
// Calculates Loan Term in Months
int months = (int)terms * 12;

// Converts Loan Term to Months
double rate = (intRate / 12);

// Converts Annual interest rate to monthly interest rate
double payment = amount * intRate / (1 - (Math.pow(1/(1 + intRate), months)));

// Calculation for Monthly payment
double remainingPrincipal = amount;

// Sets Remaining Balance
double MonthlyInterest = 0;


// saves interest payment
double MonthlyAmt = 0;

// saves principal payment
//double x[] = new double[1]; x[0] = 0;
// Number formatter to format output in table
NumberFormat CurrencyFormatter = NumberFormat.getCurrencyInstance();
Payment.setText(CurrencyFormatter.format(payment));
LoanPayments.setText("Month\tPrincipal\tInterest\tEnding Balance\n");

// double[] values = new double[months];
// counting the month backward is confusing
// for(;months > 0; months -- ) {
int currentMonth = 0; while(currentMonth < months) {

//Append loop for mortgage detail in the text area
MonthlyInterest = (remainingPrincipal * intRate);

//Monthly Payment Toward Interest
MonthlyAmt = (payment - MonthlyInterest);

//Monthly Payment Toward Principal
remainingPrincipal = (remainingPrincipal - MonthlyAmt);

//Remaining loan Balance
// values[months-1] = remainingPrincipal;
LoanPayments.append((++currentMonth) + "\t" +
CurrencyFormatter.format(MonthlyAmt) + "\t" +
CurrencyFormatter.format(MonthlyInterest) + "\t" +
CurrencyFormatter.format(remainingPrincipal) + "\n");


GraphArea.append("" + remainingPrincipal);
}
}

// Class to hold a value for a slice
public class PieValue {
double value;
Color color;

public PieValue(double value, Color color) {
this.value = value;
this.color = color;

// Show the component in a frame
JFrame frame = new JFrame();
frame.getContentPane().add(new MyComponent());
frame.setSize(300, 200);
frame.setVisible(true);

}

}

// slices is an array of values that represent the size of each slice.
public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
// Get total value of all slices
double total = 0.0D;
for (int i=0; i<slices.length; i++) {
total += slices[i].value;
}

// Draw each pie slice
double curValue = 0.0D;
int startAngle = 0;
for (int i=0; i<slices.length; i++) {
// Compute the start and stop angles
startAngle = (int)(curValue * 360 / total);
int arcAngle = (int)(slices[i].value * 360 / total);

// Ensure that rounding errors do not leave a gap between the first and last slice
if (i == slices.length-1) {
arcAngle = 360 - startAngle;
}

// Set the color and draw a filled arc
g.setColor(slices[i].color);
g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);

curValue += slices[i].value;
}
}



class MyComponent extends JComponent {
PieValue[] slices = new PieValue[4];

MyComponent() {
slices[0] = new PieValue(25, Color.red);
slices[1] = new PieValue(33, Color.green);
slices[2] = new PieValue(20, Color.pink);
slices[3] = new PieValue(15, Color.blue);
}

// This method is called whenever the contents needs to be painted
public void paint(Graphics g) {
// Draw the pie
drawPie((Graphics2D)g, getBounds(), slices);


}
}
}






Sorry, I was cut off the program runs and compiles but it does not produce a graph. Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Java Mortgage Calculator w/ Graph

#2 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Mortgage Calculator w/ Graph

Posted 16 October 2009 - 10:36 PM

Reading unindented code for more than a few lines is a nightmare please

:code:

it is one of this forum rule
Was This Post Helpful? 0
  • +
  • -

#3 whattha   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-October 09

Re: Java Mortgage Calculator w/ Graph

Posted 17 October 2009 - 07:30 AM

//Sorry, about that here is my code in better fashion. Thanks for taking a look.

import java.awt.*;
import java.text.NumberFormat;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Graphics;
import java.awt.geom.Rectangle2D;


public class MortCalc extends JFrame implements ActionListener
{
	// Declare Labels
	JLabel AmountLabel = new JLabel( "Principal Amount:$ " );
	JLabel PaymentLabel = new JLabel( "Monthly Payment: " );
	JLabel InterestLabel = new JLabel( "Interest Rate %: " );
	JLabel TermLabel = new JLabel( "Length of Loan of Loan in Years: " );

	// Declare Text Fields
	JTextField mortgageAmount = new JTextField(7);
	JTextField Payment = new JTextField(7);
	JTextField InterestRate = new JTextField(3);
	JTextField Term = new JTextField(3);


	// Declare Buttons
	JButton Loan7 = new JButton( "7 years at 5.35%" );
	JButton Loan15 = new JButton( "15 years at 5.50%" );
	JButton Loan30 = new JButton( "30 years at 5.75%" );
	JButton ExitButton = new JButton( "Exit" );
	JButton ClearButton = new JButton( "Clear All" );
	JButton CalculateButton = new JButton( "Calculate Loan" );


	// Declares Text Area and ScrollPane
	JTextArea LoanPayments = new JTextArea(20,50);
	JTextArea GraphArea = new JTextArea(19,50);
	JScrollPane scroll = new JScrollPane(LoanPayments);


	public MortCalc()
	{
		//Frame, Panel, and Layout set up
		super("Mortgage Calculator w/ Graph");
		setSize(900, 800);
		setLocation(200, 0);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel pane = new JPanel(new GridLayout(3,1));

		//Setup container and contents
		Container grid = getContentPane();
		grid.setLayout(new GridLayout(4,0,4,4));

		pane.add(grid);
		pane.add(scroll);
		grid.add(AmountLabel);
		grid.add(mortgageAmount);
		grid.add(InterestLabel);
		grid.add(InterestRate);
		grid.add(TermLabel);
		grid.add(Term);
		grid.add(PaymentLabel);
		grid.add(Payment);
		grid.add(Loan7);
		grid.add(Loan15);
		grid.add(Loan30);
		grid.add(CalculateButton);
		grid.add(ClearButton);
		grid.add(ExitButton);
		Payment.setEditable(false);
		setContentPane(pane);
		setContentPane(pane);
		setVisible(true);

		//Adds Action Listeners
		ExitButton.addActionListener(this);
		ClearButton.addActionListener(this);
		Loan7.addActionListener(this);
		Loan15.addActionListener(this);
		Loan30.addActionListener(this);
		mortgageAmount.addActionListener(this);
		InterestRate.addActionListener(this);
		Term.addActionListener(this);
		Payment.addActionListener(this);
		CalculateButton.addActionListener(this);

			}
			public static void main(String[] args)
			{
				MortCalc frame= new MortCalc();

	}

	public void actionPerformed(ActionEvent e) {
		Object command = e.getSource();
		if (command == ExitButton) {
			System.exit(0);	 }
		else if (command == Loan7) {
			calcLoan(7, 5.35);	 }
		else if (command == Loan15) {
			calcLoan(15, 5.50);	 }
		else if (command == Loan30) {
			calcLoan(30, 5.75);	 }
		else if (command == CalculateButton ) {
			double terms = 0;
			double rates = 0;
			try {
				terms = Double.parseDouble(Term.getText());
				rates = Double.parseDouble(InterestRate.getText());
			} catch (Exception ex) {
				LoanPayments.setText("Invalid term or rate Amount");
				return;

			}

			calcLoan(terms, rates);	 }
		else if (command == ClearButton) {
			mortgageAmount.setText("");
			Payment.setText("");
			InterestRate.setText("");
			Term.setText("");
			LoanPayments.setText("");

		}

	}

	private void calcLoan(double terms, double rates) {
		Term.setText(String.valueOf(terms) );
		InterestRate.setText(String.valueOf(rates));
		double amount = 0;
		try {
			amount = Double.parseDouble(mortgageAmount.getText());
			//Parse text to
		}	 catch (Exception ex) {		  LoanPayments.setText
			("Invalid mortgage Amount");
		return;	 }
		double interestRate = rates;

		// Sets interestRate amount
		double intRate = (interestRate / 100) / 12;

		// Calculates Interest Rate
		// double loanTermMonths = terms;
		// Calculates Loan Term in Months
		int months = (int)terms * 12;

		// Converts Loan Term to Months
		double rate = (intRate / 12);

		// Converts Annual interest rate to monthly interest rate
		double payment = amount * intRate / (1 - (Math.pow(1/(1 + intRate), months)));

		// Calculation for Monthly payment
		double remainingPrincipal = amount;

		// Sets Remaining Balance
		double MonthlyInterest = 0;


		// saves interest payment
		double MonthlyAmt = 0;

		// saves principal payment
		//double x[] = new double[1]; x[0] = 0;
		// Number formatter to format output in table
		NumberFormat CurrencyFormatter = NumberFormat.getCurrencyInstance();
		Payment.setText(CurrencyFormatter.format(payment));
		LoanPayments.setText("Month\tPrincipal\tInterest\tEnding Balance\n");

		// double[] values = new double[months];
		// counting the month backward is confusing
		// for(;months > 0; months -- ) {
		int currentMonth = 0;	 while(currentMonth < months) {

			//Append loop for mortgage detail in the text area
			MonthlyInterest = (remainingPrincipal * intRate);

			//Monthly Payment Toward Interest
			MonthlyAmt = (payment - MonthlyInterest);

			//Monthly Payment Toward Principal
			remainingPrincipal = (remainingPrincipal - MonthlyAmt);

			//Remaining loan Balance
			// values[months-1] = remainingPrincipal;
			LoanPayments.append((++currentMonth) + "\t" +
					CurrencyFormatter.format(MonthlyAmt) + "\t" +
					CurrencyFormatter.format(MonthlyInterest) + "\t" +
					CurrencyFormatter.format(remainingPrincipal) + "\n");


			GraphArea.append("" + remainingPrincipal);
		}
	}

	   // Class to hold a value for a slice
	   public class PieValue {
		   double value;
		   Color color;

		   public PieValue(double value, Color color) {
			   this.value = value;
			   this.color = color;

			   // Show the component in a frame
			JFrame frame = new JFrame();
			frame.getContentPane().add(new MyComponent());
			frame.setSize(300, 200);
	 		frame.setVisible(true);

		   }

	   }

	   // slices is an array of values that represent the size of each slice.
	   public void drawPie(Graphics2D g, Rectangle area, PieValue[] slices) {
		   // Get total value of all slices
		   double total = 0.0D;
		   for (int i=0; i<slices.length; i++) {
			   total += slices[i].value;
		   }

		   // Draw each pie slice
		   double curValue = 0.0D;
		   int startAngle = 0;
		   for (int i=0; i<slices.length; i++) {
			   // Compute the start and stop angles
			   startAngle = (int)(curValue * 360 / total);
			   int arcAngle = (int)(slices[i].value * 360 / total);

			   // Ensure that rounding errors do not leave a gap between the first and last slice
			   if (i == slices.length-1) {
				   arcAngle = 360 - startAngle;
			   }

			   // Set the color and draw a filled arc
			   g.setColor(slices[i].color);
			   g.fillArc(area.x, area.y, area.width, area.height, startAngle, arcAngle);

			   curValue += slices[i].value;
		   }
	   }



	   class MyComponent extends JComponent {
		   PieValue[] slices = new PieValue[4];

		   MyComponent() {
			   slices[0] = new PieValue(25, Color.red);
			   slices[1] = new PieValue(33, Color.green);
			   slices[2] = new PieValue(20, Color.pink);
			   slices[3] = new PieValue(15, Color.blue);
		   }

		   // This method is called whenever the contents needs to be painted
		   public void paint(Graphics g) {
			   // Draw the pie
			   drawPie((Graphics2D)g, getBounds(), slices);


		}
	}
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1