Skip to main content

Help.....

No replies
Luna1983
Offline
Joined: 2012-02-05
Points: 0

I was wondering if anyone could help me figure out why my 7 year mortgage does not work but my 15 and 30 years do. Any assistance would be greatly appreciated. I have also checked this program in Netbeans and Textpad and I have no errors, but it not working properly. Here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class MortCalc5 extends JFrame implements ActionListener {

// Set two-places for decimal format
DecimalFormat twoPlaces = new DecimalFormat("$0.00");

// varibles for calculator
double Loan;
double MonPay;
double IntRate;
int Pay;
String Amount;

JTextField LoanAmount;
JComboBox loanType;
JLabel PaymentAmounts;
JTextArea PaymentsArea;
JTextField payment;
JTextField LoanTerm;
JTextField interest;

// arrays for mortgages (Years, interest rate, monthly payment)
double[] mort1= {7.0, 5.35, 0.0};
double[] mort2= {15.0, 5.5, 0.0};
double[] mort3= {30.0, 5.75, 0.0};

public MortCalc5() {

super("Mortgage Calculator");
setSize(500,550);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Loan Panel
JPanel AmtPanel= new JPanel();
JLabel AmtLabel= new JLabel("Loan Amount:", JLabel.LEFT);
LoanAmount= new JTextField(10);
AmtPanel.add(AmtLabel);
AmtPanel.add(LoanAmount);

//Type Panel
JPanel TypePanel= new JPanel();
JLabel TypeLabel= new JLabel("Loan Type:", JLabel.LEFT);
String[] loans = {"7 Years at 5.35%", "15 years at 5.5%", "30 years at 5.75%"};
loanType = new JComboBox(loans);
TypePanel.add(TypeLabel);
TypePanel.add(loanType);

//Button Panel
JPanel Calculate= new JPanel();
JButton CalcButt= new JButton("Compute Payment");
CalcButt.addActionListener(this);
Calculate.add(CalcButt);

//Payment Panel
JPanel TermInterest= new JPanel();
JLabel AmountLabel= new JLabel("Payment:");
payment= new JTextField(8);
payment.setEditable(false);
//Term Panel
JLabel TermLabel= new JLabel("Term:");
LoanTerm= new JTextField(6);
LoanTerm.setEditable(false);
//Interest Panel
JLabel InterestLabel= new JLabel("Interest Rate:");
interest= new JTextField(8);
interest.setEditable(false);

TermInterest.add(AmountLabel);
TermInterest.add(payment);
TermInterest.add(TermLabel);
TermInterest.add(LoanTerm);
TermInterest.add(InterestLabel);
TermInterest.add(interest);

//Payments Panel
JPanel Payments= new JPanel();
PaymentsArea= new JTextArea("", 20, 40);
PaymentsArea.setEditable(false);
PaymentsArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(PaymentsArea);
Payments.add(scroll);

//window panel
JPanel window= new JPanel();
BoxLayout Box = new BoxLayout(window, BoxLayout.Y_AXIS);
window.setLayout(Box);
window.add(AmtPanel);
window.add(TypePanel);
window.add(Calculate);
window.add(TermInterest);
window.add(Payments);
add(window);

//visibility
setVisible(true);
}

public void actionPerformed (ActionEvent e) {
//clear list
PaymentsArea.setText("");

//Get amount
Amount= LoanAmount.getText();

//verify
try {
Loan = Double.valueOf(Amount).doubleValue();

//Mortgage Selection
Object type = loanType.getSelectedItem();
String LoanSelected = type.toString();

//Mortgage Arrays

//7 year

if (LoanSelected.equals("7 years at 5.35%")){
//calculation
mort1[2] = (Loan + (Loan * (mort1[1]/ 100))) / (mort1[0] * 12);
double int1 = (Loan * (mort1[1] / 100))/84;
double amtRemain1 = Loan + (Loan * (mort1[1]));

//term interest panel
payment.setText(twoPlaces.format(mort1[2]));
LoanTerm.setText("84 Months");
interest.setText("5.35%");

//loop payments
for (int pay=1; pay<=84; pay++){
//payment - balance
amtRemain1 = amtRemain1 - mort1[2];

//payment to textarea
PaymentsArea.append("Payment " + pay + ": " + twoPlaces.format(mort1[2]) +
" / " + "Interest: " + twoPlaces.format(int1) + " / " +
"Remaining: " + twoPlaces.format(amtRemain1) + "\n");
}
} else{

//15 year loan
if (LoanSelected.equals("15 years at 5.5%")){
//calcuation
mort2[2] = (Loan + (Loan * (mort2[1]/ 100))) / (mort2[0] * 12);
double int2 = (Loan * (mort2[1] / 100))/180;
double amtRemain2 = Loan + (Loan * (mort2[1]));

//term interest panel
payment.setText(twoPlaces.format(mort2[2]));
LoanTerm.setText("180 Months");
interest.setText("5.5%");

//loop payments
for (int pay=1; pay<=180; pay++){
//payment - balance
amtRemain2 = amtRemain2 - mort2[2];

//payment to textarea
PaymentsArea.append("Payment " + pay + ": " + twoPlaces.format(mort2[2]) +
" / " + "Interest: " + twoPlaces.format(int2) + " / " +
"Remaining: " + twoPlaces.format(amtRemain2) + "\n");
}
} else {

//30 year loan
if (LoanSelected.equals("30 years at 5.75%")){
//calcuation
mort3[2] = (Loan + (Loan * (mort3[1]/ 100))) / (mort3[0] * 12);
double int3 = (Loan * (mort3[1] / 100))/360;
double amtRemain3 = Loan + (Loan * (mort3[1]));

//term interest panel
payment.setText(twoPlaces.format(mort3[2]));
LoanTerm.setText("360 Months");
interest.setText("5.75%");

//loop payments
for (int pay=1; pay<=180; pay++){
//payment - balance
amtRemain3 = amtRemain3 - mort3[2];

//payment to textarea
PaymentsArea.append("Payment " + pay + ": " + twoPlaces.format(mort3[2]) +
" / " + "Interest: " + twoPlaces.format(int3) + " / " +
"Remaining: " + twoPlaces.format(amtRemain3) + "\n");
}
}

}}}
catch(Exception err){
PaymentsArea.setText("Please enter a loan amount.");
}
}

public static void main (String[] arguments){
MortCalc5 calc = new MortCalc5();
}
}