Monthly Loan Payment.java Code
/*
* MonthlyLoanPayment.java
*
* Created on September 15, 2000, 5:55 PM
*/
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
/**
*
* @author hunt, jan
* @version
*/
public class MonthlyLoanPayment extends java.applet.Applet implements ActionListener {
private Button btnCalculate = new Button("Calculate");
private Button btnReset = new Button("Reset");
private TextField txtAmount = new TextField(10);
private TextField txtInterestRate = new TextField(10);
private TextField txtYears = new TextField(10);
private TextField txtMonthlyPayment = new TextField(10);
private String strMonthlyPayment; //calculated monthly payment, as a string
public void init () {
Font[] fonts= { new Font("Verdana", Font.BOLD, 14), new Font("Verdana", Font.PLAIN, 14)
};//two fonts
//set layout
setLayout(new GridLayout(5,2)); //builds layout with 5 rows, 2 columns
Panel p0 = new Panel(); //create panel
p0.add(new Label("Amount:")); //add label of Amount to the panel
p0.setFont(fonts[0]);//set font for Amount to array 0
add(p0); //adds panel to the applet
txtAmount.requestFocus(); // puts focus on amount
Panel p1 = new Panel(); //create panel
p1.add(txtAmount); //add a text field to the panel
add(p1); //adds panel to the applet
Panel p2 = new Panel(); //create panel
p2.add(new Label("Interest Rate:")); //add label of Amount to the panel
p2.setFont(fonts[0]);
add(p2); //adds panel to the applet
Panel p3 = new Panel(); //create panel
p3.add(txtInterestRate); //add a text field to the panel
add(p3); //adds panel to the applet
Panel p4 = new Panel(); //create panel
p4.add(new Label("Years:")); //add label of Amount to the panel
p4.setFont(fonts[0]);
add(p4); //adds panel to the applet
Panel p5 = new Panel(); //create panel
p5.add(txtYears); //add a text field to the panel
add(p5); //adds panel to the applet
Panel p6 = new Panel(); //create panel
p6.add(new Label("Monthly Payment:")); //add label of Amount to the panel
p6.setFont(fonts[0]);
add(p6); //adds panel to the applet
Panel p7 = new Panel(); //create panel
p7.add(txtMonthlyPayment); //add a text field to the panel
p7.setFont(fonts[0]);
add(p7); //adds panel to the applet
txtMonthlyPayment.setBackground(new Color(255,255,128));
txtMonthlyPayment.setEditable(false);
Panel p8 = new Panel(); //create panel
p8.add(new Label("Enter amount, rate, years.")); //add label of Amount to the panel
p8.add(new Label("Then press Calculate."));
p8.setFont(fonts[1]); //set font for directions to array 1
add(p8); //adds panel to the applet
Panel p9 = new Panel(); //create panel
p9.add(btnCalculate); //add the calculate button to panel p9
p9.add(btnReset);//add the reset button to panel p9
btnCalculate.addActionListener(this); //add the actionListener to the button
btnReset.addActionListener(this);
add(p9); //adds panel to the applet
btnCalculate.setBackground(Color.red);
}//end init()
public void actionPerformed(ActionEvent event) {
String strAmount; //loan amount entered by user
String strInterestRate; //interest rate on loan entered by user
String strYears; //years of loan
/*if the user clicked Calculate button, get the data from input text
boxes, convert to numbers, do math, output result.*/
if(event.getSource()== btnCalculate) {
//Get the loan amount, interest rate, and years of loan
strAmount = txtAmount.getText();
strInterestRate = txtInterestRate.getText();
strYears = txtYears.getText();
if(strAmount.equals("")) {
txtAmount.setText("Need Value");
}//end else if for empty string in amount
if(strInterestRate.equals("")) {
txtInterestRate.setText("Need Value");
}//end else for interest rate
if(strYears.equals("")) {
txtYears.setText("Need Value");
}//end else for years
else if((strYears.equals("Need Value"))||(strAmount.equals("Need Value"))||(strInterestRate.equals("Need Value"))) {
return;
}//end if for need value
else {
int amount = Integer.parseInt(strAmount);
//code below does not execute in IE
//double interestRate = Double.parseDouble(strInterestRate);
Double Irate = new Double(strInterestRate); //use this line and the line below
double interestRate = Irate.doubleValue(); //to create the object and then the double
int years = Integer.parseInt(strYears);
if((amount > 0)&&(interestRate > 0)&&(years > 0)) {
calculateMonthlyPayment(amount, interestRate, years);
}//end if for strAmount > 0
else(txtMonthlyPayment.setText("No Zeros!"));
}// end else for empty strings
}//end if for getSource btnCalculate
else if(event.getSource()== btnReset) {
txtAmount.setText("");//set fields back to empty
txtInterestRate.setText("");
txtYears.setText("");
txtMonthlyPayment.setText("");
txtAmount.requestFocus();//regain focus in txtamount
}//end else if
}//end actionPerformed
public void calculateMonthlyPayment(int amount, double interestRate, int years) {
//calculate the monthly payment
double rate = interestRate/12;
double d = Math.pow(1+rate,-12.0*years);
double MonthlyPayment =(amount*rate)/(1-d);
//float intMonthlyPayment = (float) MonthlyPayment;//implicit casting
//strMonthlyPayment = String.valueOf(intMonthlyPayment);
Double y = new Double(MonthlyPayment);
String z = y.toString();
txtMonthlyPayment.setText(z.substring(0,z.indexOf(".") + 3)); //put payment string in the output text box and
//finds the decimal and makes cuts off anything past two decimal places.
}//end calculate method
}//end class MonthlyLoanPayment