-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
451 lines (445 loc) · 11.6 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
451
/*HARI OM!!!
Account captcha
Author:- Shashwat Hiregoudar
Version: 1.0.0 2152
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include<conio.h>
#include <time.h>
#include <stdbool.h>
#define MIN_DEBIT_BALANCE 5000
#define MIN_BALANCE 1000
#define NAMELENGTH 15
#define TRUE 1
#define FALSE 0
#define ADMINPASSWORD 007
struct account_details
{
int id;
char fname[NAMELENGTH];
char lname[NAMELENGTH];
// struct name name;
int acc_num;
int pin;
float balance;
char account_type;
//struct dob dob;
};
struct node
{
struct account_details account;
struct node *next;
};
typedef struct node *NODEPTR;
NODEPTR load_all();
NODEPTR getnode();
int set_pin();
void signup();
void transfer(NODEPTR sender, NODEPTR receiver);
NODEPTR load_details(int accnum);
int generate_account_number();
void printdetails(NODEPTR p);
void write_to_file(struct account_details ac);
void deposit(NODEPTR p);
void withdraw(NODEPTR p);
void apply_debit_card(NODEPTR p);
NODEPTR search_account(NODEPTR base, int accountno, int accountpin);
void signin(NODEPTR list);
void do_transactions(NODEPTR p, NODEPTR list);
void admin_access();
int generate_debit_pin();
NODEPTR searchByAccountnum(NODEPTR list, int accnum);
void file_update(NODEPTR list);
int main()
{
NODEPTR p = NULL;
int choice, signinchoice;
printf("Welcome to Gringott's Bank\n");
printf("\nEnter 1 to sign up\n");
printf("\nEnter 2 to sign in\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
signup();
break;
case 2:
printf("\nEnter \n1-> to login as admin\n2->to login as customer\n");
scanf("%d", &signinchoice);
switch (signinchoice)
{
case 1:
admin_access();
break;
case 2:
p = load_all();
signin(p);
//To pass p it should have been loaded all the accounts here it is not done so segmentation fault occurs
break;
default:
printf("Invalid Entry \n");
exit(1);
}
break;
default:
printf("Invalid choice\n");
printf("Please enter valid choice\n");
}
printf("Now we will print the updaTED \n");
NODEPTR temp = p;
while (temp != NULL)
{
printdetails(temp);
temp = temp->next;
}
file_update(p);
return 0;
}
NODEPTR load_all()
{
FILE *fptr;
int s;
NODEPTR temp = NULL, base;
struct account_details accountt;
fptr = fopen("documentary.dat", "r");
if (temp == NULL)
{
fread(&accountt, sizeof(struct account_details), 1, fptr);
temp = getnode();
base = temp;
//temp->account.account_type=account.account_type;
temp->account.acc_num = accountt.acc_num;
temp->account.balance = accountt.balance;
strcpy(temp->account.fname, accountt.fname);
//temp->account.id=account.id;
strcpy(temp->account.lname, accountt.lname);
temp->account.pin = accountt.pin;
}
while (fread(&accountt, sizeof(accountt), 1, fptr))
{
temp->next = getnode();
temp = temp->next;
//temp->account.account_type=account.account_type;
temp->account.acc_num = accountt.acc_num;
temp->account.balance = accountt.balance;
strcpy(temp->account.fname, accountt.fname);
//temp->account.id=account.id;
strcpy(temp->account.lname, accountt.lname);
temp->account.pin = accountt.pin;
}
temp->next = NULL;
fclose(fptr);
return (base);
}
NODEPTR getnode()
{
NODEPTR p;
p = (NODEPTR)malloc(sizeof(struct node));
if (p == NULL)
{
printf("Insufficient Heap Memory Available\n");
exit(1);
}
p->next = NULL;
return p;
}
int generate_account_number()
{
int number;
//initialize random seed:
srand(time(NULL));
//generate a random number: ///Show to all
number = rand() % 1000 + 1;
printf("%d ", number); //Substitute to generate random numbers without repetition (uses time.h)
return (number);
}
int set_pin()
{
int teddy = 0;
int temp;
while (teddy == 0)
{
printf("\nEnter a pin\nIt will be set as your password\n");
if (scanf("%d", &temp) != 1)
{
printf("\nEnter a valid password\n");
teddy = 0;
}
teddy = 1;
}
return temp;
}
void signup()
{
NODEPTR p;
int x;
float bal;
p = getnode();
printf("\nEnter details\n");
printf("\nEnter your first name\n");
scanf("%s", p->account.fname);
printf("\nEnter your last name\n");
scanf("%s", p->account.lname);
p->account.pin = set_pin();
printf("To create this account we need a minimum balance of Rs%d/-\nPlease deposit initial Balance\n", MIN_BALANCE);
scanf("%f", &bal);
if (bal >= MIN_BALANCE)
p->account.balance = bal;
else
{
printf("Insufficient funds\n");
exit(1);
}
x = generate_account_number();
p->account.acc_num = x;
printf("Your account number is %d\n", x);
p->next = NULL;
printf("Your details entered are\n");
printdetails(p);
write_to_file(p->account);
}
void write_to_file(struct account_details ac)
{
FILE *fp = fopen("documentary.dat", "a");
fwrite(&ac, sizeof(ac), 1, fp);
printf("Successfully written\n");
fclose(fp);
}
void printdetails(NODEPTR p)
{
printf("Name:%s %s\n", p->account.fname, p->account.lname);
printf("Balance: %f\n", p->account.balance);
printf("Acc Num:%d\n", p->account.acc_num);
}
void deposit(NODEPTR p)
{
float amt;
printf("\nEnter the amount to be deposited\n");
scanf("%f", &amt);
p->account.balance += amt;
/*Displaying modified account details_display()*/
//q.front--;
}
void withdraw(NODEPTR p)
{
float amt;
printf("\nEnter amount to be withdrawn\n");
scanf("%f", &amt);
p->account.balance -= amt;
/*Display updated account*/
}
void apply_debit_card(NODEPTR p)
{
int dpin;
if (p->account.balance >= MIN_DEBIT_BALANCE)
{
printf("You are eligible to apply for debit card\n");
dpin = generate_account_number();
printf("Your debit card pin is %d\n", dpin);
printf("This is confidential\n");
printf("Please, do not disclose your pin to anyone\n");
}
else
{
printf("Since your balance is below Rs 5000, you are ineligible to apply for debit card\n");
}
}
int generate_debit_pin()
{
int flag;
flag = rand() % 1000 + 10;
return flag;
}
void pay_interest(NODEPTR p)
{
struct account_details account;
float interest;
interest = 6;
p->account.balance = p->account.balance + (p->account.balance * interest);
}
NODEPTR search_account(NODEPTR base, int accountno, int accountpin) ////good while administrator
{
NODEPTR temp = NULL;
temp = base;
while (temp != NULL)
{
if (temp->account.acc_num == accountno && temp->account.pin == accountpin)
return (temp);
else
temp = temp->next;
}
return NULL;
}
void signin(NODEPTR list)
{
int accountnumber;
int accountpin;
int ted = 0;
int ch;
NODEPTR p;
while (ted == 0)
{
printf("\nEnter your account number:");
scanf("%d", &accountnumber);
printf("\nEnter pin:");
scanf("%d", &accountpin);
p = search_account(list, accountnumber, accountpin);
if (p == NULL)
{
printf("Account Number or the password does not match\n");
ted = 0;
}
else
ted = 1;
}
do
{
do_transactions(p, list);
printdetails(p);
printf("\n Enter 1 to continue the transactions :-");
scanf("%d", &ch);
} while (ch == 1);
}
void do_transactions(NODEPTR p, NODEPTR list)
{
NODEPTR q;
int choice;
int bear = 0;
int option;
printf("\nEnter 1->withdraw\n\n");
printf("\nEnter 2->deposit\n\n");
printf("\nEnter 3->apply for debit\n\n");
printf("\nEnter 4->transfer amount\n\n");
scanf("%d", &choice);
int accnum;
switch (choice)
{
case 1:
withdraw(p);
break;
case 2:
deposit(p);
break;
case 3:
apply_debit_card(p);
break;
case 4:
while (bear == 0)
{
printf("\nEnter the account number to whom you want to send\n");
scanf("%d", &accnum);
q = searchByAccountnum(list, accnum);
if (q == NULL)
{
bear = 0;
continue;
}
printf("Did you mean by the person\n%s %s\nwhose account number is%d\nif yes press 1\nelse press anything other than 1\n", q->account.fname, q->account.lname, q->account.acc_num);
scanf("%d", &option);
if (option == 1)
break;
else
bear = 1;
printf("Please try again\n\nEnter a valid account number\n\n");
}
transfer(p, q);
break;
default:
printf("Invalid option entry\n");
}
}
NODEPTR searchByAccountnum(NODEPTR list, int accnum)
{
NODEPTR temp = NULL;
temp = list;
while (temp != NULL)
{
if (temp->account.acc_num == accnum)
return (temp);
else
temp = temp->next;
}
return NULL;
}
void transfer(NODEPTR sender, NODEPTR receiver)
{
int amt;
printf("\nEnter the amount you want to transfer\n\n");
scanf("%d", &amt);
sender->account.balance -= amt;
receiver->account.balance += amt;
}
void admin_access()
{
int entered_password;
int ted = 0;
NODEPTR p;
while (ted == 0)
{
printf("\nEnter the password\n\n");
scanf("%d", &entered_password);
if (entered_password != 007)
{
printf("Incorrect password\n");
printf("Please Try Again!\n");
ted = 0;
}
else
ted = 1;
}
p = load_all();
NODEPTR q;
q = p;
while (q != NULL)
{
printdetails(q);
q = q->next;
}
}
void file_update(NODEPTR list)
{
struct account_details ac;
FILE *fptr = fopen("documentary.dat", "w");
while (list != NULL)
{
fwrite(&ac, sizeof(ac), 1, fptr);
list = list->next;
}
fclose(fptr);
}
void update_details(NODEPTR p)
{
char localfname[20];
char locallname[20];
int localpin;
int flag;
struct account_details account;
printf("1-> To change only first name\n");
printf("2-> To change only last name\n");
printf("3->To change only pin\n");
scanf("%d", &flag);
printf("\nEnter account number whose details need to be changed");
search_account(p, account.acc_num, localpin);
switch (flag)
{
case 1:
printf("\nEnter name to be updated/modified\n");
gets(localfname);
strcpy(p->account.fname, localfname);
break;
case 2:
printf("\nEnter last name to be updated/modified\n");
gets(locallname);
strcpy(locallname, p->account.lname);
break;
case 3:
printf("\nEnter pin to be changed\n");
scanf("%d", &localpin);
p->account.pin = localpin;
break;
default:
printf("Invalid choice\n");
}
}