-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBank_Management_System.c
More file actions
500 lines (438 loc) · 17.5 KB
/
Bank_Management_System.c
File metadata and controls
500 lines (438 loc) · 17.5 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
// AVL Node
struct AVLNode {
char Name[50];
char CNIC[15];
char Gender;
char Type[20];
int Acc_Number;
int PIN;
int balance;
struct AVLNode* right;
struct AVLNode* left;
int height;
};
struct AVLNode* avl_root = NULL;
// AVL Functions
int max(int a, int b) {
return (a > b) ? a : b;
}
int height(struct AVLNode* node) {
if (node == NULL)
return 0;
int lh = height(node->left);
int rh = height(node->right);
return (lh > rh) ? lh + 1 : rh + 1;
}
struct AVLNode* left_rotation(struct AVLNode* node) {
struct AVLNode* temp1 = node->right;
node->right = temp1->left;
temp1->left = node;
node->height = max(height(node->left), height(node->right)) + 1;
temp1->height = max(height(temp1->left), height(temp1->right)) + 1;
return temp1;
}
struct AVLNode* right_rotation(struct AVLNode* node) {
struct AVLNode* temp1 = node->left;
node->left = temp1->right;
temp1->right = node;
node->height = max(height(node->left), height(node->right)) + 1;
temp1->height = max(height(temp1->left), height(temp1->right)) + 1;
return temp1;
}
struct AVLNode* right_left_rotation(struct AVLNode* node) {
node->right = right_rotation(node->right);
return left_rotation(node);
}
struct AVLNode* left_right_rotation(struct AVLNode* node) {
node->left = left_rotation(node->left);
return right_rotation(node);
}
struct AVLNode* insert_Acc(struct AVLNode* node, char* name, char* cnic, char gender, char* type, int account_no, int PIN, int balance) {
if (node == NULL) {
// Account number should be assigned only when creating a new node
node = (struct AVLNode*)malloc(sizeof(struct AVLNode));
if (node == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
strcpy(node->Name, name);
strcpy(node->CNIC, cnic);
node->Gender = gender;
strcpy(node->Type, type);
node->Acc_Number = account_no;
node->PIN = PIN;
node->balance = balance;
node->left = NULL;
node->right = NULL;
node->height = 0;
return node; // Return the new node
} else if (account_no < node->Acc_Number) {
node->left = insert_Acc(node->left, name, cnic, gender, type, account_no, PIN, balance);
if (height(node->left) - height(node->right) == 2) {
if (account_no < node->left->Acc_Number) {
node = right_rotation(node);
} else {
node = left_right_rotation(node);
}
}
} else if (account_no > node->Acc_Number) {
node->right = insert_Acc(node->right, name, cnic, gender, type, account_no, PIN, balance);
if (height(node->right) - height(node->left) == 2) {
if (account_no > node->right->Acc_Number) {
node = left_rotation(node);
} else {
node = right_left_rotation(node);
}
}
}
node->height = max(height(node->left), height(node->right)) + 1;
return node;
}
struct AVLNode* delete_Acc(struct AVLNode* root, int account_no) {
if (root == NULL) {
return root;
}
// Standard BST delete
if (account_no < root->Acc_Number) {
root->left = delete_Acc(root->left, account_no);
} else if (account_no > root->Acc_Number) {
root->right = delete_Acc(root->right, account_no);
} else {
// Node with only one child or no child
if (root->left == NULL) {
struct AVLNode* temp = root->right;
free(root);
return temp;
} else if (root->right == NULL) {
struct AVLNode* temp = root->left;
free(root);
return temp;
}
// Node with two children: Get the inorder successor (smallest in the right subtree)
struct AVLNode* temp = root->right;
while (temp->left != NULL) {
temp = temp->left;
}
// Copy the inorder successor's data to this node
strcpy(root->Name, temp->Name);
strcpy(root->CNIC, temp->CNIC);
root->Gender = temp->Gender;
strcpy(root->Type, temp->Type);
root->Acc_Number = temp->Acc_Number;
root->PIN = temp->PIN;
root->balance = temp->balance;
// Delete the inorder successor
root->right = delete_Acc(root->right, temp->Acc_Number);
}
// Update height of the current node
root->height = max(height(root->left), height(root->right)) + 1;
// Perform AVL balancing
int balance = height(root->left) - height(root->right);
// Left Heavy
if (balance > 1) {
if (height(root->left->left) >= height(root->left->right)) {
return right_rotation(root);
} else {
return left_right_rotation(root);
}
}
// Right Heavy
if (balance < -1) {
if (height(root->right->right) >= height(root->right->left)) {
return left_rotation(root);
} else {
return right_left_rotation(root);
}
}
return root;
}
// Function to find the maximum account number
int findMax(struct AVLNode* root) {
if (root == NULL)
return 0;
int res = root->Acc_Number;
int lres = findMax(root->left);
int rres = findMax(root->right);
if (lres > res)
res = lres;
if (rres > res)
res = rres;
return res;
}
int search_Acc(struct AVLNode* node, int account_no) {
while (node != NULL) {
if (account_no > node->Acc_Number) {
node = node->right;
} else if (account_no < node->Acc_Number) {
node = node->left;
} else {
return 1;
}
}
return 0;
}
int print_data(struct AVLNode* node, int account_no) {
while (node != NULL) {
if (account_no > node->Acc_Number) {
node = node->right;
} else if (account_no < node->Acc_Number) {
node = node->left;
} else {
printf(" ####################################################################################################################################\n");
printf(" \n");
printf(" ACC_Number NAME CNIC GENDER Type Balance\n");
printf(" \n");
printf(" ####################################################################################################################################\n");
printf(" \n");
printf(" %d %s %s %c %s %d\n",
node->Acc_Number, node->Name, node->CNIC, node->Gender, node->Type, node->balance);
return 1;
}
}
return 0;
}
void pre_order(struct AVLNode* node) {
if (node->left != NULL) {
pre_order(node->left);
}
printf(" %d %s %s %c %s %d\n",
node->Acc_Number, node->Name, node->CNIC, node->Gender, node->Type, node->balance);
if (node->right != NULL) {
pre_order(node->right);
}
}
struct AVLNode* deposit(struct AVLNode* node, int account_no, int PIN, int balance, struct tm *timeInfo) {
if (node == NULL) {
printf("Account not found. Deposit failed.\n");
return NULL; // Account not found
}
if (account_no < node->Acc_Number) {
node->left = deposit(node->left, account_no, PIN, balance, timeInfo);
} else if (account_no > node->Acc_Number) {
node->right = deposit(node->right, account_no, PIN, balance, timeInfo);
} else {
// Account found
if (PIN == node->PIN) {
node->balance += balance;
printf("Deposit successful. New balance: %d\n", node->balance);
// Print the time only when the deposit is successful
printf("\nDeposited on :\n");
printf("Date: %02d/%02d/%d\n", timeInfo->tm_mday, timeInfo->tm_mon + 1, timeInfo->tm_year + 1900);
printf("Time: %02d:%02d:%02d\n", timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
} else {
printf("Incorrect PIN. Deposit failed.\n");
return node;
}
}
// Update height of the current node
node->height = max(height(node->left), height(node->right)) + 1;
// Perform AVL balancing
int balanceFactor = height(node->left) - height(node->right);
// Left Heavy
if (balanceFactor > 1) {
if (account_no < node->left->Acc_Number) {
return right_rotation(node);
} else {
return left_right_rotation(node);
}
}
// Right Heavy
if (balanceFactor < -1) {
if (account_no > node->right->Acc_Number) {
return left_rotation(node);
} else {
return right_left_rotation(node);
}
}
return node;
}
struct AVLNode* withdraw(struct AVLNode* node, int account_no, int PIN, int balance,struct tm *timeInfo) {
if (node == NULL) {
printf("Account not found. Withdrawal failed.\n");
return NULL; // Account not found
}
if (account_no < node->Acc_Number) {
node->left = withdraw(node->left, account_no, PIN, balance,timeInfo);
} else if (account_no > node->Acc_Number) {
node->right = withdraw(node->right, account_no, PIN, balance,timeInfo);
} else {
// Account found
if (PIN == node->PIN) {
if (node->balance < balance) {
printf("Insufficient balance. Withdrawal failed.\n");
return node; // Insufficient balance
} else {
node->balance -= balance;
printf("Withdrawal successful. New balance: %d\n", node->balance);
printf("\nWithdrawl on :");
printf("\nDate: %02d/%02d/%d\n", timeInfo->tm_mday, timeInfo->tm_mon + 1, timeInfo->tm_year + 1900);
printf("\nTime: %02d:%02d:%02d\n", timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
}
} else {
printf("Incorrect PIN. Withdrawal failed.\n");
return node;
}
}
// Update height of the current node
node->height = max(height(node->left), height(node->right)) + 1;
int balanceFactor = height(node->left) - height(node->right);
if (balanceFactor > 1) {
if (account_no < node->left->Acc_Number) {
return right_rotation(node);
} else {
return left_right_rotation(node);
}
}
if (balanceFactor < -1) {
if (account_no > node->right->Acc_Number) {
return left_rotation(node);
} else {
return right_left_rotation(node);
}
}
return node;
}
void displayTitle() {
printf(" ____ _ __ __ _ _____ _ \n");
printf(" | _ \\ | | | \\/ | | | / ____| | | \n");
printf(" | |_) | __ _ _ __ | | __ | \\ / | __ _ _ __ __ _ __ _ ___ _ __ ___ ___ _ __ | |_ | (___ _ _ ___| |_ ___ _ __ ___ \n");
printf(" | _ < / _` | '_ \\| |/ / | |\\/| |/ _` | '_ \\ / _` |/ _` |/ _ \\ '_ ` _ \\ / _ \\ '_ \\| __| \\___ \\ | | / __| __/ _ \\ '_ ` _ \\ \n");
printf(" | |_) | (_| | | | | < | | | | (_| | | | | (_| | (_| | __/ | | | | | __/ | | | |_ ____) | |_| \\__ \\ || __/ | | | | |\n");
printf(" |____/ \\__,_|_| |_|_|\\_\\ |_| |_|\\__,_|_| |_|\\__,_|\\__, |\\___|_| |_| |_|\\___|_| |_|\\__| |_____/ \\__, |___/\\__\\___|_| |_| |_| \n");
printf(" __/ | __/ | \n");
printf(" |___/ |___/ \n");
printf(" \n");
}
int main() {
int option, loop = 1;
time_t currentTime;
struct tm *timeInfo;
time(¤tTime);
timeInfo = localtime(¤tTime);
while (loop) {
displayTitle();
printf(" \n");
printf("\t \t \t \t \t \t \t \t1- CREATE NEW ACCOUNT\n");
printf("\t \t \t \t \t \t \t \t2- DEPOSIT AMOUNT\n");
printf("\t \t \t \t \t \t \t \t3- WITHDRAW AMOUNT\n");
printf("\t \t \t \t \t \t \t \t4- SEARCH BY ACCOUNT NUMBER\n");
printf("\t \t \t \t \t \t \t \t5- PRINT ALL ACCOUNTS\n");
printf("\t \t \t \t \t \t \t \t6- DELETE ACCOUNT\n");
printf("\t \t \t \t \t \t \t \t7- EXIT\n");
printf(" \n");
printf("\t \t \t \t \t \t \t Enter your option: ");
scanf("%d", &option);
printf(" \n");
switch (option) {
case 1: {
char name[50], cnic[15], type[20],gender;
int account_no, PIN;
float balance; // Changed type to float
printf("Enter the following details for account creation:\n");
printf("Name: ");
scanf(" %[^\n]s", name);
printf("CNIC: ");
scanf(" %s", cnic);
printf("Gender (M/F/Others): ");
scanf(" %c", &gender);
printf("Account Type(Saving/Current): ");
scanf(" %[^\n]s", type);
printf("Initial Deposit: ");
scanf("%f", &balance);
account_no = findMax(avl_root) + 1;
printf("Enter a 4-digit PIN for the account: ");
scanf("%d", &PIN);
avl_root = insert_Acc(avl_root, name, cnic, gender, type, account_no, PIN, (int)balance);
printf("Account created successfully. Account Number: %d\n", account_no);
time_t t;
time(&t);
char entry[100];
printf(entry, sizeof(entry), "Account Created with an initial deposit of %.2f on %s", balance, ctime(&t));
break;
}
case 2: {
int account_no, PIN, deposit_amount;
printf("Enter Account Number: ");
scanf("%d", &account_no);
printf("Enter 4-digit PIN: ");
scanf("%d", &PIN);
printf("Enter the amount to deposit: ");
scanf("%d", &deposit_amount);
avl_root = deposit(avl_root, account_no, PIN, deposit_amount,timeInfo);
break;
}
case 3: {
int account_no, PIN, withdraw_amount;
printf("Enter Account Number: ");
scanf("%d", &account_no);
printf("Enter 4-digit PIN: ");
scanf("%d", &PIN);
printf("Enter the amount to withdraw: ");
scanf("%d", &withdraw_amount);
avl_root = withdraw(avl_root, account_no, PIN, withdraw_amount,timeInfo);
break;
}
case 4: {
int account_no;
printf("Enter Account Number: ");
scanf("%d", &account_no);
if (search_Acc(avl_root, account_no)) {
printf("Account found!\n");
print_data(avl_root, account_no);
}
else {
printf("Account not found.\n");
}
break;
}
case 5: {
if (avl_root != NULL) {
printf(" \n");
printf(" ####################################################################################################################################\n");
printf(" \n");
printf(" ACC_Number NAME CNIC GENDER Type Balance\n");
printf(" \n");
printf(" ####################################################################################################################################\n");
printf(" \n");
pre_order(avl_root);
}
else {
printf("No accounts found.\n");
}
break;
}
case 6: {
int account_no, PIN;
char cnic[15];
printf("Enter Account Number to delete: ");
scanf("%d", &account_no);
printf("CNIC: ");
scanf(" %s", cnic);
printf("Enter 4-digit PIN: ");
scanf("%d", &PIN);
if(avl_root==NULL)
{
printf("No records to delete");
}
else if (strcmp(cnic, avl_root->CNIC) == 0 && PIN == avl_root->PIN) {
avl_root = delete_Acc(avl_root, account_no);
printf("\nAccount deleted successfully");
}
break;
}
case 7: {
loop = 0;
break;
}
default: {
printf("Invalid option. Please enter a valid option.\n");
break;
}
}
}
return 0;
}