-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanagement.c
More file actions
958 lines (742 loc) · 21.3 KB
/
management.c
File metadata and controls
958 lines (742 loc) · 21.3 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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
#pragma warning (disable : 4996) // program coded in Visual Studio 2015
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int authenticate(void);
void printMessage(void);
int viewAccount(void);
int addAccount(void);
int editAccount(void);
int deleteAccount(void);
int addBalance(void);
int withdrawMoney(void);
double totalBalance(void);
double maxBalance(void);
double minBalance(void);
double avgBalance(void);
typedef struct {
int serial;
char name[46];
int account_number;
char type[15];
double balance;
} Account;
typedef struct {
long long id;
char password[50];
} User;
int main(void)
{
int choice;
while (!authenticate())
continue;
printMessage();
scanf("%d", &choice);
while (choice != 11)
{
switch (choice)
{
case 1:
viewAccount();
break;
case 2 :
addAccount();
break;
case 3:
editAccount();
break;
case 4:
deleteAccount();
break;
case 5:
addBalance();
break;
case 6:
withdrawMoney();
break;
case 7:
totalBalance();
break;
case 8:
maxBalance();
break;
case 9:
minBalance();
break;
case 10:
avgBalance();
break;
default:
printf("Wrong choice entered.\n\n");
}
printMessage();
scanf("%d", &choice);
}
printf("\n\nThank you!\n\n");
return 0;
}
int authenticate(void)
{
int userCount; // the number of employees who can access the program
long long idNo; // the id number of the user who will access the program
char pass[50]; // the password entered by the user
int found = 0;
int i, j;
FILE *userFile = fopen("eligible.txt", "r");
if (userFile == NULL)
{
printf("Cannot grant access to the customer accounts now.\n");
return 0;
}
fscanf(userFile, "%d", &userCount);
User *user = malloc(sizeof(User) * userCount);
for (i = 0; i < userCount; i++)
fscanf(userFile, "%lld %s", &user[i].id, user[i].password);
printf("Enter your ID number: ");
scanf("%lld", &idNo);
for (i = 0; i < userCount; i++)
{
if (user[i].id == idNo)
{
j = i;
found = 1;
}
}
if (!found)
{
printf("You do not have the access to the customer accounts.\n\n");
fclose(userFile);
free(user);
return 0;
}
printf("Enter your password: ");
scanf("%s", pass);
while (strcmp(pass, user[j].password))
{
printf("Wrong password entered.\n");
printf("\nEnter your password: ");
scanf("%s", pass);
}
printf("\nWelcome!\n\n");
//clean up
fclose(userFile);
free(user);
return 1;
}
void printMessage(void)
{
printf("-------------------------------------------------------------------------\n\n");
printf("Enter the number that designates the operation");
printf(" you would like to perform.\n\n");
printf("1. View account.\n");
printf("2. Add new account.\n");
printf("3. Edit existing account.\n");
printf("4. Delete an account.\n");
printf("5. Add money to an account.\n");
printf("6. Withdraw money from an account.\n");
printf("7. Find total balance of all accounts.\n");
printf("8. Display max balance holder.\n");
printf("9. Display min balance holder.\n");
printf("10. Display average of balances.\n");
printf("11. Exit.\n\n");
printf("Now, enter choice: ");
}
int viewAccount(void)
{
int accCount; // number of accounts
int show; // the account number of the account to be shown
int i;
int found = 0;
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("Error!! File not found.");
return -1;
}
fscanf(originalFile, "%*[A-Z a-z:] %d %*[^\n] %*[^\n]", &accCount);
Account *accounts = malloc(sizeof(Account) * accCount);
printf("Enter account number of the account you want to view: ");
scanf("%d", &show);
for (i = 0; i < accCount; i++)
{
fscanf(originalFile, "%d %45[A-Z a-z.] %d %s %lf", &accounts[i].serial, accounts[i].name,
&accounts[i].account_number, accounts[i].type, &accounts[i].balance);
if (accounts[i].account_number == show)
{
found = 1;
break;
}
}
if (found)
{
printf("\nAccount holder: %s\n", accounts[i].name);
printf("Account number: %d\n", accounts[i].account_number);
printf("Account type: %s\n", accounts[i].type);
printf("Account balance: $%.2f\n\n", accounts[i].balance);
}
else
{
printf("\nNo such account exists.\n\n");
}
// clean up
fclose(originalFile);
free(accounts);
return 1;
}
int addAccount(void)
{
int accCount; // number of accounts
char accCount_str[25]; // will read "Number of accounts: "
char tbl_head_1[200]; // will contain the table headers
char tbl_head_2[200]; // will contain the dashes
int i;
char response; // to check if the account is being created in the way wanted
int field; // the field to be edited
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("Error!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%[A-Z a-z:] %d %[^\n] %[^\n]", accCount_str, &accCount, tbl_head_1, tbl_head_2);
Account *accounts = malloc(sizeof(Account) * (accCount + 1));
for (i = 0; i < accCount; i++)
fscanf(originalFile, "%d %45[A-Z a-z.] %d %s %lf", &accounts[i].serial, accounts[i].name,
&accounts[i].account_number, accounts[i].type, &accounts[i].balance);
printf("\nEnter new account holder's name: ");
scanf("%*c %44[A-Z a-z.]", accounts[i].name);
printf("Enter the account type: ");
scanf("%s", accounts[i].type);
printf("Enter balance: ");
scanf("%lf", &accounts[i].balance);
++accCount;
accounts[i].account_number = accounts[i - 1].account_number + 1;
accounts[i].serial = accCount;
while(1)
{
printf("\nThe account is being created in this way-\n");
printf("Account name: %s\n", accounts[i].name);
printf("Account number: %d\n", accounts[i].account_number);
printf("Account type: %s\n", accounts[i].type);
printf("Account balance: $%.2f\n\n", accounts[i].balance);
printf("Would you like to edit any field? ( y / n ): ");
scanf(" %c", &response);
if (response != 'y')
break;
printf("\nEnter the number designating the field you would like to edit.\n\n");
printf("1. Name\n");
printf("2. Account type\n");
printf("3. Balance\n\n");
printf("Now, enter choice: ");
scanf("%d", &field);
switch (field)
{
case 1:
printf("\nEnter new name: ");
scanf("%*c %44[^\n]", accounts[i].name);
break;
case 2:
printf("\nEnter new account type: ");
scanf("%*c %[^\n]", accounts[i].type);
break;
case 3:
printf("\nEnter new balance: ");
scanf("%lf", &accounts[i].balance);
break;
default:
printf("\nWrong choice.\n");
}
}
FILE *newFile = fopen("tempmaster.txt", "w");
if (newFile == NULL)
{
printf("Error in adding account.\n\n");
fclose(originalFile);
free(accounts);
return -1;
}
fprintf(newFile, "%s%d\n\n%s\n%s\n", accCount_str, accCount, tbl_head_1, tbl_head_2);
for (i = 0; i < accCount; i++)
fprintf(newFile, "%-6d %-45s %-12d %-9s %-15.2f\n", accounts[i].serial,
accounts[i].name, accounts[i].account_number, accounts[i].type, accounts[i].balance);
// clean up
fclose(originalFile);
fclose(newFile);
free(accounts);
// modification
remove("masterfile.txt");
rename("tempmaster.txt", "masterfile.txt");
printf("Account added successfully.\n\n");
return 1;
}
int editAccount(void)
{
int accCount; // number of accounts
char accCount_str[25]; // will read "Number of accounts: "
char tbl_head_1[200]; // will contain the table headers
char tbl_head_2[200]; // will contain the dashes
int edit; // account number of the account to be edited
char response; // to make sure the right account is being edited
int choice; // to choose the field that has to be edited
char repeat; // to decide if further editing is needed
int i, j = -1;
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("Error!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%[A-Z a-z:] %d %[^\n] %[^\n]", accCount_str, &accCount, tbl_head_1, tbl_head_2);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
fscanf(originalFile, "%d %45[A-Z a-z.] %d %s %lf", &accounts[i].serial, accounts[i].name,
&accounts[i].account_number, accounts[i].type, &accounts[i].balance);
printf("Enter the account number of the account to be edited: ");
scanf("%d", &edit);
for (i = 0; i < accCount; i++)
{
if (accounts[i].account_number == edit)
{
j = i;
break;
}
}
if (j < 0)
{
printf("\nNo such account exists.\n\n");
fclose(originalFile);
free(accounts);
return -1;
}
printf("\nCurrently, the account is stored in this way:\n");
printf("Name: %s\n", accounts[j].name);
printf("Account type: %s\n", accounts[j].type);
printf("Balance:$ %.2f\n", accounts[j].balance);
printf("\nIs this the account to be edited? ( y / n ): ");
scanf(" %c", &response);
if (response != 'y')
{
printf("\nNo account edited.\n\n");
fclose(originalFile);
free(accounts);
return 1;
}
while (1)
{
printf("Enter the number designating the field you would like to edit.\n\n");
printf("1. Name\n");
printf("2. Account type\n");
printf("3. Balance\n\n");
printf("Now, enter choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter new name: ");
scanf("%*c %44[^\n]", accounts[j].name);
break;
case 2:
printf("Enter new account type: ");
scanf("%*c %[^\n]", accounts[j].type);
break;
case 3:
printf("Enter new balance: ");
scanf("%lf", &accounts[j].balance);
break;
default:
printf("Wrong choice.\n");
}
printf("\nThe edited account is stored in this way:\n");
printf("Name: %s\n", accounts[j].name);
printf("Account type: %s\n", accounts[j].type);
printf("Balance: $%.2f\n", accounts[j].balance);
printf("\nWould you like to edit more fields ( y / n )?\n");
scanf(" %c", &repeat);
if (repeat == 'n')
break;
else
continue;
}
FILE *newFile = fopen("tempmaster.txt", "w");
if (newFile == NULL)
{
printf("\nError in editing account.\n");
fclose(originalFile);
free(accounts);
return -1;
}
fprintf(newFile, "%s%d\n\n%s\n%s\n", accCount_str, accCount, tbl_head_1, tbl_head_2);
for (i = 0; i < accCount; i++)
fprintf(newFile, "%-6d %-45s %-12d %-9s %-15.2f\n", accounts[i].serial,
accounts[i].name, accounts[i].account_number, accounts[i].type, accounts[i].balance);
// clean up
fclose(originalFile);
fclose(newFile);
free(accounts);
// modification
remove("masterfile.txt");
rename("tempmaster.txt", "masterfile.txt");
printf("\nAccount edited successfully.\n\n");
return 1;
}
int deleteAccount(void)
{
int accCount; // number of accounts
char accCount_str[25]; // will read "Number of accounts: "
char tbl_head_1[200]; // will contain the table headers
char tbl_head_2[200]; // will contain the dashes
int del; // account number of the account to be deleted
int i, j = -1;
int serial;
char response;
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("Error!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%[A-Z a-z:] %d %[^\n] %[^\n]", accCount_str, &accCount, tbl_head_1, tbl_head_2);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
fscanf(originalFile, "%d %45[A-Z a-z.] %d %s %lf", &accounts[i].serial, accounts[i].name,
&accounts[i].account_number, accounts[i].type, &accounts[i].balance);
printf("Enter the account number of the account to be deleted: ");
scanf("%d", &del);
for (i = 0; i < accCount; i++)
{
if (accounts[i].account_number == del)
{
j = i;
break;
}
}
if (j < 0)
{
printf("\nNo such account exists.\n\n");
fclose(originalFile);
free(accounts);
return -1;
}
printf("\nAccount holder: %s\n", accounts[j].name);
printf("Account number: %d\n", accounts[j].account_number);
printf("Account type: %s\n", accounts[j].type);
printf("Account balance: $%.2f\n\n", accounts[j].balance);
printf("Is this the account to be deleted? ( y / n ): ");
scanf(" %c", &response);
if (response != 'y')
{
printf("No account deleted.\n\n");
fclose(originalFile);
free(accounts);
return 1;
}
FILE *newFile = fopen("tempmaster.txt", "w");
if (newFile == NULL)
{
printf("Error in deleting account.\n");
fclose(originalFile);
free(accounts);
return -1;
}
accCount--; // Since the number of accounts has just decreased by one
fprintf(newFile, "%s%d\n\n%s\n%s\n", accCount_str, accCount, tbl_head_1, tbl_head_2);
for (i = 0, serial = 1; i <= accCount; i++)
{
if (i != j)
{
fprintf(newFile, "%-6d %-45s %-12d %-9s %-15.2f\n", serial++,
accounts[i].name, accounts[i].account_number, accounts[i].type, accounts[i].balance);
}
}
// clean up
fclose(originalFile);
fclose(newFile);
free(accounts);
// modification
remove("masterfile.txt");
rename("tempmaster.txt", "masterfile.txt");
printf("Account deleted successfully.\n\n");
return 1;
}
int addBalance(void)
{
int accCount; // number of accounts
char accCount_str[25]; // will read "Number of accounts: "
char tbl_head_1[200]; // will contain the table headers
char tbl_head_2[200]; // will contain the dashes
int edit; // account number of the account to which money is to be added
char response; // to make sure the right account is selected and right amount of money was added
double amount; // the amount of money that is to be added
int i, j = -1;
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("Error!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%[A-Z a-z:] %d %[^\n] %[^\n]", accCount_str, &accCount, tbl_head_1, tbl_head_2);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
fscanf(originalFile, "%d %45[A-Z a-z.] %d %s %lf", &accounts[i].serial, accounts[i].name,
&accounts[i].account_number, accounts[i].type, &accounts[i].balance);
printf("\nEnter the account number of the account to which money is to be added: ");
scanf("%d", &edit);
for (i = 0; i < accCount; i++)
{
if (accounts[i].account_number == edit)
{
j = i;
break;
}
}
if (j < 0)
{
printf("\nNo such account exists.\n\n");
fclose(originalFile);
free(accounts);
return -1;
}
printf("\nCurrently, the account is stored in this way:\n");
printf("Name: %s\n", accounts[j].name);
printf("Account type: %s\n", accounts[j].type);
printf("Balance: $%.2f\n\n", accounts[j].balance);
printf("Is this the right account? ( y / n ): ");
scanf(" %c", &response);
if (response != 'y')
{
printf("\nNo changes were made.\n\n");
fclose(originalFile);
free(accounts);
return 1;
}
do {
printf("\nEnter the amount of money to be added: ");
scanf("%lf", &amount);
printf("\nDo you want to add $%.2f? ( y / n ): ", amount);
scanf(" %c", &response);
} while (response != 'y');
accounts[j].balance += amount;
FILE *newFile = fopen("tempmaster.txt", "w");
if (newFile == NULL)
{
printf("\nError in saving changes.\n\n");
fclose(originalFile);
free(accounts);
return -1;
}
fprintf(newFile, "%s%d\n\n%s\n%s\n", accCount_str, accCount, tbl_head_1, tbl_head_2);
for (i = 0; i < accCount; i++)
fprintf(newFile, "%-6d %-45s %-12d %-9s %-15.2f\n", accounts[i].serial,
accounts[i].name, accounts[i].account_number, accounts[i].type, accounts[i].balance);
printf("\nAfter adding balance, the account is stored in this way:\n");
printf("Name: %s\n", accounts[j].name);
printf("Account type: %s\n", accounts[j].type);
printf("Balance: $%.2f\n\n", accounts[j].balance);
// clean up
fclose(originalFile);
fclose(newFile);
free(accounts);
// modification
remove("masterfile.txt");
rename("tempmaster.txt", "masterfile.txt");
return 1;
}
int withdrawMoney(void)
{
int accCount; // number of accounts
char accCount_str[25]; // will read "Number of accounts: "
char tbl_head_1[200]; // will contain the table headers
char tbl_head_2[200]; // will contain the dashes
int edit; // account number of the account from which money is to be withdrawn
char response; // to make sure the right account is selected and the right amount of money is being withdrawn
double amount; // the amount of money to be withdrawn
int i, j = -1;
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("\nError!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%[A-Z a-z:] %d %[^\n] %[^\n]", accCount_str, &accCount, tbl_head_1, tbl_head_2);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
fscanf(originalFile, "%d %45[A-Z a-z.] %d %s %lf", &accounts[i].serial, accounts[i].name,
&accounts[i].account_number, accounts[i].type, &accounts[i].balance);
printf("\nEnter the account number of the account from which money is to be withdrawn: ");
scanf("%d", &edit);
for (i = 0; i < accCount; i++)
{
if (accounts[i].account_number == edit)
{
j = i;
break;
}
}
if (j < 0)
{
printf("\nNo such account exists.\n\n");
fclose(originalFile);
free(accounts);
return -1;
}
printf("\nCurrently, the account is stored in this way:\n");
printf("Name: %s\n", accounts[j].name);
printf("Account type: %s\n", accounts[j].type);
printf("Balance: $%.2f\n", accounts[j].balance);
printf("\nIs this the right account? ( y / n ): ");
scanf(" %c", &response);
if (response != 'y')
{
printf("No changes were made.\n\n");
fclose(originalFile);
free(accounts);
return 1;
}
do {
printf("\nEnter the amount of money to be withdrawn: ");
scanf("%lf", &amount);
printf("\nDo you want to withdraw $%.2f? ( y / n ): ", amount);
scanf(" %c", &response);
} while (response != 'y');
accounts[j].balance -= amount;
FILE *newFile = fopen("tempmaster.txt", "w");
if (newFile == NULL)
{
printf("\nError in saving changes.\n");
fclose(originalFile);
free(accounts);
return -1;
}
fprintf(newFile, "%s%d\n\n%s\n%s\n", accCount_str, accCount, tbl_head_1, tbl_head_2);
for (i = 0; i < accCount; i++)
fprintf(newFile, "%-6d %-45s %-12d %-9s %-15.2f\n", accounts[i].serial,
accounts[i].name, accounts[i].account_number, accounts[i].type, accounts[i].balance);
printf("\nAfter withdrawing money, the account is stored in this way:\n");
printf("Name: %s\n", accounts[j].name);
printf("Account type: %s\n", accounts[j].type);
printf("Balance: $%.2f\n\n", accounts[j].balance);
// clean up
fclose(originalFile);
fclose(newFile);
free(accounts);
// modification
remove("masterfile.txt");
rename("tempmaster.txt", "masterfile.txt");
return 1;
}
double totalBalance(void)
{
int accCount; // number of accounts
int i;
double total = 0;
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("\nError!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%*[A-Z a-z:] %d %*[^\n] %*[^\n]", &accCount);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
{
fscanf(originalFile, "%*d %*[A-Z a-z.] %*d %*s %lf", &accounts[i].balance);
total += accounts[i].balance;
}
printf("\nThe total balance of all accounts is $%.2f\n\n", total);
// clean up
fclose(originalFile);
free(accounts);
return total;
}
double maxBalance(void)
{
double max;
int i, j;
int accCount; // number of accounts
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("\nError!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%*[A-Z a-z:] %d %*[^\n] %*[^\n]", &accCount);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
{
fscanf(originalFile, "%*d %45[A-Z a-z.] %d %*s %lf", accounts[i].name,
&accounts[i].account_number, &accounts[i].balance);
}
for (i = 0, j = 0, max = accounts[i].balance; i < accCount; i++)
{
if (accounts[i].balance > max)
{
max = accounts[i].balance;
j = i;
}
}
printf("\nAccount no. %d has the maximum balance of $%.2lf\n", accounts[j].account_number, accounts[j].balance);
printf("The ownwer of that account is %s\n\n", accounts[j].name);
// clean up
fclose(originalFile);
free(accounts);
return max;
}
double minBalance(void)
{
double min;
int i, j;
int accCount; // number of accounts
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("\nError!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%*[A-Z a-z:] %d %*[^\n] %*[^\n]", &accCount);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
{
fscanf(originalFile, "%*d %45[A-Z a-z.] %d %*s %lf", accounts[i].name,
&accounts[i].account_number, &accounts[i].balance);
}
for (i = 0, j = 0, min = accounts[i].balance; i < accCount; i++)
{
if (accounts[i].balance < min)
{
min = accounts[i].balance;
j = i;
}
}
printf("\nAccount no. %d has the minimum balance of $%.2lf\n", accounts[j].account_number, accounts[j].balance);
printf("The ownwer of that account is %s\n\n", accounts[j].name);
// clean up
fclose(originalFile);
free(accounts);
return min;
}
double avgBalance(void)
{
int accCount; // number of accounts
int i;
double total = 0;
double average;
FILE *originalFile = fopen("masterfile.txt", "r");
if (originalFile == NULL)
{
printf("\nError!! File not found.\n\n");
return -1;
}
fscanf(originalFile, "%*[A-Z a-z:] %d %*[^\n] %*[^\n]", &accCount);
Account *accounts = malloc(sizeof(Account) * accCount);
for (i = 0; i < accCount; i++)
{
fscanf(originalFile, "%*d %*[A-Z a-z.] %*d %*s %lf", &accounts[i].balance);
total += accounts[i].balance;
}
average = total / (double)accCount;
printf("\nThe average of the balances is $%.2f\n\n", average);
// clean up
fclose(originalFile);
free(accounts);
return average;
}