I am posting my working Mortgage calculator code, the displaychart code, and an example of what I know I need to do to call the display chart but need to incorporate it into my program.
Here is the code to my functioning program:
/*
/** Mortgage Calculator Program
* Week 5 PRG421
*Change Request #7
High-Level Requirements
Requestor: Ninfa Pendleton - Rapid City, SD
Write the program in Java (with a graphical user interface)
and have it calculate and display the mortgage payment amount from user input
of the amount of the mortgage and the user's selection from a menu of available mortgage loans:
- 5 years at 5%
- 7 years at 5.35%
- 15 years at 5.5 %
- 30 years at 5.75%
Use an array for the mortgage data for the different loans.
Read the year and interest rates to fill the array from a sequential file.
Display the mortgage payment amount followed by the loan balance and interest paid
for each payment over the term of the loan. Add graphics in the form of a chart
(functioning pie chart or bar graph, that shows loan principal, total interest, and total amount paid over loan).
Allow the user to loop back and enter a new amount and make a new selection or quit.
Please insert comments in the program to document the program.
*
*Author:
*Due September 20, 2010*/
//import needed packages
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.text.*;
import java.util.*;
//declare calculator class
public class Calculator extends JFrame {
// Double[] rates;
// Holds years, interest rate strings read from file
ArrayList<String> standardTerms = new ArrayList<String>();
String[] rates;
String[] years;
private JLabel amountLabel = new JLabel("Mortgage Amount:");
private JTextField amount = new JTextField();
private JLabel termLabel = new JLabel("Term of Loan:");
private JTextField term = new JTextField();
private JLabel rateLabel = new JLabel("Rate: ('0.0525')");
private JTextField rate = new JTextField();
private JComboBox termList = new JComboBox();
private JLabel payLabel = new JLabel("Monthly Payment:");
private JLabel payment = new JLabel();
private JButton calculate = new JButton("Calculate");
private JButton clear = new JButton("Clear");
private JButton exit = new JButton("Exit");
private JTextArea paymentSchedule = new JTextArea();
private JScrollPane schedulePane = new JScrollPane(paymentSchedule);
private Container cp = getContentPane();
public Calculator(String title) {
super.setTitle(title);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getStandardLoanTermsFromFile();
termList.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox) e.getSource();
int cbIndex = cb.getSelectedIndex();
if (cbIndex > -1) {
String[] parts = standardTerms.get(cbIndex).split(",");
term.setText(years[cbIndex]);
rate.setText(rates[cbIndex]);
}
}
});
//handle the button events
calculate.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//calculate the payments.
double p = Double.parseDouble(amount.getText());
double r = Double.parseDouble(rate.getText()) / 12;
double n = Integer.parseInt(term.getText()) * 12;
double monthlyPayment = p * Math.pow(1 + r, n) * r / (Math.pow(1 + r, n) - 1);
DecimalFormat df = new DecimalFormat("$###,###.00");
payment.setText(df.format(monthlyPayment));
//calculate the loan information
double principal = p;
int month;
StringBuffer buffer = new StringBuffer();
buffer.append("Month\tAmount\tInterest\tBalance\n");
for (int i = 0; i < n; i++) {
month = i + 1;
double interest = principal * r;
double balance = principal + interest - monthlyPayment;
buffer.append(month + "\t");
buffer.append(new String(df.format(principal)) + "\t");
buffer.append(new String(df.format(interest)) + "\t");
buffer.append(new String(df.format(balance)) + "\n");
principal = balance;
}
paymentSchedule.setText(buffer.toString());
} catch (Exception ex) {
System.out.println(ex);
}
}
});
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
term.setText("");
rate.setText("");
amount.setText("");
payment.setText("");
paymentSchedule.setText("");
}
});
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(1);
}
});
JPanel calcScreen = new JPanel();
calcScreen.setLayout(new GridLayout(10, 4));
calcScreen.add(amountLabel);
calcScreen.add(amount);
calcScreen.add(new Label("Select Mortgage Terms from List or enter manually"));
calcScreen.add(termList);
calcScreen.add(termLabel);
calcScreen.add(term);
calcScreen.add(rateLabel);
calcScreen.add(rate);
calcScreen.add(payLabel);
calcScreen.add(payment);
JPanel buttons = new JPanel();
buttons.setLayout(new BoxLayout(buttons, BoxLayout.X_AXIS));
buttons.add(calculate);
buttons.add(clear);
buttons.add(exit);
JPanel calc = new JPanel();
calc.setLayout(new BoxLayout(calc, BoxLayout.Y_AXIS));
calc.add(calcScreen);
calc.add(buttons);
cp.add(BorderLayout.NORTH, calc);
cp.add(BorderLayout.CENTER, schedulePane);
}
/** class MyPanel extends JPanel
{
public void paintComponent( Graphics g )
{
super.paintComponent( g ); // Always call the original paintComponent
// ... before doing your stuff
Graphics2D g2D = (Graphics2D) g;
g.setColor(Color.black);
g.fillRect(0,0,300,400);
g.setColor(Color.red);
g.fillRect(40,300,15,15);
g.setColor(Color.white);
g.drawString("Total Mortgage Principal "+ (100 - (int)totalinterestpercentage) + " %",60,312);
g.drawString("Total Mortgage Interest "+ (int)totalinterestpercentage + " %",60,337);
g.setColor(Color.blue);
g.fillRect(40,325,15,15);
g.setColor(Color.red);
g.fillArc(50, 50, 200, 200, 90, 360);
g.setColor(Color.blue);
g.fillArc(50, 50, 200, 200, 90, (int) interestdegrees);
setBackground(Color.black);
}
}*/
private void getStandardLoanTermsFromFile() {
try {
String line;
BufferedReader br = new BufferedReader(new FileReader("term.txt"));
while ((line = br.readLine()) != null) {
standardTerms.add(line);
termList.addItem(line);
}
br.close();
int numberOfLoans = standardTerms.size();
//allocate the arrays
rates = new String[numberOfLoans];
years = new String[numberOfLoans];
String [] temp;
for (int index = 0; index < numberOfLoans; index ++)
{
temp = standardTerms.get(index).split(",");
years[index] = temp[0];
rates[index] = temp[1];
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
public static void main(String[] args) {
Calculator frame = new Calculator("Mortgage Calculator");
frame.setSize(400, 600);
frame.setVisible(true);
}
}
Here is the code for my displaychart class:
import java.awt.*;
import javax.swing.JPanel;
public class DisplayChart extends JPanel
{
public DisplayChart()
{
JPanel dc = new JPanel();
add(dc);
}
public void paint(Graphics comp)
{
Graphics2D comp2D = (Graphics2D) comp;
comp.setColor(Color.gray);
comp.fillRect(0,0,300,400);
comp.setColor(Color.yellow);
comp.fillRect(40,300,15,15);
comp.setColor(Color.green);
comp.drawString("Total Mortgage Principal " + (100-(int)principal) + "%",60,312);
comp.drawString("Total Mortgage Interest " +(int)interest+ "%",60,337);
comp.setColor(Color.blue);
comp.fillRect(40,325,15,15);
comp.setColor(Color.red);
comp.fillArc(50,50,200,90,360);
comp.setColor(Color.blue);
comp.fillArc(50,50,200,200,90, (int)interest + principal);
setBackground(Color.black);
}
}
and I know i have to put something like this in my program somewhere to get it to work:
ublic class Viewer {
public static void main(String[] args)
{
JFrame frame = new Jframe();
frame.setSize(300,400);
frame.setTitle("Mortgage Chart");
fram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DisplayChart component = new DisplayChart();
frame.add(component);
frame.setVisible(true);
}
}
Any guidance or suggestions will be greatly appreciated. Today and tomorrow then I can work with Java for fun instead of out of desparation...LOL
The file that works with the program is also attached.
Attached File(s)
-
term.txt (41bytes)
Number of downloads: 203

New Topic/Question
Reply




MultiQuote







|