Java Programming Problem-Need HELP!
Hello everyone. I wanted to see if anyone can help me with my programming assignment. This is due by tonight at midnight (EST.). I had to break the coding up into 2 different posts because of the length. This is the first post of the code. I will post the 2nd part of the coding as a response to this post. My assignment is to read the interest rates and terms for the array from a text file, then add graphics in the form of a chart that displays the change in principle balance over the life of the loan. If anyone can help me, it will be greatly appreciated.
/*Java Package Imports*
import java.awt.*;
import javax.swing.*;
import java.text.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.awt.geom.*;
import java.io.*;
public class MortgageCalculatorGUI5 extends JFrame implements ItemListener, ActionListener
{
/*Declaring and initializing variables*/
int terms[] = {7,15,20,30};
double rates[] = {5.35, 5.5, 5.65,5.75};
double rateIn = 0;
double termIn = 0;
double functionx = 0;
/*Welcome display or output*/
JPanel row1 = new JPanel();
JTextArea welcome = new JTextArea(" Welcome to Nicole's Mortgage Calculator!"
+ "\n1. Please enter a loan amount."
+ "\n2. Check either \"Preset Values\" or \"Manual Input.\" then check a rate and term."
+ "\n3. Click the Enter button to view your monthly mortgage payments and amortization table."
+ "\n4. Click the Reset button to return all values to the default.", 5, 23);
/*Amount field*/
JPanel row2 = new JPanel();
JLabel amountLabel = new JLabel("1." + " Please enter the loan amount: $", JLabel.RIGHT);
JTextField amount = new JTextField(10);
/*Preset term and rate checkboxes*/
JPanel row3 = new JPanel();
JCheckBox preset = new JCheckBox("2." + " Check Preset Values", false);
JCheckBox tr1 = new JCheckBox("7 year, 5.35%", false);
JCheckBox tr2 = new JCheckBox("15 year, 5.5%", false);
JCheckBox tr4 = new JCheckBox("20 year, 5.65%", false);
JCheckBox tr3 = new JCheckBox("30 year, 5.75%", false);
JCheckBox stealth2 = new JCheckBox("", true);
/*Entered input term and rate fields*/
JPanel row4 = new JPanel();
JCheckBox manual = new JCheckBox("3." + " Check Manual Input", false);
JLabel termLabel = new JLabel("Check the term in years: ", JLabel.RIGHT);
JTextField term = new JTextField("",2);
JLabel rateLabel = new JLabel("Check the interest rate: ", JLabel.RIGHT);
JTextField rate = new JTextField("",4);
/*Checkbox resetting.*/
JCheckBox stealth1 = new JCheckBox("", true);
/*Established control buttons*/
JPanel row5 = new JPanel();
ButtonGroup buttons = new ButtonGroup();
JButton enter = new JButton(" Enter ");
JButton reset = new JButton(" Reset ");
JButton exit = new JButton(" Exit ");
JButton graph = new JButton("View Graph");
/*Monthly Payment field*/
JPanel row6 = new JPanel();
JLabel moPmtLabel = new JLabel("The monthly payment will be: ", JLabel.RIGHT);
JTextField moPmt = new JTextField (10);
/*Amortization Table*/
JPanel row7 = new JPanel();
JTextArea amorTable = new JTextArea("Month Payment Interest Balance", 11, 35);
JScrollPane sp7 = new JScrollPane(amorTable,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
/*Created the GUI*/
public MortgageCalculatorGUI5()
{
super("Nicole's Mortgage Calculator5");
setSize (700,700); // Presets window size
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout layout = new FlowLayout();
Container pane = getContentPane(); //Create a container for the panes
pane.setBackground(Color.blue); // Set the background color of the pane to orange
pane.setLayout(layout);
/*Added button group for checkboxes*/
ButtonGroup boxes = new ButtonGroup();
boxes.add(tr1);
boxes.add(tr2);
boxes.add(tr3);
boxes.add(tr4);
boxes.add(stealth2);
/*Add another button group for checkboxes*/
ButtonGroup choice = new ButtonGroup();
choice.add(preset);
choice.add(manual);
choice.add(stealth1);
/*Made the Welcome box blue*/
welcome.setBackground(Color.pink);
/*Made the checkboxes blue*/
tr1.setBackground(Color.pink);
tr2.setBackground(Color.pink);
tr3.setBackground(Color.pink);
tr4.setBackground(Color.pink);
/*Created event listeners for each button*/
tr1.addItemListener(this);
tr2.addItemListener(this);
tr3.addItemListener(this);
tr4.addItemListener(this);
preset.addItemListener(this);
manual.addItemListener(this);
enter.addActionListener(this);
reset.addActionListener(this);
exit.addActionListener(this);
graph.addActionListener(this);
/*Row 1 of layout*/
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.setBackground(Color.blue);
row1.setLayout(layout1);
row1.add(welcome);
welcome.setEditable(false); //disable editing of welcome message
pane.add(row1); // Create the pane
/*Row 2 of layout*/
FlowLayout layout2 = new FlowLayout(FlowLayout.CENTER, 5, 5);
row2.setBackground(Color.pink);
row2.setLayout(layout2);
row2.add(amountLabel);
row2.add(amount);
pane.add(row2);
/*Row 3 of layout*/
FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER, 5, 5);
row3.setBackground(Color.pink);
row3.setLayout(layout3);
row3.add(preset);
row3.add(tr1);
row3.add(tr2);
row3.add(tr3);
row3.add(tr4);
/*Disabled by default*/
tr1.setEnabled(false);
tr2.setEnabled(false);
tr3.setEnabled(false);
tr4.setEnabled(false);
pane.add(row3);
/*Row 4 of layout*/
FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER, 5, 5);
row4.setBackground(Color.pink);
row4.setLayout(layout4);
row4.add(manual);
row4.add(termLabel);
row4.add(term);
row4.add(rateLabel);
row4.add(rate);
/*Disabled by default*/
termLabel.setEnabled(false);
term.setEnabled(false);
rateLabel.setEnabled(false);
rate.setEnabled(false);
pane.add(row4);
/*Row 5 of layout*/
FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER, 5, 5);
row5.setBackground(Color.pink);
row5.setLayout(layout5);
row5.add(enter);
row5.add(reset);
row5.add(exit);
row5.add(graph);
graph.setEnabled(false);
pane.add(row5);
/*Row 6 of layout*/
FlowLayout layout6 = new FlowLayout(FlowLayout.CENTER, 5, 5);
row6.setBackground(Color.pink);
row6.setLayout(layout6);
row6.add(moPmtLabel);
row6.add(moPmt);
moPmt.setEditable(false); //disable editing field
pane.add(row6);
/*Row 7 of layout*/
FlowLayout layout7 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row7.setBackground(Color.pink);
row7.setLayout(layout7);
row7.add(sp7); //Add the scrollpane instead of the component (amorTable)
amorTable.setEditable(false); //disable editing of welcome message
pane.add(row7); // Create the pane
setContentPane(pane);
setVisible(true);
setResizable(false); // Keeps the window from being resized
}
/
If anyone can help me at all, it will be greatly appreciated.
Thanks,
Nicole
[7868 byte] By [
Nkia1978] at [2007-11-11 10:17:01]

