Tuesday, August 7, 2007

code:Loan Amortization

This is a very useful loan amortization program. You will be asked to enter loan amount, number of years to pay of the loan, and annual interest rate. Once the input is entered the program will show you month by month interest, principal and balance.

This code is not java application but rather a java applet that can run in a web browser. Main difference between a java application and a java applet is that an applet does NOT have a main method that all java applications must have.

NOTE: I would like to thank all from the comp . lang . java . programmer goggle group that helped out with the amortization schedule formulas and the math part of the program as I am not that good in math :)


/**
* Author: http://javacodee.blogspot.com/
* Date: Aug 7, 2007
* Time: 7:59:28 PM
*/

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.lang.*;

public class LoanSchedule extends JApplet implements ActionListener
{
//Create all needed labels
private JLabel jlblLoanAmount = new JLabel("Loan Amount");
private JLabel jlblNumOfYears = new JLabel("Number Of Years");
private JLabel jlblInterestRate = new JLabel("Interest Rate (Annual)");

//Create all needed text fields
private JTextField jtfLoanAmount = new JTextField(10);
private JTextField jtfNumOfYears = new JTextField(10);
private JTextField jtfInterestRate = new JTextField(10);

//Calculate button is also needed
private JButton jbtCalculate = new JButton("Amortize Loan");

//...and a text area where the results will be displayed
private JTextArea jtaResults = new JTextArea();

public void init()
{
//Panel p1 will hold the input
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(3,2));
p1.add(jlblLoanAmount);
p1.add(jtfLoanAmount);
p1.add(jlblNumOfYears);
p1.add(jtfNumOfYears);
p1.add(jlblInterestRate);
p1.add(jtfInterestRate);

//Panel p2 will hold panel p1 and the calculate button
JPanel p2 = new JPanel();
p2.setLayout(new BorderLayout());
p2.setBorder(new TitledBorder("Enter loan amount, Number of years and annual interest rate"));
p2.add(p1, BorderLayout.WEST);
p2.add(jbtCalculate, BorderLayout.EAST);

//Action listener for the button
jbtCalculate.addActionListener(this);

//Make the text area scrollable and uneditable
JScrollPane scrollPane = new JScrollPane(jtaResults);
jtaResults.setRows(12);
jtaResults.setEditable(false);

//Place the two panels to the applet
getContentPane().add(p2, BorderLayout.CENTER);
getContentPane().add(scrollPane, BorderLayout.SOUTH);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == jbtCalculate)
calculateLoan();
else
System.out.println("you will never see this text!");
/*
Because we only have one element that uses actionListener (jbtCalculate) this method should only have
a call to calculate() method in it but i put the if-else statement just to show that it can handle other
elements with alctiolistener. For example, if we had another button that had addActionListener attached to it
we could say something like this:

if(e.getSource() == jbtCalculate)
{
do something here;
}
else if(e.getSource() == jbtSomeOtherElement)
{
do something else here;
}
*/
}

public void calculateLoan()
{
int numberOfYears = Integer.parseInt(jtfNumOfYears.getText());
double loanAmount = Double.parseDouble(jtfLoanAmount.getText());
double annualInterestRate = (Double.parseDouble(jtfInterestRate.getText())) / 100;

double monthlyInterestRate = annualInterestRate / 12;
double numberOfMonths = numberOfYears * 12;
double monthlyPayment = loanAmount * (monthlyInterestRate / (1 - Math.pow(1 + monthlyInterestRate, - numberOfMonths)));
double totalPayment = monthlyPayment * numberOfMonths;
double balance = loanAmount;
double interest;
double principal;

jtaResults.append("Payment#\t" + "Interest\t" + "Principal\t" + "Balance\n\n");

for(int i = 0; i < numberOfYears * 12; i++)
{
interest = (int)(monthlyInterestRate * balance * 100) / 100.0;
principal = (int)((monthlyPayment - interest) * 100 ) / 100.0;
balance = (int)((balance - principal) * 100 ) / 100.0;

jtaResults.append(i + 1 + "\t" + interest + "\t" + principal + "\t" + balance +"\n");
}

jtaResults.append("\n\nMonthly Payment: $" + (int)(monthlyPayment * 100) / 100.0 + "\n");
jtaResults.append("Total Payment: $" + (int)(totalPayment * 100) / 100.0 + "\n\n");
}

}

15 comments:

Voronetskyy Yevgen said...

Hi! Thank for you blog. I have a question: "How do you make your java code colored? Do you use any template for this purpose?"

Anonymous said...

Loan amortization daily activities come packed chronologically, which indicates that the transaction that is created first and major moves around the supposition that it must have taken position a whole interval of transaction after the mortgage was taken out, but not the first day of the mortgage, which is also known as the amount time frame.

Instant Payday Loans

Java said...

That is very nice. I am also writing many java programs for free learn at www.javatheprogram.blogspot.com.

Unknown said...

So luck to come across your excellent blog. Your blog brings me a great deal of fun.. Good luck with the site.
SM

Unknown said...

it won’t be a taboo topic however Going site Indianainvestmentwatch generally people are not enough to talk on such topics. To the next. Cheers

Emirates Loan said...

Thank for share such a nice information in your blog. can you tell us more details about Credit Card in UAE .

Goreng Ayam Marketing said...

AWESOME THE PROMOTION OF GIRL BEAUTIFUL SALES KLIK HERE SABUNG AYAM ONLINE

GOOD PRICE AND CHEAP
http://www.gorengayam.com 


PLEASE PRICE DIRECTLY

