Link to home
Start Free TrialLog in
Avatar of pgmtkl
pgmtkl

asked on

calling other programs

I am working on an assignment for a gui mortgage program that calls a support program. I have modeled it after a sample program we recieved in class but am confused. Can someone look and see what I am missing?? any help is appreciated. the 2 programs are listed below:


1st program-
import javax.swing.*;
import java.awt.*;

public class ThursdayMortgageWeek3GUI extends JFrame {
      ThursdaySupportWeek3 mortgageSupport=new ThursdaySupportWeek3(this);


JPanel row10 = new JPanel();
JLabel amntLabel = new JLabel("Mortgage Amount: $", JLabel.LEFT);
JTextField loanField = new JTextField(10);


JPanel row20 =new JPanel();
JComboBox options = new JComboBox();
JLabel optionsLabel = new JLabel();
JLable lbltype= new JLable("Please select Term and APR for Mortgage",JLabel.Left);
JTextField years                  = new JTextField(2);

JPanel row30 = new JPanel();
JButton calButton = new JButton("Calculate");
JButton resetButton = new JButton("Reset");
JButton endButton = new JButton("End");

JPanel row40 = new JPanel();
JLabel paymtLabel = new JLabel("Monthly Payment Amount:");
JTextField paymtField = new JTextField();

JPanel row50 = new JPanel();
JLabel outputLabel = new JLabel("Loan Amoritization Table");
JLabel lblPaymentNum= new JLabel("Payment Number",JLabel.LEFT);
JLabel lblInterest= new JLabel("Interest",JLabel.LEFT);
JLabel lblPrinciple= new JLabel("Principal",JLabel.LEFT);
JLabel lblBalance= new JLabel("Balance",JLabel.LEFT);

JPanel row60 = new JPanel();
JTextArea textField = new JTextArea(5, 55);
JLabel lblText= new JLabel("Output");


public ThursdayMortgageWeek3GUI() {

super("Week 3 - Mortgage GUI");
setSize(650,500);
setLocation(250,60);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Font f = new Font ("Arial", Font.PLAIN, 30);
Font b = new Font ("Arial", Font.BOLD, 16);
FlowLayout layout = new FlowLayout();
Container pane = getContentPane();
pane.setLayout(layout);


loanField.addActionListener(mortgageSupport);
options.addActionListener(mortgageSupport);
calButton.addActionListener(mortgageSupport);
resetButton.addActionListener(mortgageSupport);
endButton.addActionListener(mortgageSupport);

FlowLayout flow10 = new FlowLayout();
row10.setLayout(flow10);
row10.add(amntLabel);
row10.add(loanField);
pane.add(row10);


GridLayout flow20 = new GridLayout(1,4,5,1);
row20.setLayout(flow20);
row20.add(optionsLabel);
row20.add(options);
row20.add(lbltype)
options.setEnabled(true);
options.addItem("Select Option");
options.addItem("7 year at 5.35%");
options.addItem("15 year at 5.5%");
options.addItem("30 year at 5.75%");
pane.add(row20);


GridLayout flow30 = new GridLayout (1,4,75,1);
row30.add(calButton);
row30.add(resetButton);
row30.add(endButton);
pane.add(row30);

FlowLayout flow40 = new FlowLayout();
row40.setLayout(flow40);
paymtField.setLineWrap(true);
JScrollPane textpane= new JScrollPane(paymntField,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
row40.add(paymtLabel);
row40.add(paymtField);
pane.add(row40);

Grid Layout flow50= new GridLayout(1,4,5,1);
row50.setLayout(flow50);
rbTermInterest.setFont(b);
row50.add(outputLabel);
row50.add(lblPaymentNum);
row50.add(lblInterest);
row50.add(lblPrinciple);
row50.add(lblBalance);
pane.add(row50);      

FlowLayout flow60 = new FlowLayout();
txtResults.setLineWrap(true);
row60.setLayout(flow60);
JScrollPane textpane= new JScrollPane(paymntField,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
row60.add(lblText);
row60.add(textField);
pane.add(row60);


}

public static void main(String[] args) {
      ThursdayMortgageWeek3GUI guiFrame= new ThursdayMortgageWeek3GUI();
      guiFrame.setVisible(true);
      }
}
      
_______________________________________________________________________________

2nd program :

import java.awt.event.*;
import java.text.*;
import java.lang.*;
import java.io.*;

public class ThursdaySupportWeek3 implements ItemListener,ActionListener
{

ThursdayMortgageWeek3GUI gui;
double amount = 0;
double term = 0;
double intrst = 0;
double moIn = 0;
double moTrm = 0;
double prin = 0;
double payment = 0;
double termArray[] = { 7, 15, 30 };
double intrstArray[] = { 5.35, 5.50, 5.75 };

//to format payment amounts
DecimalFormat df = new DecimalFormat("$#,###.00");

public ThursdaySupportWeek3(ThursdayMortgageWeek3GUI  in)
{
gui=in;
}
public static void main(String []  args)
{
}
//

public void itemStateChanged(ItemEvent arg0) {

Object button = gui.options.getSelectedItem();
String buttonPress = button.toString();

Object source = event.getSource();

if (buttonPress == calButton) {
startCalculations();

}
if (buttonPress == resetButton) {
reset();

}
if (buttonPress == endButton) {
end();

}
}

public void actionPerformed(ActionEvent event)
{
      String buttonClicked = event.getActionComman();
      if (buttonClicked == "Select Option")
            setComboBoxNull();
      if (buttonClicked == "Select Option" && !gui.options.isSelected())
            clearResults();
            
      if (buttonClicked =="Calculate" && gui.options.isSelected())
            {
            try
            {
            amount = Double.parseDouble(loanField.getText());
                  } catch (NumberFormatException e) {
                  clearResults();
                  gui.textField.append("Please Do Not Use Commas.Invalid Entry");
                  loanField.setText(null);

}

termArray[0]=Double.parseDouble(txtYears);
intrstArray[0]=Double.parseDouble(txtIntRate);
array=0;
clearResults();
funcCalculate();
}

if (buttonClicked =="Calculate")
{
      clearResults();
      funcCalculate();
      }
if (buttonClicked == "Clear")
      setNull();
if (buttonClicked =="Exit")
      System.exit(0);
}
      
void startCalculations() {

do
{
            NumberFormat fmt = NumberFormat.getInstance();
            fmt.setGroupingUsed (true);
            fmt.setMaximumFractionDigits(2);
            fmt.setMinimumFractionDigits(2);
            String txtInitialAmount = gui.txtAmount.getText();
            gui.options.setEnabled(true);
            gui.calButton.setEnabled(true);

double newPrin = amount;
double newIn = moIn * newPrin;
double reduction = payment - newIn;
newPrin = newPrin - reduction;


for (int i = 0; i < options[Array]; i++) {
options.addItem(optionsArray[i]);
}
}
while (c<=options[Array]);
}

int index = options.getSelectedIndex();
term = termArray[index];
intrst = intrstArray[index];

if (amount > 0) {
amount = Double.parseDouble(loanField.getText());
moIn = (intrst / 12) / 100;
moTrm = term * 12;
payment = amount * (moIn / (1 - java.lang.Math.pow((1 + moIn), (-moTrm))));

paymtField.setText("" + df.format(payment));

textField.append("Payment#");
textField.append(" ");
textField.append("Payment Amount");
textField.append(" ");
textField.append("Interest");
textField.append(" ");
textField.append("Principle");
textField.append(" ");
textField.append("");
textField.append(" \n");



for (int i = 1; i <= moTrm; i++) {


textField.append(" " + i);
textField.append(" " + df.format(payment));
textField.append(" " + df.format(newIn));
textField.append(" " + df.format(reduction));
textField.append(" " + df.format(newPrin) + "\n");

}

}
if (amount < 0) {
JOptionPane.showMessageDialog(null, "Please Enter Valid Amount",
"Message Dialog", JOptionPane.PLAIN_MESSAGE);
loanField.setText(null);
}
}

void reset() {

loanField.setText(null);
paymtField.setText(null);

}

void end() {
System.exit(0);
}

}
}
Avatar of mindwalkr
mindwalkr