# 1 Re: Java Programming Problem-Need HELP!
Here is the 2nd part of the coding. Thanks everyone.
/
*Method to launch graph's Window*
/*Method to launch graph's Window*/
public void graphWindow()
{
ChartFrame graphwin = new ChartFrame();
graphwin.setBounds(300, 100, 400, 500);
}
/*Create event handlers for each button*/
public void itemStateChanged(ItemEvent event)
{
Object item = event.getItem();
if (item == tr1)
{
termIn = terms[0]*12;
rateIn = rates[0]/1200;
}
if (item == tr2)
{
termIn = terms[1]*12;
rateIn = rates[1]/1200;
}
if (item == tr3)
{
termIn = terms[2]*12;
rateIn = rates[2]/1200;
}
if (item == tr4)
{
termIn = terms[3]*12;
rateIn = rates[3]/1200;
}
if (item == preset)
{
gopreset();
}
if (item == manual)
{
gomanual();
}
}
public void gopreset() // switches the program for user input
{
term.setText("");
rate.setText("");
moPmt.setText("");
tr1.setEnabled(true);
tr2.setEnabled(true);
tr3.setEnabled(true);
tr4.setEnabled(true);
termLabel.setEnabled(false);
term.setEnabled(false);
rateLabel.setEnabled(false);
rate.setEnabled(false);
functionx = 1;
}
public void gomanual() // switches the program for user input
{
stealth2.setSelected(true);
tr1.setEnabled(false);
tr2.setEnabled(false);
tr3.setEnabled(false);
tr4.setEnabled(false);
termLabel.setEnabled(true);
term.setEnabled(true);
rateLabel.setEnabled(true);
rate.setEnabled(true);
functionx = 2;
}
public void actionPerformed(ActionEvent evt)throws NumberFormatException
{
try
{
/*Enter button event figures out the monthly payment and reports*/
if (evt.getSource() == enter)
{
if (functionx == 2)
{
graph.setEnabled(true); //Enable the View Graph button
amorTable.setText("Month Payment Interest Balance");
double termx = Double.parseDouble(term.getText());
double ratex = Double.parseDouble(rate.getText());
double termIn = termx * 12;
double rateIn = ratex/1200;
double amountIn = Double.parseDouble(amount.getText());
double payment = (amountIn * Math.pow(1 + rateIn,termIn) * rateIn) / (Math.pow(1 + rateIn,termIn) - 1); //Figures out the monthly payments.
/*Displays the monthly payment.*/
NumberFormat currency = NumberFormat.getCurrencyInstance();
String stringPmt;
stringPmt = NumberFormat.getCurrencyInstance().format(payment);
moPmt.setText(stringPmt);
/*amortization table*/
for(int i = 1; i <= termIn; ++i)
{
double intpmt = amountIn * rateIn; // Figures interest amount for each payment
amountIn = amountIn + intpmt; //Adds the interest to the principle
if(amountIn > payment)
amountIn = amountIn - payment;
else
{
payment = amountIn;
amountIn = 0;
}
String output = ("\n"
+i
+" "
+currency.format(payment)
+" "
+currency.format(intpmt)
+" "
+currency.format(amountIn));
amorTable.append(output);
}
}
}
if (functionx == 1)
{
graph.setEnabled(true);
amorTable.setText("Month Payment Interest Balance");
double amountIn = Double.parseDouble(amount.getText());
double payment = (amountIn * Math.pow(1 + rateIn,termIn) * rateIn) / (Math.pow(1 + rateIn,termIn) - 1); //Figures out the monthly payments.
/*Display the monthly payment.*/
NumberFormat currency = NumberFormat.getCurrencyInstance();
String stringPmt;
stringPmt = NumberFormat.getCurrencyInstance().format(payment);
moPmt.setText(stringPmt);
/*amortization table*/
for(int i = 1; i <= termIn; ++i)
{
double intpmt = amountIn * rateIn; // Figures interest amount for each payment
amountIn = amountIn + intpmt; //Adds the interest to the principle
if(amountIn > payment)
amountIn = amountIn - payment;
else
{
payment = amountIn;
amountIn = 0;
}
String output = ("\n"
+i
+" "
+currency.format(payment)
+" "
+currency.format(intpmt)
+" "
+currency.format(amountIn));
amorTable.append(output);
}
}
/*View Graph button event opens pie graph window*/
if (evt.getSource() == graph)
{
graphWindow();
}
/*Reset button event clears values*/
if (evt.getSource() == reset)
{
graph.setEnabled(false); //Disable the View Graph button
amount.setText("");
term.setText("");
rate.setText("");
moPmt.setText("");
amorTable.setText("Month Payment Interest Balance");
/*uncheck rate/term checkboxes*/
tr1.setSelected(false);
tr2.setSelected(false);
tr3.setSelected(false);
tr4.setSelected(false);
/*disable all rate/term fields*/
tr1.setEnabled(false);
tr2.setEnabled(false);
tr3.setEnabled(false);
tr4.setEnabled(false);
termLabel.setEnabled(false);
term.setEnabled(false);
rateLabel.setEnabled(false);
rate.setEnabled(false);
/*select hidden checkboxes*/
stealth1.setSelected(true);
stealth2.setSelected(true);
}
/*Exit button event exits the program*/
if (evt.getSource() == exit)
{
System.exit(-1); // exit the program
}
}
/*Error handler if non-numeric data is entered as loan amount*/
catch (NumberFormatException e)
{
String error = ("\nPlease ensure all fields contain only numeric data");
amorTable.append(error);
}
}
/*PiePanel class from Java website*/
class PiePanel extends JPanel
{
private PieSlice[] slice;
private int current = 0;
private float totalSize = 0;
private Color background;
public PiePanel(int sliceCount)
{
slice = new PieSlice[sliceCount];
background = getBackground();
}
public void addSlice(Color sColor, float sSize)
{
if (current <= slice.length) {
slice[current] = new PieSlice(sColor, sSize);
totalSize += sSize;
current++;
}
}
public void paintComponent(Graphics comp) {
super.paintComponent(comp);
Graphics2D comp2D = (Graphics2D) comp;
int width = getSize().width - 10;
int height = getSize().height - 15;
int xInset = 5;
int yInset = 5;
if (width < 5)
xInset = width;
if (height < 5)
yInset = height;
comp2D.setColor(background);
comp2D.fillRect(0, 0, getSize().width, getSize().height);
comp2D.setColor(Color.lightGray);
Ellipse2D.Float pie = new Ellipse2D.Float(
xInset, yInset, width, height);
comp2D.fill(pie);
float start = 0;
for (int i = 0; i < slice.length; i++) {
float extent = slice[i].size * 360F / totalSize;
comp2D.setColor(slice[i].color);
Arc2D.Float drawSlice = new Arc2D.Float(
xInset, yInset, width, height, start, extent,
Arc2D.Float.PIE);
start += extent;
comp2D.fill(drawSlice);
}
}
}
class PieSlice
{
Color color = Color.lightGray;
float size = 0;
PieSlice(Color pColor, float pSize)
{
color = pColor;
size = pSize;
}
}
class ChartFrame
{
Color color = Color.lightGray;
float size = 0;
ChartFrame(Color cColor, float cSize)
{
color = cColor;
size = cSize;
}
}
public static void main(String[] arguments) {
MortgageCalculatorGUI5 frame = new MortgageCalculatorGUI5();
}
}
/