-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
450 lines (434 loc) · 14.2 KB
/
main.c
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
#define NRM "\x1B[0m"
#define BOLD "\x1B[1m"
#define BLK "\x1B[30m"
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
struct details
{
long int acc_no;
char name[100];
char passwd[100];
char email[100];
};
struct trans
{
int trno;//Personal transaction number
char tstr[100];//timesting
char desc[200];//Description
float tramt;//transaction amount
float bal;//balance after transaction
};
void cur_time(char *ts)
{
struct tm* ptr;
time_t t = time(NULL);
ptr = gmtime(&t);
strftime(ts,100,"%d-%m-%Y-%H:%M:%S",ptr);
// printf("%s\n",ts);
}
float float_rand(float min, float max)
{
srand(time(0));
rand(); // Empty call to remove related values.
float scale = rand() / (float) RAND_MAX; /* [0, 1.0] */
return min + scale * ( max - min );
}
void slep(float sec)
{
sec*=1000;
#ifdef _WIN32
Sleep(sec);
#else
usleep(sec*1000);
#endif
}
int gettrans(char uname[], struct trans *tar)
{
FILE *fptr;
char fname[] = "";
strcat(fname,uname);
strcat(fname,"_trs.csv");
fptr = fopen(fname, "r");
fseek(fptr, -3, SEEK_END);
int ctr,ltr; //Current, Last transcation number
fscanf(fptr,"%d",<r);
rewind(fptr);
fscanf(fptr,"%d",&ctr);
while(ctr < ltr)
{
tar[ctr-1].trno = ctr;
fscanf(fptr, ",%[^,],%[^,],%f,%f\n", tar[ctr-1].tstr, tar[ctr-1].desc, &tar[ctr-1].tramt, &tar[ctr-1].bal);
fscanf(fptr,"%d",&ctr);
}
fclose(fptr);
return ltr-1;
}
float balance(char uname[])
{
struct trans tar[100];
int n;
n = gettrans(uname, tar);
return tar[n-1].bal;
}
int withdraw(char uname[], float amt, char desc[])
{
float bal = balance(uname);
if (bal<amt)
{
printf(RED"Insufficient balance in your account!\n"NRM);
return -1;
}
FILE *fptr;
char fname[50] = "";
strcat(fname,uname);
strcat(fname,"_trs.csv");
fptr = fopen(fname, "a+");
fseek(fptr, -3, SEEK_END);
int ltr; //Current, Last transcation number
fscanf(fptr,"%d",<r);
char dstr[50]="";
cur_time(dstr);
fprintf(fptr,",%s,%s,%.2f,%.2f\n%03d",dstr,desc,-1*amt,bal-amt,ltr+1);
fclose(fptr);
return 0;
}
int deposit(char uname[], float amt, char desc[])
{
float bal = balance(uname);
FILE *fptr;
char fname[50] = "";
strcat(fname,uname);
strcat(fname,"_trs.csv");
fptr = fopen(fname, "a+");
fseek(fptr, -3, SEEK_END);
int ltr; //Current, Last transcation number
fscanf(fptr,"%d",<r);
fseek(fptr,0,SEEK_END);
char dstr[100]="";
cur_time(dstr);
fprintf(fptr,",%s,%s,%.2f,%.2f\n%03d",dstr,desc,amt,bal+amt,ltr+1);
fclose(fptr);
return 0;
}
int logchk(char *usr, char *pass, struct details *det)
{
FILE *fptr;
long int acc_no;
fptr = fopen("acc_data.csv","r");
fseek(fptr, -7, SEEK_END);
fscanf(fptr,"%ld", &acc_no);//Last non-existent account number
long int a;//Current account number
char u[100],e[100],p[100];
fseek(fptr, 0, SEEK_SET);
fscanf(fptr,"%ld",&a);
while (a!=acc_no)
{
fscanf(fptr, ",%[^,],%[^,],%s", u, e, p);
if (!strcmp(usr,u))
{
det->acc_no = a;
if (!strcmp(pass,p))
{
strcpy(det->name,u);
strcpy(det->email,e);
strcpy(det->passwd,p);
return 0;//Correct password
}
else
return 1;//Wrong password
}
fscanf(fptr,"%ld",&a);
};
fclose(fptr);
return -1;//Username not found
}
int login(struct details *det)
{ //fn1
int i1=0;
char usrn[100],pass[100];
for (;i1<3;i1++) //Three chances for wrong password.
{
printf("\nEnter username: ");
printf(YEL);
scanf("%s",usrn);
printf(NRM);
printf("\nEnter password: ");
printf(BLK);
scanf("%s",pass);
printf(NRM);
int status;
status = logchk(usrn, pass, det);
if (status == 0){//Verifying step
return 0;
}
else if (status == 1)
printf(RED"\nWrong password!\n"NRM);
else
{
printf(RED"Account with name '%s' doesn't exist. Sign-Up to create one!\n"NRM,usrn);
return -1;
}
}
printf(RED"Too many invalid attempts. Try again later!\n"NRM);
return -1;
}
void sign_up ()
{
struct details u1;
FILE *fptr,*fptr2;
fptr = fopen("acc_data.csv","a+");
fseek(fptr, -7, SEEK_END); //Read next account number
fscanf(fptr,"%ld", &u1.acc_no);
printf("Enter your name: ");
printf(YEL);
scanf("%s",u1.name);
printf(NRM);
printf("Enter your email address: ");
printf(YEL);
scanf("%s",u1.email);
printf(NRM);
printf("Enter password: ");
printf(BLK);
scanf("%s",u1.passwd);
printf(NRM);
int stat;
stat = logchk(u1.name, u1.passwd, &u1);
if (!(stat==-1))
{
printf(RED"Username '%s' already exists! Kindly login to access.\n"NRM,u1.name);
fclose(fptr);
return;
}
char dstr[100]="";
cur_time(dstr);
fprintf(fptr, ",%s,%s,%s\n%ld", u1.name, u1.email, u1.passwd, u1.acc_no+1);//Append data to file.
printf(GRN"Account created successfully!\n"NRM);
printf("Your account number is: ");
printf(YEL"%ld\n"NRM,u1.acc_no);
char fname[110]="";
strcat(fname,u1.name);
strcat(fname,"_trs.csv");
fptr2 = fopen(fname,"a+");
fprintf(fptr2, "001,%s,Initial Deposit,10000,10000\n002",dstr);//TRID,date_string,Desc,Transaction_amt,Balance
fclose(fptr);
fclose(fptr2);
}
int main()
{
char border[] = "===================================================================================\n";
printf(MAG"%s"NRM,border);
printf(MAG BOLD"======================== Welcome to N.P. Banking Services =========================\n" NRM);
printf(MAG"%s"NRM,border);
int flag=1,log=0;
struct details det;
while (flag)
{
printf(CYN"\n1. Log-In\n2. Sign-Up\n3. Exit\nEnter your choice(integer only): " NRM);
int ch1;
printf(YEL);
scanf("%d",&ch1);
printf(NRM);
switch (ch1)
{
case 1:
printf(BOLD MAG"================================ Log-In ===========================================\n"NRM);
int resp1;
resp1 = login(&det);
if (resp1==0)
{
log = 1;
printf(GRN"Successfully logged in!\n"NRM);
printf(BOLD MAG"=============================== Welcome %s ======================================\n"NRM,det.name);
}
break;
case 2:
printf(BOLD MAG"================================ Sign-Up ===========================================\n"NRM);
sign_up();//
break;
default:
flag=0;
printf(BOLD BLU"\nThank you for using our service. Good Day!"NRM);
break;
}
while (log==1)
{
printf(CYN"\n1. Check your account balance\n2. View your transactions\n3. Money Transfer\n4. Crypto Invest\n5. Exit\nEnter your choice(integer only): "NRM);
int ch2=0;
int arsz; // Array Size
struct trans tar[100];
float amount; // Transaction amount for money transfer
printf(YEL);
scanf("%d",&ch2);
printf(NRM);
char line[] = "---------------------------------------------------------------------------------------\n";
switch(ch2)
{
case 1:
printf(GRN"\nYour account balance is:");
printf(YEL" %.2f Rupees Only\n"NRM,balance(det.name));
break;
case 2:
arsz = gettrans(det.name, tar);
int it=0;
printf("%s",line);
printf("|TrNo.| Date-Time-UTC | Description | Tr.Amt | Balance |\n");
printf("%s",line);
for (it=0;it<arsz;it++)
{
printf("|%4d | %19s | %30s | %10.2f | %10.2f|\n",tar[it].trno, tar[it].tstr, tar[it].desc, tar[it].tramt, tar[it].bal);
slep(0.01);
}
printf("%s",line);
break;
case 3:
printf(BOLD MAG"===================== MONEY TRANSFER ====================\n"NRM);
struct details det2;
long int accno2;
printf("Enter account number to which amount must be transferred: ");
printf(YEL);
scanf("%ld",&accno2);
printf(NRM);
printf("Enter name of account to which amount must be transferred: ");
printf(YEL);
scanf("%s",det2.name);
printf(NRM);
// Check if account exists
int resp2 = logchk(det2.name, "ABC", &det2);
if (resp2==-1)
{
printf(RED"Account with name '%s' doesn't exist.\n"NRM,det2.name);
break;
}
else if (resp2==1 && (accno2 != det2.acc_no))
{
printf(RED"Receiver's account name and account number don't match.\n"NRM);
break;
}
else if (resp2==1 && (accno2 == det.acc_no))
{
printf(YEL"You cannot transfer money to your own account.\n"NRM);
break;
}
printf("Enter amount to be transferred: ");
printf(YEL);
scanf("%f",&amount);
printf(NRM);
printf("Enter password to confirm: ");
printf(BLK);
char passwd[50];
scanf("%s",passwd);
printf(NRM);
if (strcmp(passwd, det.passwd)!=0)
{
printf(RED"Wrong password\n"NRM);
printf(RED"Transaction failed!\n"NRM);
break;
}
char str1[100]="Transfer to account ", buff1[20];
sprintf(buff1, "%ld", accno2);
strcat(str1,buff1);
char str2[100]="Transfer from account ", buff2[20];
sprintf(buff2, "%ld", det.acc_no);
strcat(str2,buff2);
if (withdraw(det.name, amount, str1)==0 && deposit(det2.name, amount, str2)==0)
{
printf(GRN"\nMoney transferred successfully!\n"NRM);
}
else
{
printf(RED"\nTransaction failed!\n"NRM);
}
break;
case 4:
printf(BOLD MAG"===================== CRYPTO INVEST ====================\n"NRM);
printf("Enter amount to be invested: ");
printf(YEL);
scanf("%f",&amount);
printf(NRM);
printf(CYN"Choose in which currency you want to invest: \n1. Bitcoin \n2. Ethereum \n3. Litecoin \n4. Ripple \n5. Back\nEnter your choice(integer only): "NRM);
printf(YEL);
int ch3;
scanf("%d",&ch3);
printf(NRM);
char desc[50]="Investment in ";
int resp3=1;
switch (ch3)
{
case 1:
strcat(desc,"Bitcoin");
break;
case 2:
strcat(desc,"Ethereum");
break;
case 3:
strcat(desc,"Litecoin");
break;
case 4:
strcat(desc,"Ripple");
break;
case 5:
resp3=0;
break;
default:
resp3=0;
printf(RED"Invalid choice!\n"NRM);
break;
}
if (resp3==0)
{
break;
}
printf("%s",line);
if (withdraw(det.name, amount, desc)==0)
{
printf(GRN"Investment successful!\n"NRM);
}
else
{
printf(RED"Investment failed!\n"NRM);
break;
}
slep(2);
float rate = float_rand(-15,15);
if (rate<0)
{
printf(BOLD YEL"\n%s has plummeted by %.2f%%!\n\n"NRM,desc,rate);
}
else
{
printf(BOLD GRN"\n%s has soared by %.2f%%!\n\n"NRM,desc,rate);
}
amount = amount + (amount*rate/100);
if (deposit(det.name, amount, "Investment Returns")==0)
{
printf(GRN"Returns deposited successfully!\n"NRM);
}
else
{
printf(RED"Depositing failed!\n"NRM);
}
break;
default:
log = 0;
printf(YEL"\nLogged out!\n"NRM);
break;
}
printf("%s",border);
}
}
return 0;
}