Repository navigation
Expand file tree
/
Copy pathBankService.java
More file actions
149 lines (110 loc) · 3.65 KB
/
Copy pathBankService.java
File metadata and controls
149 lines (110 loc) · 3.65 KB
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
import java.util.*;
public class BankService {
private Scanner sc = new Scanner(System.in);
private ArrayList<BankAccount> accounts = new ArrayList<>();
public void createAccount() {
sc.nextLine(); // clear buffer
System.out.print("Enter Account Number: ");
String accNo = sc.nextLine();
System.out.print("Enter Name: ");
String name = sc.nextLine();
System.out.print("Initial Balance: ");
double balance = sc.nextDouble();
System.out.println("1. Savings 2. Current");
int type = sc.nextInt();
if (type == 1) {
accounts.add(new SavingsAccount(accNo, name, balance, 0.05));
} else {
accounts.add(new CurrentAccount(accNo, name, balance, 2000));
}
System.out.println("Account Created!");
}
public void performTransaction() {
System.out.print("Enter Account Number: ");
String accNo = sc.next();
BankAccount acc = findAccount(accNo);
if (acc == null) {
System.out.println("Account not found!");
return;
}
System.out.println("1. Deposit 2. Withdraw");
int ch = sc.nextInt();
System.out.print("Enter amount: ");
double amt = sc.nextDouble();
if (ch == 1)
acc.deposit(amt);
else
acc.withdraw(amt);
}
public void viewAccounts() {
for (BankAccount acc : accounts) {
acc.displayAccountInfo();
System.out.println("----------------------");
}
}
public void loanMenu() {
System.out.print("Enter Account Number: ");
String accNo = sc.next();
BankAccount acc = findAccount(accNo);
if (acc == null) {
System.out.println("Account not found!");
return;
}
if (!(acc instanceof Loanable)) {
System.out.println("Loan not supported!");
return;
}
Loanable loanAcc = (Loanable) acc;
System.out.println("1. Apply Loan");
System.out.println("2. Repay Loan");
System.out.println("3. Loan Status");
int ch = sc.nextInt();
switch (ch) {
case 1:
loanAcc.applyLoan();
break;
case 2:
loanAcc.repayLoan();
break;
case 3:
loanAcc.getLoanStatus();
break;
default:
System.out.println("Invalid choice");
}
}
private BankAccount findAccount(String accNo) {
for (BankAccount acc : accounts) {
if (acc.accountNumber.equals(accNo)) {
return acc;
}
}
return null;
}
public void fd() {
System.out.println("=== Fixed Deposit ===");
System.out.print("Enter name: ");
sc.next();
String name = sc.nextLine();
System.out.print("Enter age: ");
int age = sc.nextInt();
double principal;
System.out.print("Enter amount: ");
principal = sc.nextDouble();
while (principal <= 0) {
System.out.println("Enter valid amount:");
principal = sc.nextDouble();
}
int duration;
System.out.print("Enter duration (months): ");
duration = sc.nextInt();
while (duration <= 0) {
System.out.println("Enter valid duration:");
duration = sc.nextInt();
}
FixedDeposit fd = new FixedDeposit(principal, duration, age);
System.out.println("Interest Rate: " + fd.getInterestRate());
System.out.println("Interest: " + fd.calculateInterest());
System.out.println("Total: " + (principal + fd.calculateInterest()));
}
}