• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 971
  • Last Modified:

Add graphics in the form of a chart to an Amortization/Mortgage program

How can I modify program  to read the interest rates to fill the array from a sequential file and display /add graphics in the form of a chart."


This What  i Have so far:


import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;

//Declared program AmorSchedule class
public class  Schedule2 extends JFrame implements ActionListener,ItemListener { // Creates

//Declared calculation varibles and strings

int term = 0;

double principal = 0;

double rate = 0;

double monthlyPayment = 0;

double interest = 0;

String mPrincipal[] = {"200000", "250000","300000"};

String mTerm[] = { "7", "15", "30" };

String mInterst[] = { "5.35%", "5.50%", "5.75%" };

//The purpose of this program is to create an Amortization Paymment Schedule
//The functions below will initiate display panel
// Format panel frame and preselect label buttons.
JPanel row1 = new JPanel();
JLabel mortgage_label = new JLabel("Wonda's Amortization Schedule Calculator",
JLabel.CENTER);//This function creates title bar in Gui panel

JPanel row2 = new JPanel(new GridLayout(1, 2));

JLabel principal_label = new JLabel("Mortgage Principal $", JLabel.LEFT);

JTextField principal_txt = new JTextField(10);//Set text size parameters

JPanel row3 = new JPanel(new GridLayout(1, 2));

JLabel term_label = new JLabel("Mortgage Term (Yrs)", JLabel.LEFT);

JTextField term_txt = new JTextField(10);

JPanel row4 = new JPanel(new GridLayout(1, 2));

JLabel rate_label = new JLabel("Interest Rate (%)", JLabel.LEFT);

JTextField rate_txt = new JTextField(10);

//This will display buttons just above text area output
JPanel radioPanel = new JPanel();
Container contentPane=getContentPane();
JComboBox jc=new JComboBox();

JPanel row5 = new JPanel(new GridLayout(1, 2));

JLabel payment_label = new JLabel("Monthly Payment $", JLabel.LEFT);

JTextField payment_txt = new JTextField(10);

// The function below will set and aling button lay out panel
JPanel button = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 12));

JButton clearButton = new JButton("Clear");

JButton exitButton = new JButton("Exit");

JButton calculateButton = new JButton("Calculate");


JButton nop=new JButton("# of Payment");

JButton amt = new JButton("Amount");

JButton interestb = new JButton("Interest");

JButton balanceb = new JButton("Balance");

// The is will display number of payments,amount due after payment
//interest rate and pay off balance in text box  area
JTextArea displayArea = new JTextArea(100,135);//Text are paramenters set

JScrollPane scroll = new JScrollPane(displayArea);

//Container method componets
public  Schedule2()
{

super("Wonda's Amortization Schedule ");
setSize(600,800);//Set initial container program diameter
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();

Border rowborder = new EmptyBorder(3, 10, 3, 10);

pane.add(row1);
row1.add(mortgage_label);
row1.setMaximumSize(new Dimension(7500, row1.getMinimumSize().height));
row1.setBorder(rowborder);

pane.add(row2);
row2.add(principal_label);
row2.add(principal_txt);
row2.setMaximumSize(new Dimension(7500, row2.getMinimumSize().height));
row2.setBorder(rowborder);

pane.add(row3);
row3.add(term_label);
row3.add(term_txt);
row3.setMaximumSize(new Dimension(7500, row3.getMinimumSize().height));
row3.setBorder(rowborder);

pane.add(row4);
row4.add(rate_label);
row4.add(rate_txt);
row4.setMaximumSize(new Dimension(7500, row4.getMinimumSize().height));
row4.setBorder(rowborder);

//
contentPane.setLayout(new FlowLayout(FlowLayout.CENTER));
jc.addItem("Select number of years and interest rate from this drop down");
jc.addItem("200000 7 Years at 5.35%");//Array for first calculation
jc.addItem("250000 15 Years at 5.50%");//Array for second calculation
jc.addItem("300000 30 Years at 5.75%");//Array for third calculation

contentPane.add(jc);
pane.add(row5);
row5.add(payment_label);
row5.add(payment_txt);
payment_txt.setEnabled(false); // This function will set payment amount uneditable
row5.setMaximumSize(new Dimension(7500, row5.getMinimumSize().height));
row5.setBorder(rowborder);

//The function bellow will set intial astetic look and color to button
//text are  back ground
button.add(calculateButton);
calculateButton.setBackground( Color.green);//This command will add button color background

button.add(clearButton);
clearButton.setBackground( Color.yellow);//This command will add button color background

button.add(exitButton);
exitButton.setBackground( Color.orange);//This command will add button color background

displayArea.setBackground( Color.white);//This command will set text color background

pane.add(button);
JPanel button1 = new JPanel(new FlowLayout(FlowLayout.CENTER,62, 8));//This will set button parameters over text area
button1.add(nop);
button1.add(amt);
button1.add(interestb);
button1.add(balanceb);
pane.add(button1);

button.setMaximumSize(new Dimension(7500, button.getMinimumSize().height));

scroll.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
pane.add(scroll);


pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
setVisible(true);
setContentPane(pane);

// Event handling codes for buttons, dropped down menus, mouse events and calcualtion
// This section is to add listeners
jc.addItemListener(this);// This listner add drop down box
clearButton.addActionListener(this);
exitButton.addActionListener(this);
calculateButton.addActionListener(this);

}
//The function below will auto populate upon drop down selection
//display terms in years and interest rate
//in designated text field set to calculate at user manual input
public void itemStateChanged(ItemEvent ie){
if(ie.getItem()== "200000 7 Years at 5.35%"){
principal  = 200000;
rate = 5.35;
term = 7;
} else if (ie.getItem()== "250000 15 Years at 5.50%") {
principal  = 250000;
rate = 5.50;
term = 15;
} else if (ie.getItem()== "300000 30 Years at 5.75%") {
principal  = 300000;
rate = 5.75;
term = 30;
}
principal_txt.setText(Double.toString(principal));
rate_txt.setText(Double.toString(rate));
term_txt.setText(Integer.toString(term));

}
//Declared action listner method will calculate upon
//button selection, clear or exit program upon selecting desired
//action determined by end user
public void actionPerformed(ActionEvent event)