http://gorengayammarketing.wikidot.com/4-ciri-jalu-pada-ayam-petarung-yang-mematikan

Verisca sandi Anastasia said...

Agen Sabung Ayam Terbaik Indonesia AGENS128.
Agen Bola Sbobet Indonesia
Link Alternatif Sbobet
Contact Kami :
BBM : D8B84EE1 / AGENS128
Line id : agens1288
WhatsApp : 0877-8922-1725
Telegram : AgenS128 / https://t.me/AgenS128

Credit Loan Solution said...

Are you tired of seeking loans and Mortgages,have you been turned down constantly By your banks and other financial institutions,We offer any form of loan to individuals and corporate bodies at low interest rate.If you are interested in taking a loan,feel free to contact us today,we promise to offer you the best services ever.Just give us a try,because a trial will convince you.What are your Financial needs?Do you need a business loan?Do you need a personal loan?Do you want to buy a car?Do you want to refinance?Do you need a mortgage loan?Do you need a huge capital to start off your business proposal or expansion? Have you lost hope and you think there is no way out, and your financial burdens still persists? Contact us (gaincreditloan1@gmail.com)

Your Name:...............
Your Country:...............
Your Occupation:...............
Loan Amount Needed:...............
Loan Duration...............
Monthly Income:...............
Your Telephone Number:.....................
Business Plan/Use Of Your Loan:...............
Contact Us At : gaincreditloan1@gmail.com
Phone number :+1-270-551-4764 (WhatsApp Only)

Kyle Walker said...

WINNING303 - JUDI BOLA ONLINE | CASINO ONLINE | SLOT ONLINE | POKER ONLINE TERPERCAYA

BANDAR BOLA ONLINE

BANDAR TOGEL ONLINE

BANDAR SABUNG AYAM ONLINE

BANDAR TOGEL ONLINE

BANDAR BOLA ONLINE TERPERCAYA

* KUNJUNGI SITUS KAMI DI *

www.winning303.net


*NB : MENANG BERAPAPUN, PASTI KAMI BAYAR !!! *

* Melayani LiveChat 7 x 24 Jam Nonstop :
- WA : +6281717177303
- BBM : WINNING303
- LINE : WINNING303*

Fast Loan Finance said...


Do you need Personal Loan?
Business Cash Loan?
Unsecured Loan
Fast and Simple Loan?
Quick Application Process?
Approvals within 24-72 Hours?
No Hidden Fees Loan?
Funding in less than 1 Week?
Get unsecured working capital?
Contact Us At :oceancashcapital@gmail.com
Phone number :+16474864724 (Whatsapp Only)

LOAN SERVICES AVAILABLE INCLUDE:
================================
*Commercial Loans.
*Personal Loans.
*Business Loans.
*Investments Loans.
*Development Loans.
*Acquisition Loans .
*Construction loans.
*Credit Card Clearance Loan
*Debt Consolidation Loan
*Business Loans And many More:

LOAN APPLICATION FORM:
=================
Full Name:................
Loan Amount Needed:.
Purpose of loan:.......
Loan Duration:..
Gender:.............
Marital status:....
Location:..........
Home Address:..
City:............
Country:......
Phone:..........
Mobile / Cell:....
Occupation:......
Monthly Income:....

Contact Us At oceancashcapital@gmail.com
Phone number :+16474864724 (Whatsapp Only)

Anggastika said...

Cari Agen Bola Terpercaya atau Cari Agen Sabung Ayam Terpercaya??
Ada Nihh
agen sbobet

agensv388

Hubungi kami Sekarang Juga :
WA: +6287785425244
LINE: WINNING303

Kevin Lawrence said...

Getting a loan of $450.000.00 USD dollars with 2% interest rate per a year from Mr Lorenzo Diego Loan Firms was so easy.when other financial investors turned me down. Mr Lorenzo Diego offered me a loan that got my business going today He's Certified to Offer the Following Types of Loans * Personal Loans (Unsecured) * Business Loans (Unsecured) * Debt Consolidation Loans * Improve Your Home * Investment Loans. He's honored to meet your financial needs. Credit problems should not stop you from getting the loan you need. He can finance up to $10,000.00 USD Dollars to $100,000,000.00 USD Dollars anywhere in the world as long as He 2% interest can be guaranteed on these projects.
Email mrlorenzodiegoloanfirm@outlook.com
Whatsapp ... + 1 (346) 226-2778  

ACCESS LOAN FIRM (accessloansfirm@gmail.com) said...

You are welcome to ACCESS LOANS FIRM here we make sure you get the best loan for your business or pay your bills. Just answer a few simple questions and meet the terms. Contact us now for a 3% loan on all loans, it's easy, fast and secured.

AVAILABLE LOANS WE OFFER ARE;

*Personal Loan and Housing Loan,
* Home renovation and Hospital bills
* Enlargement of Business
* Refinancing Agricultural Extensions and Gold Mining
* Financing projects with higher financial needs
*Business Loan and Investment Loan 
CONTACT INFORMATIONS ARE;

HEAD OFFICE(accessloansfirm@gmail.com)

WHATSAPP:  +12342018860

 A good loan start with better Borrowing

Datafincorp said...

Do you need a genuine Loan to settle your bills and startup
business? contact us now with your details to get a good
Loan at a low rate of 3% per Annual email us:
Do you need Personal Finance?
Business Cash Finance?
Unsecured Finance
Fast and Simple Finance?
Quick Application Process?
Finance. Services Rendered include,
*Debt Consolidation Finance
*Business Finance Services
*Personal Finance services Help
Please write back if interested with our interest rate datanfincorpfinance@gmail.com