-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
546 lines (521 loc) · 14.5 KB
/
main.c
File metadata and controls
546 lines (521 loc) · 14.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
// Global Declarations
const int acc_no_len = 11;
char AccNumPreFix[8] = "01520521";
struct user usr;
FILE *fPtr;
char filename[50] = "Accounts/", hfilename[50] = "Accounts/";
// User Data Structure
struct user
{
char acc_no[12];
char name[50];
char phone_no[50];
char password[50];
char pin[10];
float balance;
};
// Function Definitions
void welcome_scr();
void displayHeader();
void menu();
int registerUser();
int loginUser();
void handleUser();
void depositMoney();
void withdrawMoney();
void transfer_money(int isClosure);
void account_details();
void transaction_details();
int logout();
void account_closure();
void printDivider(int n);
int verifyPin();
void generateAccountNumber();
// Main function
int main()
{
welcome_scr();
return 0;
}
// Welcome screen
void welcome_scr()
{
int choice;
Label_1:
system("cls");
displayHeader();
printf("\t 1. Register a new account\n");
printf("\t 2. Login to an existing account\n\n");
printf("\t Enter your choice: ");
scanf("%d", &choice);
if (choice == 1)
{
if (registerUser() == 0)
goto Label_1;
}
else if (choice == 2)
{
if (loginUser() == 0)
goto Label_1;
}
else
{
system("cls");
printf("\t INVALID CHOICE!\nPress any key to return....");
getch();
goto Label_1;
}
handleUser();
}
void displayHeader()
{
printf("\t\t\033[1;36m-----------------------\n");
printf("\t\t| Welcome to DigiBank |\n");
printf("\t\t-----------------------\033[0;m\n\n");
}
// Menu for user
void menu()
{
displayHeader();
printf("\t\033[1;33m --- Main Menu ---\033[0;m\n");
printf("\t 1. Deposit Money \n");
printf("\t 2. Withdraw Money \n");
printf("\t 3. Transfer Money \n");
printf("\t 4. Account Status \n");
printf("\t 5. Transaction details \n");
printf("\t 6. Account Closure \n");
printf("\t 7. Logout \n");
}
// Register new user account
int registerUser()
{
system("cls");
printf("\t Enter your name:\t");
fflush(stdin);
scanf("%[^\n]", usr.name);
printf("\t Enter your phone number:\t");
scanf("%s", usr.phone_no);
printf("\t Enter your password:\t");
scanf("%s", usr.password);
printf("\t Enter your transaction pin:\t");
scanf("%s", usr.pin);
usr.balance = 0;
generateAccountNumber();
strcat(filename, usr.phone_no);
fPtr = fopen(strcat(filename, ".dat"), "w");
fwrite(&usr, sizeof(struct user), 1, fPtr);
int done = 0; // holds info of whether the acc creation is successfull or not
fclose(fPtr);
if (fwrite != 0)
{
printf("\n\t \033[1;32mACCOUNT SUCCESSFULLY CREATED.\033[0;m\n");
printf("\t Your Account Number is: %s\n\n", usr.acc_no);
strcat(hfilename, usr.phone_no);
fPtr = fopen(strcat(hfilename, "h.txt"), "a");
done = 1;
fclose(fPtr);
}
else
{
printf("\t \033[1;31m SOMETHING WENT WRONG!!!\033[0;m\n\n");
}
printf("\t Press any key to continue....\t");
getch();
return done;
}
// Logs in an existing user
int loginUser()
{
char phone[50], pword[50];
system("cls");
printf("\t Phone Number:\t");
scanf("%s", &phone);
printf("\t Password:\t");
scanf("%s", pword);
strcat(filename, phone);
fPtr = fopen(strcat(filename, ".dat"), "r");
if (fPtr == NULL)
{
printf("\t \033[1;33mACCOUNT NOT REGISTERED!\033[0;m\n\n");
strcpy(filename, "Accounts/");
fclose(fPtr);
}
else
{
fread(&usr, sizeof(struct user), 1, fPtr);
fclose(fPtr);
if (!strcmp(usr.password, pword))
{
// Move to home screen
strcat(hfilename, usr.phone_no);
strcat(hfilename, "h.txt");
printf("\t Password Matched!\n\n"); // Password hashing
return 1;
}
else
{
printf("\t \033[1;31mINVALID PASSWORD!!!\033[0;m\n\n");
strcpy(filename, "Accounts/");
}
}
printf("\t Press any key to continue....");
getch();
return 0;
}
// Handles the logged in user
void handleUser()
{
int choice;
choice = 0;
goto Label_2;
Label_2:
while (1)
{
system("cls");
menu();
printf("\n\t Enter your choice: ");
scanf("%d", &choice);
system("cls");
switch (choice)
{
case 1:
depositMoney();
break;
case 2:
withdrawMoney();
break;
case 3:
transfer_money(0);
break;
case 4:
account_details();
break;
case 5:
transaction_details();
break;
case 6:
account_closure();
break;
case 7:
if (logout())
welcome_scr();
break;
default:
printf("\t \033[1;31mINVALID CHOICE!!!\033[0;m!");
getch();
break;
}
}
}
// Deposits money
void depositMoney()
{
system("cls");
time_t tm;
time(&tm);
printf("\t \033[1;33mDEPOSIT MONEY\033[0;m\n");
printDivider(50);
float dip_amt;
printf("\n\t Enter the amount to be deposited:\t");
scanf("%f", &dip_amt);
if (verifyPin())
{
usr.balance += dip_amt;
printf("\n\t \033[1;32mMONEY DEPOSITED.\033[0;m\n");
printf("\t Current balance: %.2f\n", usr.balance);
// saving updated data
fPtr = fopen(filename, "w");
fwrite(&usr, sizeof(struct user), 1, fPtr);
fclose(fPtr);
// saving history
fPtr = fopen(hfilename, "a");
fprintf(fPtr, "\t Rs %.2f has been deposited to your account \n", dip_amt);
fprintf(fPtr, "\t Date /time of transaction %s\n", ctime(&tm));
fclose(fPtr);
}
else
{
printf("\n\t \033[1;31mINCORRECT TRANSACTION PIN!\033[0;m\n");
}
printf("\n\t Press any key to continue..........\n");
getch();
}
// Withdraws money
void withdrawMoney()
{
system("cls");
time_t tm;
time(&tm);
printf("\t \033[1;33mWITHDRAW MONEY\033[0;m\n");
printDivider(50);
float with_amt;
printf("\n\t Enter the amount to withdraw:\t");
scanf("%f", &with_amt);
if (verifyPin())
{
if (usr.balance < with_amt)
{
printf("\n\t \033[1;31mINSUFFICIENT BALANCE!\033[0;m\n");
}
else
{
usr.balance -= with_amt;
printf("\n\t \033[1;32mMONEY SUCCESSFULLY WITHDRAWN\033[0;m\n");
printf("\t Current balance: Rs %.2f\n", usr.balance);
fPtr = fopen(filename, "w");
fwrite(&usr, sizeof(struct user), 1, fPtr);
fclose(fPtr);
fPtr = fopen(hfilename, "a");
fprintf(fPtr, "\t Rs %.2f has been withdrawn from your account \n", with_amt);
fprintf(fPtr, "\t Date /time of transaction %s\n", ctime(&tm));
fclose(fPtr);
}
}
else
{
printf("\n\t \033[1;31mINCORRECT TRANSACTION PIN!\033[0;m\n");
}
printf("\n\t Press any key to continue..........\n");
getch();
}
// Transfers money
void transfer_money(int isClosure)
{
time_t tm;
if (!isClosure)
{
system("cls");
time(&tm);
printf("\t \033[1;33mTRANSFER MONEY\033[0;m\n");
printDivider(50);
}
char rec_phn[50];
float trans_amt;
printf("\n\t Enter the phone number of receiver: ");
scanf("%s", &rec_phn);
char rec_tdata[50] = "Accounts/";
char rec_data[50] = "Accounts/";
strcat(rec_tdata, rec_phn);
strcat(rec_data, rec_phn);
strcat(rec_data, ".dat");
strcat(rec_tdata, "h.txt");
if (!isClosure)
{
printf("\t Enter the amount to be transferred: ");
scanf("%f", &trans_amt);
}
else
{
trans_amt = usr.balance;
}
if (verifyPin())
{
fPtr = fopen(rec_data, "r");
if (fPtr == NULL)
{
printf("\n\t \033[1;31mRECEIVER'S ACCOUNT NUMBER NOT REGISTERED!\033[0;m\n");
fclose(fPtr);
}
else
{
struct user rec;
fread(&rec, sizeof(struct user), 1, fPtr);
fclose(fPtr);
printf("\n\t Transferring Rs %.2f to %s\n", trans_amt, rec.name);
printf("\t press 'y' to confirm and 'n' to cancel:\t");
char ch = getch();
if (ch == 'y')
{
if (usr.balance < trans_amt)
{
printf("\n\t \033[1;31mINSUFFICIENT BALANCE!\033[0;m\n");
fclose(fPtr);
}
else
{
usr.balance -= trans_amt;
printf("\n\n\t \033[1;32mMONEY TRANSFERRED SUCCESSFULLY\033[0;m\n");
if (!isClosure)
printf("\t Current balance: Rs %.2f\n", usr.balance);
rec.balance += trans_amt;
fPtr = fopen(rec_data, "w");
fwrite(&rec, sizeof(struct user), 1, fPtr);
fclose(fPtr);
fPtr = fopen(rec_tdata, "a");
fprintf(fPtr, "\t Rs %.2f has been transferred from %s \n", trans_amt, usr.name);
fprintf(fPtr, "\t Date /time of transaction %s\n", ctime(&tm));
fclose(fPtr);
fPtr = fopen(filename, "w");
fwrite(&usr, sizeof(struct user), 1, fPtr);
fclose(fPtr);
fPtr = fopen(hfilename, "a");
fprintf(fPtr, "\t Rs %.2f has been transferred to %s \n", trans_amt, rec.name);
fprintf(fPtr, "\t Date /time of transaction %s\n", ctime(&tm));
fclose(fPtr);
}
}
else
{
printf("\n\t \033[1;31mTRANSACTION FAILED\033[0;m\n");
}
}
}
else
{
printf("\n\t \033[1;31mINCORRECT TRANSACTION PIN!\033[0;m\n");
}
printf("\n\t Press any key to continue..........\n");
getch();
}
// Logged in user's details
void account_details()
{
system("cls");
if (verifyPin())
{
system("cls");
printf("\n\t \033[1;33mACCOUNT DETAILS\033[0;m\n");
printDivider(50);
printf("\t Account Holder: %s\n", usr.name);
printf("\t Account Number: %s\n", usr.acc_no);
printf("\t Phone Number: %s\n", usr.phone_no);
printf("\t Current Balance: Rs %.2f\n", usr.balance);
printDivider(50);
}
else
{
printf("\n\t \033[1;31mINCORRECT TRANSACTION PIN!\033[0;m\n");
}
printf("\n\t Press any key to continue..........\n");
getch();
}
// Logged in user's history
void transaction_details()
{
system("cls");
if (verifyPin())
{
system("cls");
fPtr = fopen(hfilename, "r");
char c = fgetc(fPtr);
if (c == EOF)
printf("\n\t \033[1;36mNO RECENT TRANSACTIONS!\033[0;m\n");
else
{
printf("\n\t \033[1;33mTRANSACTION DETAILS\033[0;m\n");
printDivider(55);
while (c != EOF)
{
printf("%c", c);
c = fgetc(fPtr);
}
printDivider(55);
}
fclose(fPtr);
}
else
{
printf("\n\t \033[1;31mINCORRECT TRANSACTION PIN!\033[0;m\n");
}
printf("\n\t Press any key to continue..........\n");
getch();
}
// Logout current user
int logout()
{
system("cls");
printf("\n\t Are you sure you want to log out?\n");
char cf;
printf("\t Press 'y' to confirm and 'n' to cancel:\t");
cf = getch();
if (cf == 'y')
{
printf("\n\n\t \033[1;36mTHANK YOU FOR USING OUR BANK MANAGEMENT SYSTEM\033[0;m\n\n");
printf("\t Press any key to continue...\t");
getch();
strcpy(filename, "Accounts/");
strcpy(hfilename, "Accounts/");
return 1;
}
return 0;
}
// Closes The Account Permanently
void account_closure()
{
char ch;
system("cls");
printf("\n\t Are you sure you want to close this account?");
printf("\n\t Press 'y' to confirm:\n");
ch = getch();
if (ch == 'y')
{
if (verifyPin())
{
if (usr.balance > 0)
{
printf("\n\t You have Rs %.2f in your account\n", usr.balance);
printf("\n\t Click '1' to withdraw and '2' to transfer:\t");
ch = getch();
if (ch == '1')
{
printf("\n\t \033[1;32mRs %.2f has been withdrawn from your account\033[0;m\n", usr.balance);
usr.balance = 0;
printf("\n\t Press any key to continue...\t");
getch();
}
else if (ch == '2')
{
transfer_money(1);
}
}
remove(filename);
remove(hfilename);
strcpy(filename, "Accounts/");
strcpy(hfilename, "Accounts/");
welcome_scr();
}
else
{
printf("\n\t \033[1;31mINCORRECT TRANSACTION PIN!\033[0;m\n");
getch();
}
}
}
// Provides a horizontal line of n width
void printDivider(int n)
{
printf("\t ");
while (n != 0)
{
printf("-");
n--;
}
printf("\n");
}
// Verifies Transaction Pin before each transaction
int verifyPin()
{
char gotPin[10];
printf("\t Enter Your Transaction Pin:\t");
scanf("%s", gotPin);
if (!strcmp(gotPin, usr.pin))
return 1;
else
return 0;
}
// Generates a random account number with some constant prefix
void generateAccountNumber()
{
for (int i = 0; i < 8; i++)
usr.acc_no[i] = AccNumPreFix[i];
for (int i = 8; i < acc_no_len; i++)
{
usr.acc_no[i] = '0' + rand() % 10;
}
usr.acc_no[acc_no_len] = '\0'; // Null-terminate the string
}