Java Program to Calculate Remaining Balance on a Loan
Often, you will want to know the remaining balance on a loan. It is easily calculated if you know the original principal, the interest rate, the loan length, and the number of payments made. To find the remaining balance, you must sum the payments, subtracting from each payment the amount allocated to interest. Then subtract that result from the principal.
The RemBalance applet is used to find the remaining balance of a loan.
Java Program For Bank Loan:
x
124
124
1
import java.awt.*;
2
import java.awt.event.*;
3
import javax.swing.*;
4
import java.text.*;
5
public class RemBalance extends JApplet
6
implements ActionListener {
7
JTextField orgPText, paymentText, remBalText,
8
rateText, numPayText;
9
JButton doIt;
10
double orgPrincipal;
11
double intRate;
12
double payment;
13
double numPayments;
14
final int payPerYear = 12;
15
NumberFormat nf;
16
public void init() {
17
try {
18
SwingUtilities.invokeAndWait(new Runnable () {
19
public void run() {
20
makeGUI();
21
}
22
});
23
} catch(Exception exc) {
24
System.out.println("Can't create because of "+ exc);
25
}
26
}
27
private void makeGUI() {
28
GridBagLayout gbag = new GridBagLayout();
29
GridBagConstraints gbc = new GridBagConstraints();
30
setLayout(gbag);
31
JLabel heading = new
32
JLabel("Find Loan Balance ");
33
JLabel orgPLab = new JLabel("Original Principal ");
34
JLabel paymentLab = new JLabel("Amount of Payment ");
35
JLabel numPayLab = new JLabel("Number of Payments Made ");
36
JLabel rateLab = new JLabel("Interest Rate ");
37
JLabel remBalLab = new JLabel("Remaining Balance ");
38
orgPText = new JTextField(10);
39
paymentText = new JTextField(10);
40
remBalText = new JTextField(10);
41
rateText = new JTextField(10);
42
numPayText = new JTextField(10);
43
remBalText.setEditable(false);
44
doIt = new JButton("Compute");
45
gbc.weighty = 1.0;
46
gbc.gridwidth = GridBagConstraints.REMAINDER;
47
gbc.anchor = GridBagConstraints.NORTH;
48
gbag.setConstraints(heading, gbc);
49
gbc.anchor = GridBagConstraints.EAST;
50
gbc.gridwidth = GridBagConstraints.RELATIVE;
51
gbag.setConstraints(orgPLab, gbc);
52
gbc.gridwidth = GridBagConstraints.REMAINDER;
53
gbag.setConstraints(orgPText, gbc);
54
gbc.gridwidth = GridBagConstraints.RELATIVE;
55
gbag.setConstraints(paymentLab, gbc);
56
gbc.gridwidth = GridBagConstraints.REMAINDER;
57
gbag.setConstraints(paymentText, gbc);
58
gbc.gridwidth = GridBagConstraints.RELATIVE;
59
gbag.setConstraints(rateLab, gbc);
60
gbc.gridwidth = GridBagConstraints.REMAINDER;
61
gbag.setConstraints(rateText, gbc);
62
gbc.gridwidth = GridBagConstraints.RELATIVE;
63
gbag.setConstraints(numPayLab, gbc);
64
gbc.gridwidth = GridBagConstraints.REMAINDER;
65
gbag.setConstraints(numPayText, gbc);
66
gbc.gridwidth = GridBagConstraints.RELATIVE;
67
gbag.setConstraints(remBalLab, gbc);
68
gbc.gridwidth = GridBagConstraints.REMAINDER;
69
gbag.setConstraints(remBalText, gbc);
70
gbc.anchor = GridBagConstraints.CENTER;
71
gbag.setConstraints(doIt, gbc);
72
add(heading);
73
add(orgPLab);
74
add(orgPText);
75
add(paymentLab);
76
add(paymentText);
77
add(numPayLab);
78
add(numPayText);
79
add(rateLab);
80
add(rateText);
81
add(remBalLab);
82
add(remBalText);
83
add(doIt);
84
orgPText.addActionListener(this);
85
numPayText.addActionListener(this);
86
rateText.addActionListener(this);
87
paymentText.addActionListener(this);
88
doIt.addActionListener(this);
89
nf = NumberFormat.getInstance();
90
nf.setMinimumFractionDigits(2);
91
nf.setMaximumFractionDigits(2);
92
}
93
public void actionPerformed(ActionEvent ae) {
94
double result = 0.0;
95
String orgPStr = orgPText.getText();
96
String numPayStr = numPayText.getText();
97
String rateStr = rateText.getText();
98
String payStr = paymentText.getText();
99
try {
100
if(orgPStr.length() != 0 &&
101
numPayStr.length() != 0 &&
102
rateStr.length() != 0 &&
103
payStr.length() != 0) {
104
orgPrincipal = Double.parseDouble(orgPStr);
105
numPayments = Double.parseDouble(numPayStr);
106
intRate = Double.parseDouble(rateStr) / 100;
107
payment = Double.parseDouble(payStr);
108
result = compute();
109
remBalText.setText(nf.format(result));
110
}
111
showStatus("");
112
} catch (NumberFormatException exc) {
113
showStatus("Invalid Data");
114
remBalText.setText("");
115
}
116
}
117
double compute() {
118
double bal = orgPrincipal;
119
double rate = intRate / payPerYear;
120
for(int i = 0; i < numPayments; i++)
121
bal -= payment - (bal * rate);
122
return bal;
123
}
124
}
Output: