-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCalculatingLoanPayment.java
188 lines (146 loc) · 4.21 KB
/
CalculatingLoanPayment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package Solutions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//we are calculating the loan payment for different banks like HDFC, ICICI, SBI etc.
interface Bank {
String getBankName();
}
class HDFC implements Bank {
private final String BNAME;
public HDFC() {
BNAME = "HDFC BANK";
}
public String getBankName() {
return BNAME;
}
}
class ICICI implements Bank {
private final String BNAME;
ICICI() {
BNAME = "ICICI BANK";
}
public String getBankName() {
return BNAME;
}
}
class SBI implements Bank {
private final String BNAME;
public SBI() {
BNAME = "SBI BANK";
}
public String getBankName() {
return BNAME;
}
}
abstract class Loan {
protected double rate;
abstract void getInterestRate(double rate);
public void calculateLoanPayment(double loanamount, int years) {
/*
* to calculate the monthly loan payment i.e. EMI
*
* rate=annual interest rate/12*100; n=number of monthly installments;
* 1year=12 months. so, n=years*12;
*
*/
double EMI;
int n;
n = years * 12;
rate = rate / 1200;
EMI = ((rate * Math.pow((1 + rate), n)) / ((Math.pow((1 + rate), n)) - 1)) * loanamount;
System.out.println("your monthly EMI is " + EMI + " for the amount" + loanamount + " you have borrowed");
}
}
class HomeLoan extends Loan {
public void getInterestRate(double r) {
rate = r;
}
}
class BussinessLoan extends Loan {
public void getInterestRate(double r) {
rate = r;
}
}
class EducationLoan extends Loan {
public void getInterestRate(double r) {
rate = r;
}
}
abstract class AbstractFactory {
public abstract Bank getBank(String bank);
public abstract Loan getLoan(String loan);
}
class BankFactory extends AbstractFactory {
public Bank getBank(String bank) {
if (bank == null) {
return null;
}
if (bank.equalsIgnoreCase("HDFC")) {
return new HDFC();
} else if (bank.equalsIgnoreCase("ICICI")) {
return new ICICI();
} else if (bank.equalsIgnoreCase("SBI")) {
return new SBI();
}
return null;
}
public Loan getLoan(String loan) {
return null;
}
}
class LoanFactory extends AbstractFactory {
public Bank getBank(String bank) {
return null;
}
public Loan getLoan(String loan) {
if (loan == null) {
return null;
}
if (loan.equalsIgnoreCase("Home")) {
return new HomeLoan();
} else if (loan.equalsIgnoreCase("Business")) {
return new BussinessLoan();
} else if (loan.equalsIgnoreCase("Education")) {
return new EducationLoan();
}
return null;
}
}
class FactoryCreator {
public static AbstractFactory getFactory(String choice) {
if (choice.equalsIgnoreCase("Bank")) {
return new BankFactory();
} else if (choice.equalsIgnoreCase("Loan")) {
return new LoanFactory();
}
return null;
}
}
public class CalculatingLoanPayment {
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the name of Bank from where you want to take loan amount: ");
String bankName = br.readLine();
System.out.print("\n");
System.out.print("Enter the type of loan e.g. home loan or business loan or education loan : ");
String loanName = br.readLine();
AbstractFactory bankFactory = FactoryCreator.getFactory("Bank");
Bank b = bankFactory.getBank(bankName);
System.out.print("\n");
System.out.print("Enter the interest rate for " + b.getBankName() + ": ");
double rate = Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("Enter the loan amount you want to take: ");
double loanAmount = Double.parseDouble(br.readLine());
System.out.print("\n");
System.out.print("Enter the number of years to pay your entire loan amount: ");
int years = Integer.parseInt(br.readLine());
System.out.print("\n");
System.out.println("you are taking the loan from " + b.getBankName());
AbstractFactory loanFactory = FactoryCreator.getFactory("Loan");
Loan l = loanFactory.getLoan(loanName);
l.getInterestRate(rate);
l.calculateLoanPayment(loanAmount, years);
}
}