My advise is the following:

Get a decent IDE like eclipse and try creating the project in there. You have several typing errors in the code (you can get eclipse at www.eclipse.org)

After you do that, you can immediately correct the invalid classes (due to typing errors) in the code. Eclipse also detects incorrect methods you're calling on some objects and you an fix them in a second.

Basically what I think is missing in your program, is the "core" functionality. You must define the clearResults(), setNull() and funcCalculate(). My advise is to:

 - Make the funcCalculate() a public method of ThursdaySupport.
 - Make clearResults() a public method of ThursdayMortgage class.

funcCalculate, should access the ThursdayMortgage class, get the input values from there, and process the calculation. Then the result should be passed back to the ThursdayMortage again to be rendered.
clearResults should be accessed by the ThursdaySupport when the rest button is clicked. This method basically just sets all input fields to empty (if it's a text field, just do textField.setText("");

Without getting into much detail on the chosen architecture, which I think it's not clearly separated. The support class should only deal with numbers and calculations. The ThursdayMortgage should be only GUI. You should have more class to handle events. You can define inlined anynymous classes in the ThursdayMorgage class. One for each button... this way you don't have to be retrieving the event source and compare it to a literal String (which is so wrong, but works).
So... get it to work as it is, and then you can convert it to a different architecture :)

Hope it helps!

ASKER CERTIFIED SOLUTION
Avatar of Nguyen Huu Phuoc
Nguyen Huu Phuoc
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pgmtkl

ASKER

Thank you for your help.  I have made these changes and a few others. the first program compiles and then goes to the 2nd and I am getting error on this section. I have a public class to get the actions performed. I am getting error that the compiler can not find the symbol : method for setNull() and then clear results. the Gui is selected. Am i missing something in this portion of the code??


public void actionPerformed(ActionEvent event)
{
      String buttonClicked = event.getActionCommand();
      if (buttonClicked == "Select Option")
            setNull();
      if (buttonClicked == "Select Option" && !gui.options.isSelected())
            clearResults();
            
      if (buttonClicked =="Calculate" && gui.options.isSelected())
            {
            try
            {
            amount = Double.parseDouble(loanField.getText());
                  } catch (NumberFormatException e) {
                  clearResults();
                  gui.textField.append("Please Do Not Use Commas.Invalid Entry");
                  loanField.setText(null);
You haven't code   setNull(),  clearResults() method for that Class yet.
Phuoc H. Nguyen
Avatar of pgmtkl

ASKER

I have the declarations listed right below this section (shown below). Is it supposed to be listed at the beginning off program??

public void actionPerformed(ActionEvent event)
{
      String buttonClicked = event.getActionCommand();
      if (buttonClicked == "Select Option")
            setNull();
      if (buttonClicked == "Select Option" && !gui.options.isSelected())
            clearResults();
            
      if (buttonClicked =="Calculate" && gui.options.isSelected())
            {
            try
            {
            amount = Double.parseDouble(loanField.getText());
                  } catch (NumberFormatException e) {
                  clearResults();
                  gui.textField.append("Please Do Not Use Commas.Invalid Entry");
                  loanField.setText(null);

}

termArray[0]=Double.parseDouble(txtYears);
intrstArray[0]=Double.parseDouble(txtIntRate);
array=0;
clearResults();
funcCalculate();
setNull();
}