{
Object source = event.getSource();
if (source instanceof AbstractButton) {

if (event.getSource() == calculateButton) {

try {
      Payment();
      
////Validate users input
//If no numerical value entered by end user
//this function will display error message notifying
//end user of invalid entry prompting to try again
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Amount Entered Must Be A Value More Than Zero. This Is An Invaild Entry! Please Try Again",
"ERROR", JOptionPane.ERROR_MESSAGE);
}
}

if (event.getSource() == clearButton) {
principal_txt.setText(null);
term_txt.setText(null);
rate_txt.setText(null);
payment_txt.setText(null);
displayArea.setText(null);
}

if (event.getSource() == exitButton) {
System.exit(0);
}
}
}

//This function below will calculate payments
public void Payment()
{

// function below will calculate principal loan      
double principal = Double.parseDouble(principal_txt.getText()); // Principal

// This function below will calculate rate and term
float rate1 = Float.parseFloat((String) rate_txt.getText()); // Interest
double rate = rate1 / 100;
int term = Integer.parseInt((String) term_txt.getText()); // Length of Loan

// Monthly interst rate
double perMonthRate = rate / 12;

// Number of months over which loan is amortized
double termMonths = term * 12;

//Program calculation formula

double monthlyPayment = principal
* (perMonthRate / (1 - Math.pow((1 + perMonthRate), (-termMonths))));

//Program varibles formatted in $ currency,% for monthly interest rates
// and mortgage interest rates based on terms and condition of year
DecimalFormat df = new DecimalFormat("\u00A4#,##0.00"); //to transcribe to currency
DecimalFormat pf = new DecimalFormat("#,##0.00%"); //to transcribe to percentage
DecimalFormat mi = new DecimalFormat("#,##0.000%"); //to transcribe to percentage
payment_txt.setText("" + df.format(monthlyPayment));

// Amoritization variables declared to
//set interest paid on initial loan amount,
//prinicpal applied to each payment
//ultimately displaying total principal owed after payment
double interestPaid = 0;  
double monthlyPrincipal = 0;
double principalBalance = principal;

//Payment number set up
int paymentNumber = 1;
int temp=1;

// This loop increments the term length in years based on specified selection
for (int y = 1; y <= term; y++) { // This functon will intiate outer loop

// Inserts a blank line
displayArea.append("");

// This loop will  calculate and display the amortization payment schedule
for (int m = 0; m < 12; m++) { // This funtione will initiate inner loop

interestPaid = principalBalance * perMonthRate;
monthlyPrincipal = monthlyPayment - interestPaid;
principalBalance = principalBalance - monthlyPrincipal;
displayArea.append("Payment " + paymentNumber + "\t\t"
+ df.format(monthlyPrincipal) + "\t\t" + df.format(interestPaid)
+ "\t\t" + df.format(principalBalance) + "\n");
paymentNumber++;
temp++;
if(temp==36){
try
 
//This loop will hesitate program  five second before calculation begins
     {
        Thread.currentThread().sleep(500);
      }
      catch (InterruptedException e)
      {
        e.printStackTrace();
      }
      temp=1;
}
displayArea.setCaretPosition(10);
}

}

}

//End of declared main method
public static void main(String[] arguments) {

 Schedule2 mgt = new  Schedule2();


}
}

Help I need to complete this today!!
0
WondaBriagar
Asked:
WondaBriagar
  • 3
2 Solutions
 
hoomanvCommented:
to create charts use JFreeChart
http://www.jfree.org/jfreechart/index.php
0
 
Mayank SAssociate Director - Product EngineeringCommented:
There are plenty of open-source reporting and charting tools available in Java which can be used:

http://java-source.net/open-source/charting-and-reporting
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> ie.getItem()== "250000 15 Years at 5.50%"

You need to use equals () while comparing Strings everywhere:

ie.getItem().equals ( "250000 15 Years at 5.50%" )
0
 
WondaBriagarAuthor Commented:
I am still confused I tried to substitute above code only to get compiling  error
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You must have substituted it wrongly. Post one of the lines where you get that error.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now