Please Help me!!! I know this is a stupid newbie error, but I am a newbie. My program compiles and runs, but I can't get the interestP and the principalP to pass to the other class to fill the graph. I have worked on this non-stop all day.
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
import java.lang.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
import javax.swing.JTabbedPane;
import javax.swing.JComponent;
import java.awt.font.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.text.NumberFormat;
// Create MortgageCalculator class
public class MortCal5 extends JFrame implements ActionListener
{
//declare variables
double principle = 0.0;
double interest = 0.0;
int years = 0;
double months = 0;
int termArray[] = {7, 15, 30};
double[] rates;
private double intTotal = 0.0;
private double priTotal = 0.0;
private double payTotal = 0.0;
double interestP = 0.0;
double principalP = 0.0;
double[] values;
private String[] names;
JFrame f = new JFrame();
// Text for Mortgage
JPanel row1 = new JPanel(); // Sets up Panels, Preselect Buttons, and Frame.
JLabel mortgageLabel = new JLabel("MORTGAGE PAYMENT CALCULATOR",
JLabel.CENTER);
JPanel row2 = new JPanel(new GridLayout(1, 2));
JLabel principalLabel = new JLabel("Mortgage Amount $", JLabel.LEFT);
JTextField principalText = new JTextField(10);
JPanel row3 = new JPanel (new GridLayout(1, 2));
JLabel termLabel = new JLabel("Mortgage Term (Years)", JLabel.LEFT);
JTextField termText = new JTextField(3);
JPanel row4 = new JPanel(new GridLayout(1,2));
JLabel intRateLabel = new JLabel("Interest Rate (%)", JLabel.LEFT);
JTextField intRateText = new JTextField(5);
JPanel row5 = new JPanel(new GridLayout(1,2));
JLabel paymentLabel = new JLabel("Monthly Payment $", JLabel.LEFT);
JTextField paymentText = new JTextField(10);
JPanel row6 = new JPanel(new GridLayout (1, 4));
JLabel payLabel = new JLabel("Payment", JLabel.LEFT);
JLabel prinLabel = new JLabel("", JLabel.LEFT);//Left for spacing and future needs
JLabel intLabel = new JLabel("Interest", JLabel.LEFT);
JLabel balLabel = new JLabel("Balance", JLabel.LEFT);
// Create buttons to calculate payment, clear payment field and exit program
JPanel radioPanel = new JPanel();
JRadioButton button7 = new JRadioButton("7 Years at 5.35%", false);
JRadioButton button15 = new JRadioButton("15 Years at 5.50%", false);
JRadioButton button30 = new JRadioButton("30 Years at 5.75%", false);
JPanel button = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 10));
JButton calculateButton = new JButton("CALCULATE");
JButton clearButton = new JButton("CLEAR");
JButton exitButton = new JButton("EXIT");
JTextArea displayArea = new JTextArea(10, 30);
JScrollPane scroll = new JScrollPane(displayArea);
JTextArea graphArea = new JTextArea (10, 30);
JButton graphButton = new JButton("DISPLAY GRAPH");
//sets decimal format
DecimalFormat dollarAmount = new DecimalFormat("0.00");
DecimalFormat percentAmount = new DecimalFormat("##.##");
DataInputStream istream;
//constructor Builds and displays GUI
public void frame1()
{
this.setTitle("Mortgage Calculator");
setSize(450, 450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
getRates();
Border rowboarder = new EtchedBorder();
pane.add(row1);
row1.add(mortgageLabel);
row1.setMaximumSize(new Dimension (10000, row1.getMinimumSize().height));
pane.add(row2);
row2.add(principalLabel);
row2.add(principalText);
row2.setMaximumSize(new Dimension (10000, row2.getMinimumSize().height));
principalText.setText("200000");
pane.add(row3);
row3.add(termLabel);
row3.add(termText);
row3.setMaximumSize(new Dimension (10000, row3.getMinimumSize().height));
termText.setEditable(false);
termText.setText("30");
pane.add(row4);
row4.add(intRateLabel);
row4.add(intRateText);
row4.setMaximumSize(new Dimension (10000, row4.getMinimumSize().height));
intRateText.setEditable(false);
intRateText.setText("5.75");
// set Radio Buttons as group so only one can be selected
ButtonGroup buttonGrp = new ButtonGroup();
buttonGrp.add(button7);
buttonGrp.add(button15);
buttonGrp.add(button30);
//add radio buttons to GUI
radioPanel.add(button7);
radioPanel.add(button15);
radioPanel.add(button30);
pane.add(radioPanel);
radioPanel.setMaximumSize(new Dimension(10000,
radioPanel.getMinimumSize().height));
pane.add(row5);
row5.add(paymentLabel);
row5.add(paymentText);
row5.setMaximumSize(new Dimension (10000, row5.getMinimumSize().height));
paymentText.setEditable(false);
// Add Buttons for calculate, clear, and exit
button.add(calculateButton);
button.add(clearButton);
button.add(exitButton);
pane.add(button);
// Add Listeners to buttons
calculateButton.addActionListener(this);
clearButton.addActionListener(this);
exitButton.addActionListener(this);
button7.addActionListener(this);
button15.addActionListener(this);
button30.addActionListener(this);
graphButton.addActionListener(this);
//Add labels above text box (Amortization)
pane.add(row6);
row6.add(payLabel);
row6.add(balLabel);
row6.add(intLabel);
row6.add(prinLabel);//added for spacing and future need
row6.setMaximumSize(new Dimension (10000, row6.getMinimumSize().height));
pane.add(row6);
scroll.setBorder(BorderFactory.createEtchedBorder());
pane.add(scroll);
pane.add(graphButton);
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
setContentPane(pane);
setVisible(true);
}
//Main method
public static void main(String[] args)throws ParseException
{
MortCal5 frame = new MortCal5();
frame.frame1();
}
//opens the rates.txt file and reads the interest rates into the array
public void getRates()
{
try
{
ArrayList<String> arrayRates = new ArrayList<String>();
BufferedReader inputfile = new BufferedReader(new FileReader("rates.txt"));
String data;
while ((data = inputfile.readLine()) != null)
{
System.out.println(data);
arrayRates.add(data);
}
rates = new double[arrayRates.size()];
for (int i = 0; i < rates.length; ++i)
{
rates[i] = Double.parseDouble(arrayRates.get(i));
}
inputfile.close();
}
catch(Exception ec)
{
JOptionPane.showMessageDialog(null,
"Could not find the file specified", "Error Message",
JOptionPane.ERROR_MESSAGE);
}
}
//Verifies input for positive number and returns payment amount
public void actionPerformed(ActionEvent event) {
if (event.getSource() == button7)
{
years = 7;
for (int i = 0; i < rates.length; ++i)
{
interest = rates[0];
}
}
else if (event.getSource() == button15)
{
years = 15;
for (int i = 0; i < rates.length; ++i)
{
interest = rates[1];
}
}
else if (event.getSource() == button30)
{
years = 30;
for (int i = 0; i < rates.length; ++i)
{
interest = rates[2];
}
}
intRateText.setText(Double.toString(interest));
termText.setText(Integer.toString(years));
if (event.getSource() == calculateButton)
{
try
{
displayArea.setText(null);
String principalTest = principalText.getText();
principle = Double.parseDouble(principalTest);
if (principle < 1)
{
throw new OutOfRangeException();
}
}
catch (NumberFormatException e)
{
principalText.setText("Invalid Amount");
JOptionPane.showMessageDialog(null, "Enter Positive Numbers Only ", "INVALD ENTRY", JOptionPane.ERROR_MESSAGE);
return;
}
catch (OutOfRangeException e)
{
principalText.setText("Invalid Amount");
JOptionPane.showMessageDialog(null, "Enter Positive Numbers Only ", "INVALD ENTRY", JOptionPane.ERROR_MESSAGE);
return;
}
setResultValue();
}
//clears fields
if (event.getSource() == clearButton)
{
principalText.setText("");
termText.setText("");
intRateText.setText("");
paymentText.setText("");
displayArea.setText(null);
}
if (event.getSource() == graphButton)
{
JFrame f = new JFrame("Chart");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.getContentPane().add(new ChartPanel(values, names, "Principle/Interest"));
f.setSize(400, 300);
f.setLocation(300,300);
f.setVisible(true);
double principleP = 0.0;
double interestP = 0.0;
double[] values = new double[2];
String[] names = new String[2];
values[0] = principleP;
names[0] = "Principle";
values[1] = interestP;
names[1] = "Interest";
System.out.println(interestP);
f.getContentPane().add(new ChartPanel(values, names, "Principle/Interest"));
}
if (event.getSource() == exitButton)
{
System.exit(0);
}
}
//Calculates mortgage payment, creates and displays amortization schedule
public void setResultValue()
{
double amount = Double.parseDouble(principalText.getText());
double term = Double.parseDouble(termText.getText());
double rate = Double.parseDouble(intRateText.getText()) / 100.;
double result = (amount * ( rate/12))/(1-( Math.pow (1/( 1 +(rate/12)), (term*12))));
paymentText.setText("" + dollarAmount.format(result));
double intPaid = 0;
double principalPaid = 0;
double balance = amount;
double monthlyRate = rate / 12;
double months = term * 12;
int payment = 1;
for (int y = 1; y <= term; y++)
{
displayArea.append("");
// Loop to to calculate and display the payment schedule
for (int m = 0; m < 12; m++)
{ /* start inner loop */
intPaid = balance * monthlyRate;
principalPaid = result - intPaid;
balance = balance - principalPaid;
displayArea.append("Month " + payment + "\t" + " " +" "
+ dollarAmount.format(balance) + "\t" +" "
+ dollarAmount.format(intPaid) + "\n");
payment++;
displayArea.setCaretPosition(0);
intTotal += intPaid;
priTotal += principalPaid;
payTotal += result;
interestP = (intTotal)/(payTotal) * 100;
principalP = (priTotal)/(payTotal) * 100;
}
}
System.out.println(intTotal);//temporary to check results of total (will delete)
System.out.println(priTotal);
/*public double getinterestP()
{
interestP = (intTotal)/(payTotal) * 100;
return interestP;
}*/
}
}
//class to extend Exception to validate numbers for negative and 0 input
class OutOfRangeException extends Exception //Burd, B. (2007). Java for Dummies. Indianapolis, IN: Wiley Publishing, Inc..
{
}
class ChartPanel extends JPanel {
private double[] values;
private String[] names;
private String title;
public ChartPanel(double[] v, String[] n, String t)
{
names = n;
values = v;
title = t;
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if (values == null || values.length == 0)
return;
double minValue = 0;
double maxValue = 0;
for (int i = 0; i < values.length; i++) {
if (minValue > values[i])
minValue = values[i];
if (maxValue < values[i])
maxValue = values[i];
}
Dimension d = getSize();
int clientWidth = d.width;
int clientHeight = d.height;
int barWidth = clientWidth / values.length;
Font titleFont = new Font("SansSerif", Font.BOLD, 20);
FontMetrics titleFontMetrics = g.getFontMetrics(titleFont);
Font labelFont = new Font("SansSerif", Font.PLAIN, 10);
FontMetrics labelFontMetrics = g.getFontMetrics(labelFont);
int titleWidth = titleFontMetrics.stringWidth(title);
int y = titleFontMetrics.getAscent();
int x = (clientWidth - titleWidth) / 2;
g.setFont(titleFont);
g.drawString(title, x, y);
int top = titleFontMetrics.getHeight();
int bottom = labelFontMetrics.getHeight();
if (maxValue == minValue)
return;
double scale = (clientHeight - top - bottom) / (maxValue - minValue);
y = clientHeight - labelFontMetrics.getDescent();
g.setFont(labelFont);
for (int i = 0; i < values.length; i++) {
int valueX = i * barWidth + 1;
int valueY = top;
int height = (int) (values[i] * scale);
if (values[i] >= 0)
valueY += (int) ((maxValue - values[i]) * scale);
else {
valueY += (int) (maxValue * scale);
height = -height;
}
g.setColor(Color.lightGray);
g.fillRect(valueX, valueY, barWidth - 2, height);
g.setColor(Color.black);
g.drawRect(valueX, valueY, barWidth - 2, height);
int labelWidth = labelFontMetrics.stringWidth(names[i]);
x = i * barWidth + (barWidth - labelWidth) / 2;
g.drawString(names[i], x, y);
}
}
}