Need Help Finishing program

Calculator Completion Assistance

Page 1 of 1

0 Replies - 2780 Views - Last Post: 17 May 2005 - 06:39 PM Rate Topic: -----

#1 12_MMth_99  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-May 05

Need Help Finishing program

Post icon  Posted 17 May 2005 - 06:39 PM

I am trying to finish up my mortgage calculator with the following information. If someone could let me know how and what I have to do to incorporate the following instructions into my program, I would be so ever grateful:

Add a field for the user to enter his annual salary. After the user has selected a mortgage and the monthly mortgage payment has been calculated, determine if the user "qualifies" for the loan by comparing 28% of the user's monthly salary to the mortgage payment amount. If 28% of the monthly salary equals or exceeds the mortgage payment, post the words "You qualify!" in large, bold, colored letters on the screen, and display a graphic of a house near the bottom of the screen. If 28% of the monthly salary is less than the mortgage payment, post the word "Sorry" in large, italic, black letters on the screen, and display a graphic of the international no symbol near the bottom of the screen.

Here is my code for reference:

[/CODE]
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import java.text.NumberFormat;
import java.text.*;
import java.util.*;
import java.io.*;
import java.text.DecimalFormat;


public class MikeFinalMorCalc extends JFrame implements ActionListener
{

JLabel AmountLabel = new JLabel(" Enter Mortgage Amount:$ ");
JTextField mortgageAmount = new JTextField(10);

JLabel SalaryLabel = new JLabel(" Annual Salary:$ ");
JTextField salaryAmount = new JTextField(10);
JButton sumAvg = new JButton("Summary Calculation");

JLabel PaymentLabel = new JLabel(" Monthly Payment: ");
JTextField monthlyPayment = new JTextField(10);

JLabel chosenPTlabel = new JLabel(" Chosen Term and interest Rate: ");
JTextField chosenPT = new JTextField(10);

JButton newcalcButton = new JButton("Reset values");

JTextArea mortgageTable = new JTextArea(10,35);
JScrollPane scroll = new JScrollPane(mortgageTable);

String term[];
String rate[];
String menuChoice[];
int numRec;
double iInterestRate;
int iTermAmt;

public MikeFinalMorCalc()
{
super("MORTGAGE CALCULATOR");
fRecordCount();
term = new String[numRec];
rate = new String[numRec];
readFile();

JMenuBar mb = new JMenuBar();

JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic('F');
JMenuItem exitItem = new JMenuItem("Exit");
fileMenu.add(exitItem);
exitItem.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});

mb.add(fileMenu);
setJMenuBar(mb);

JMenu choicesMenu = new JMenu("Choices");
choicesMenu.setMnemonic('C');
menuChoice = new String[numRec];
for(int x=0; x < numRec; x++) {
menuChoice[x] = (term[x] + " Yrs" + " @ " + rate[x] + "%" );
JMenuItem menuPct_Term = new JMenuItem(menuChoice[x]);
menuPct_Term.setActionCommand(menuChoice[x]);
menuPct_Term.addActionListener(this);
choicesMenu.add(menuPct_Term);
}

choicesMenu.addActionListener(new ActionListener()
{
public void actionPerformed (ActionEvent e) {

String mi = e.getActionCommand();
System.out.println(mi);

}
} );



exitItem.addActionListener(new ActionListener ()
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
});

mb.add(choicesMenu);
setJMenuBar(mb);



setSize(600, 400);
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel pane = new JPanel();
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
Container grid = getContentPane();
grid.setLayout(new GridLayout(5,3));
pane.add(grid);
pane.add(scroll);
grid.setBackground(Color.yellow);
setCursor(new Cursor(Cursor.HAND_CURSOR));

mortgageAmount.setBackground(Color.blue);
mortgageAmount.setForeground(Color.yellow);
mortgageAmount.setCaretColor(Color.white);
mortgageAmount.setFont(new Font("Times New Roman", Font.PLAIN, 18));

salaryAmount.setBackground(Color.blue);
salaryAmount.setForeground(Color.yellow);
salaryAmount.setCaretColor(Color.white);
salaryAmount.setFont(new Font("Times New Roman", Font.PLAIN, 18));

monthlyPayment.setBackground(Color.blue);
monthlyPayment.setForeground(Color.yellow);
monthlyPayment.setFont(new Font("Times New Roman", Font.PLAIN, 18));

