-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
239 lines (198 loc) · 10.7 KB
/
Main.java
File metadata and controls
239 lines (198 loc) · 10.7 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import java.io.*;
import java.util.*;
public class Main {
static HashMap<Integer,BankAccount> accounts=new HashMap<>();
static final String File_Name="accounts.txt";
static {
System.out.println("=================================");
System.out.println("\nWelcome to "+BankAccount.Bank_Name+".....\n");
System.out.println("=================================");
}
public static void main(String[] args) {
loadAccs();
syncCounter();
Scanner sc=new Scanner(System.in);
while(true){
System.out.println("=================================");
System.out.println("Press : ");
System.out.println("1. Create Account.");
System.out.println("2. Login.");
System.out.println("3. Exit.");
System.out.println("=================================");
int choice=sc.nextInt();
try {
switch (choice) {
case 1: {
System.out.println("Enter first name : ");
sc.nextLine();
String name = sc.nextLine();
System.out.println("Enter Age : ");
int age = sc.nextInt();
System.out.println("Create password : ");
String password=sc.next();
BankAccount acc = new BankAccount(name, age,password);
accounts.put(acc.getAccountNumber(),acc);
System.out.println("=================================");
System.out.println("Account created Successfully...\nYour unique account number is " + acc.getAccountNumber() + ".");
System.out.println("=================================");
saveAccs();
break;
}
case 2: {
System.out.println("Account number : ");
int accNo=sc.nextInt();
BankAccount acc = findAcc(accNo);
if (acc != null) {
System.out.println("Password : ");
String pass=sc.next();
if (acc.checkPass(pass)) {
System.out.println("=================================");
System.out.println("Welcome " + acc.getName().toUpperCase() + "...");
System.out.println("=================================");
int choice2 = 0;
while (choice2 != 10) {
System.out.println("=================================");
System.out.println("Press : ");
System.out.println("1. Deposit.");
System.out.println("2. Withdraw.");
System.out.println("3. Account Details.");
System.out.println("4. Online Transaction.");
System.out.println("5. Check Balance");
System.out.println("6. Change Password");
System.out.println("7. Transaction History");
System.out.println("8. Filter Transaction History");
System.out.println("10. Bank portal.");
System.out.println("11. Exit.");
System.out.println("=================================");
choice2 = sc.nextInt();
try {
switch (choice2) {
case 1: {
System.out.println("Amount : ");
acc.deposit(sc.nextDouble());
saveAccs();
System.out.println("Successfully Deposited.......");
break;
}
case 2: {
System.out.println("Amount : ");
acc.withdraw(sc.nextDouble());
saveAccs();
System.out.println("Transaction Successful.......");
break;
}
case 3: {
acc.display();
break;
}
case 4: {
System.out.println("Receiver's account number : ");
int recNo = sc.nextInt();
BankAccount rec = findAcc(recNo);
System.out.println("Amount : ");
double amount = sc.nextDouble();
if(rec==null){
System.out.println("### Receiver not found ###");
break;
}
acc.transfer(rec, amount);
saveAccs();
break;
}
case 5: {
System.out.println("=================================\nBank Balance : Rs." + acc.getBalance()+"\n=================================");
break;
}
case 6:{
System.out.println("Old password : ");
String oldPass=sc.next();
System.out.println("New password : ");
acc.changePass(oldPass,sc.next());
saveAccs();
break;
}
case 7:{
acc.showTransactions();
break;
}
case 8:{
System.out.println("Press 'D' for debit and 'C' for credit");
sc.nextLine();
String type=sc.nextLine();
acc.filterTransactionByType(type);
break;
}
case 10: {
break;
}
case 11: {
System.out.println("=================================");
System.out.println("Thanks! for visiting "+BankAccount.Bank_Name+"\nPunaah aagman ki pratiksha...");
System.out.println("=================================");
return;
}
default:
System.out.println("##### Enter valid operation! #####");
}
} catch (InsufficientBalanceException | IllegalArgumentException e) {
System.out.println(e.getMessage());
}
}
} else {
System.out.println("##### Incorrect password! #####");
}
}else{
System.out.println("### Account not found! ###");
}
break;
}
case 3: {
System.out.println("=================================");
System.out.println("Thanks! for visiting "+BankAccount.Bank_Name+"\nPunaah aagman ki pratiksha...");
System.out.println("=================================");
return;
}
default:
System.out.println("##### Enter valid operation! #####");
}
}catch (IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
}
static BankAccount findAcc(int accNo){
return accounts.get(accNo);
}
static void saveAccs(){
try(BufferedWriter bw=new BufferedWriter(new FileWriter(File_Name))){
for(BankAccount acc:accounts.values()){
bw.write(acc.fileToString());
bw.newLine();
}
}catch (IOException e){
System.out.println("##### Error in Saving files! #####");
}
}
static void loadAccs(){
try(BufferedReader br=new BufferedReader(new FileReader(File_Name))){
String line;
while((line=br.readLine())!=null){
BankAccount acc=BankAccount.stringToFile(line);
if(acc!=null){
accounts.put(acc.getAccountNumber(), acc);
}
}
} catch (IOException e) {
System.out.println("##### No previous data! #####");
}
}
static void syncCounter(){
int max = 20260000;
for(BankAccount acc:accounts.values()){
if(acc.getAccountNumber() > max){
max = acc.getAccountNumber();
}
}
BankAccount.setCounter(max + 1);
}
}