chosenPT.setBackground(Color.blue);
chosenPT.setForeground(Color.yellow);
chosenPT.setFont(new Font("Times New Roman", Font.PLAIN, 18));

mortgageTable.setBackground(Color.black);
mortgageTable.setForeground(Color.red);
mortgageTable.setFont(new Font("Times New Roman", Font.PLAIN, 14));

grid.add(AmountLabel);
grid.add(mortgageAmount);

grid.add(PaymentLabel);
grid.add(monthlyPayment);

grid.add(SalaryLabel);
grid.add(salaryAmount);

monthlyPayment.setEditable(false);
grid.add(newcalcButton);
grid.add(sumAvg);

setContentPane(pane);
setVisible(true);

newcalcButton.addActionListener(this);
mortgageAmount.addActionListener(this);
monthlyPayment.addActionListener(this);
salaryAmount.addActionListener(this);
sumAvg.addActionListener(this);


}

public void actionPerformed(ActionEvent e)
{
String mi = e.getActionCommand();
System.out.println(mi);

Object command = e.getSource();

int loanTerm = 0;

{
loanTerm = 0;
}

{
loanTerm = 1;
}

{
loanTerm = 2;
}

double mortgage = 0;
double rate = 0;
double salary =0;
double [][] loans = {{7, 5.15}, {15, 5.875}, {30, 6.25},};

try
{

mortgage = Double.parseDouble(mortgageAmount.getText());

}
catch (NumberFormatException nfe)
{
JOptionPane.showMessageDialog (this, "Error! Invalid input!");
return;
}


double interestRate = loans [loanTerm][1];
double intRate = (interestRate / 100) / 12;
double loanTermMonths = loans [loanTerm] [0];
int months = (int)loanTermMonths * 12;
double interestRateMonthly = (intRate / 12);
double payment = mortgage * intRate / (1 - (Math.pow(1/(1 + intRate), months)));
double remainingLoanBalance = mortgage;
double monthlyPaymentInterest = 0;
double monthlyPaymentPrincipal = 0;
double salaryPercentage = 0;

NumberFormat myCurrencyFormatter = NumberFormat.getCurrencyInstance(Locale.US);

monthlyPayment.setText(myCurrencyFormatter.format(payment));
mortgageTable.setText("Month\tPrincipal\tInterest\tEnding Balance\n" +
"---------\t----------\t------------\t---------------------\n");

for (;months > 0 ; months -- )
{
monthlyPaymentInterest = (remainingLoanBalance * intRate);
//Calculate H = R x I
monthlyPaymentPrincipal = (payment - monthlyPaymentInterest);
//Calculate C = P - H
remainingLoanBalance = (remainingLoanBalance - monthlyPaymentPrincipal);
// Calculate R = R - C
salaryPercentage =(salary * 0.28);

// H = monthlyPaymentInterest
// R = remainingLoanBalance
// P = payment
// C = monthlyPaymentPrincipal
// I = interestRateMonthly

mortgageTable.setCaret (new DefaultCaret());
mortgageTable.append(String.valueOf(months) + "\t" +
myCurrencyFormatter.format(monthlyPaymentPrincipal) + "\t" +
myCurrencyFormatter.format(monthlyPaymentInterest) + "\t" +
myCurrencyFormatter.format(remainingLoanBalance) + "\n");
}
if(command == newcalcButton)
{
mortgageAmount.setText(null);
monthlyPayment.setText(null);
mortgageTable.setText(null);

}
}
public static void main(String[] args)
{
MikeFinalMorCalc JLabel= new MikeFinalMorCalc();
}


public void fRecordCount() {

try {

BufferedReader reader = new BufferedReader(new FileReader("LoanChoices.txt"));
String temp;
numRec = 0;
while((temp = reader.readLine())!= null){
numRec ++;
}

numRec = numRec /2;

reader.close( );

}
catch (IOException e) {

System.out.println("There was an input/output error.");
System.exit(0);

}

}


public void readFile(){

try {
BufferedReader reader2 = new BufferedReader(new FileReader("LoanChoices.txt"));
for(int xx = 0; xx < numRec;xx ++) {
term[xx] = reader2.readLine();
rate[xx] = reader2.readLine();
}

reader2.close( );
}
catch (IOException e) {
System.out.println("There was an input/output error.");
System.exit(0);
}

}

}
[CODE]

Is This A Good Question/Topic? 0
  • +

Page 1 of 1