-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplain.c
More file actions
672 lines (620 loc) · 26.6 KB
/
plain.c
File metadata and controls
672 lines (620 loc) · 26.6 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int no_of_records;
// Struct Record is defined to represent each record in the file
struct Record {
int idx;
char name[20];
int amount;
char recordType[8];
char expenseType[8];
} record;
// Function to print the main menu
void printMenu() {
printf("\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
printf("\n\nPERSONAL FINANCE MANAGEMENT\n");
printf("\nMenu:\n");
printf("1. Insert a record\n");
printf("2. Display records\n");
printf("3. Search a record by name\n");
printf("4. Modify a record\n");
printf("5. Delete a record\n");
printf("6. Sort data based on the amount\n");
printf("7. Display Income sources\n");
printf("8. Display expenses\n");
printf("9. Calculate interest of loan\n");
printf("10. Generate Report\n");
printf("11. Exit\n");
printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
}
// Function to count the number of records in the file
int getNoOfRecords(){
FILE *fp = fopen("File_Of_Records.txt", "r");
if(fp == NULL){
printf("\n\nError opening the file\n");
return -1;
}
no_of_records = 0;
while (fscanf(fp, "%d\t%s\t%d\t%s\t%s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) == 5) {
no_of_records++;
}
fclose(fp);
return no_of_records;
}
// Function to insert a new record into the file
void insertRecord() {
FILE *fp = fopen("File_Of_Records.txt", "a");
if(fp == NULL){
printf("\n\nError opening the file\n");
return;
}
no_of_records = getNoOfRecords();
record.idx = no_of_records + 1;
printf("\nEnter Name of the record: ");
scanf("%s", record.name);
printf("Enter the Amount: ");
scanf("%d", &record.amount);
printf("Enter type of the record -> Ex. INCOME / EXPENSE : ");
scanf("%s", record.recordType);
if(strcmp(record.recordType, "INCOME") == 0)
strcpy(record.expenseType, "NULL");
else if(strcmp(record.recordType, "EXPENSE") == 0){
printf("Type of the Expense -> Ex. NEED / WANT / INVEST : ");
scanf("%s", record.expenseType);
if(
strcmp(record.expenseType, "NEED") == 0 ||
strcmp(record.expenseType, "WANT") == 0 ||
strcmp(record.expenseType, "INVEST") == 0
){}
else{
printf("\nInvalid Input\n");
return;
}
}
else {
printf("\nInvalid Input\n");
return;
}
fprintf(fp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
printf("\n\nRECORD SUCCESSFULLY INSERTED\n");
fclose(fp);
}
// Function to display all records from the file
void displayRecord() {
no_of_records = getNoOfRecords();
if(no_of_records <= 0){
printf("\n\nFILE IS EMPTY\n");
return;
}
FILE *fp = fopen("File_Of_Records.txt", "r");
if(fp == NULL){
printf("\n\nError opening the file\n");
return;
}
printf("\n\nPERSONAL FINANCE MANAGEMENT\n");
printf("\n%-4s %-20s %-8s %-10s %-10s\n", "IDX", "Record Name", "Amount", "RType", "EType");
printf("----------------------------------------------------------\n");
while(fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
printf("%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
printf("\n\n%d records matched\n", no_of_records);
fclose((fp));
}
// Function to search for records by name
void searchByName(char *nameToFind){
no_of_records = getNoOfRecords();
int similarRecord = 0;
if(no_of_records <= 0){
printf("\n\nFILE IS EMPTY\n\n");
return;
}
FILE *fp = fopen("File_Of_Records.txt", "r");
if(fp == NULL){
printf("\n\nError opening the file\n");
return;
}
printf("\n\nPERSONAL FINANCE MANAGEMENT\n");
printf("\n%-4s %-20s %-8s %-10s %-10s\n", "IDX", "Record Name", "Amount", "RType", "EType");
printf("----------------------------------------------------------\n");
while(fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
if(strcmp(record.name, nameToFind) == 0){
printf("%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
similarRecord++;
}
}
if (similarRecord == 0)
printf("\n\nNo such record exist in the file\n");
else
printf("\n\n%d records matched\n", similarRecord);
fclose(fp);
}
// Function to modify the name of a record
void modifyName(int index){
no_of_records = getNoOfRecords();
if (index > no_of_records || index < 1)
{
printf("\n\nINVALID INDEX\n");
return;
}
FILE *fp = fopen("File_Of_Records.txt", "r");
FILE *tp = fopen("temp.txt", "w");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
for(int i = 1; i < index; i++){
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
printf("\nEnter Name of the record: ");
scanf("%s", record.name);
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", index, record.name, record.amount, record.recordType, record.expenseType);
while(fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
fp = fopen("File_Of_Records.txt", "w");
tp = fopen("temp.txt", "r");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
while(fscanf(tp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(fp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
printf("\n\nRECORD SUCCESSFULLY UPDATED\n");
}
// Function to modify the amount of a record
void modifyAmount(int index){
no_of_records = getNoOfRecords();
if (index > no_of_records || index < 1)
{
printf("\n\nINVALID INDEX\n");
return;
}
FILE *fp = fopen("File_Of_Records.txt", "r");
FILE *tp = fopen("temp.txt", "w");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
for(int i = 1; i < index; i++){
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
printf("\nEnter the Amount: ");
scanf("%d", &record.amount);
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", index, record.name, record.amount, record.recordType, record.expenseType);
while(fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
fp = fopen("File_Of_Records.txt", "w");
tp = fopen("temp.txt", "r");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
while(fscanf(tp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(fp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
printf("\n\nRECORD SUCCESSFULLY UPDATED\n");
}
// Function to modify the type of a record
void modifyRtype(int index){
no_of_records = getNoOfRecords();
if (index > no_of_records || index < 1)
{
printf("\n\nINVALID INDEX\n");
return;
}
FILE *fp = fopen("File_Of_Records.txt", "r");
FILE *tp = fopen("temp.txt", "w");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
for(int i = 1; i < index; i++){
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
printf("\nEnter type of the record -> Ex. INCOME / EXPENSE : ");
scanf("%s", record.recordType);
if(strcmp(record.recordType, "INCOME") == 0)
strcpy(record.expenseType, "NULL");
else if(strcmp(record.recordType, "EXPENSE") == 0){
printf("Type of the Expense -> Ex. NEED / WANT / INVEST : ");
scanf("%s", record.expenseType);
}
else {
printf("\nInvalid Input\n");
return;
}
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", index, record.name, record.amount, record.recordType, record.expenseType);
while(fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
fp = fopen("File_Of_Records.txt", "w");
tp = fopen("temp.txt", "r");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
while(fscanf(tp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(fp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
printf("\n\nRECORD SUCCESSFULLY UPDATED\n");
}
// Function to modify a record (name, amount, type)
void modifyRecord(int index) {
no_of_records = getNoOfRecords();
if (index > no_of_records || index < 1)
{
printf("\n\nINVALID INDEX\n");
return;
}
int choice;
printf("\n\n1. Update Record Name: \n");
printf("2. Update Record Amount: \n");
printf("3. Update Record Type or Expense Type: \n");
printf("4. Update all: \n");
printf("Enter choice: ");
scanf("%d", &choice);
FILE *fp = fopen("File_Of_Records.txt", "r");
FILE *tp = fopen("temp.txt", "w");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
switch (choice)
{
case 1:
modifyName(index);
break;
case 2:
modifyAmount(index);
break;
case 3:
modifyRtype(index);
break;
case 4:
for(int i = 1; i < index; i++){
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
printf("\nEnter Name of the record: ");
scanf("%s", record.name);
printf("Enter the Amount: ");
scanf("%d", &record.amount);
printf("Enter type of the record -> Ex. INCOME / EXPENSE : ");
scanf("%s", record.recordType);
if(strcmp(record.recordType, "INCOME") == 0)
strcpy(record.expenseType, "NULL");
else if(strcmp(record.recordType, "EXPENSE") == 0){
printf("Type of the Expense -> Ex. NEED / WANT / INVEST : ");
scanf("%s", record.expenseType);
}
else {
printf("\n\nINVALID INPUT\n");
return;
}
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", index, record.name, record.amount, record.recordType, record.expenseType);
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
while(fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
fp = fopen("File_Of_Records.txt", "w");
tp = fopen("temp.txt", "r");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
while(fscanf(tp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(fp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
printf("\n\nRECORD SUCCESSFULLY UPDATED\n");
default:
printf("\n\nInvalid choice. Please enter a valid option.\n");
break;
}
}
// Function to delete a record by index
void deleteRecord(int index) {
no_of_records = getNoOfRecords();
if (index > no_of_records || index < 1)
{
printf("\n\nINVALID INDEX\n");
return;
}
FILE *fp = fopen("File_Of_Records.txt", "r");
FILE *tp = fopen("temp.txt", "w");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
for(int i = 1; i < index; i++){
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType);
while(fscanf(fp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(tp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx - 1, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
fp = fopen("File_Of_Records.txt", "w");
tp = fopen("temp.txt", "r");
if(fp == NULL || tp == NULL){
printf("\n\nError opening the file\n");
return;
}
while(fscanf(tp, "%d\t %s\t %d\t %s\t\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
fprintf(fp, "%-4d %-20s %-8d %-10s %-10s\n", record.idx, record.name, record.amount, record.recordType, record.expenseType);
}
fclose(fp);
fclose(tp);
printf("\n\nRECORD SUCCESSFULLY DELETED\n");
}
// Function to sort records by amount
void sortRecordsByAmount() {
no_of_records = getNoOfRecords();
struct Record recordsArr[no_of_records];
FILE *fp = fopen("File_Of_Records.txt", "r");
if(fp == NULL){
printf("\n\nError opening the file\n");
return;
}
int k = 0;
while(fscanf(fp, "%d\t %s\t %d\t %s\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
recordsArr[k] = record;
k++;
}
fclose(fp);
for(int i = 0; i < no_of_records; i++){
for (int j = 0; j < no_of_records; j++)
{
if (recordsArr[j].amount > recordsArr[i].amount)
{
struct Record temp = recordsArr[i];
recordsArr[i] = recordsArr[j];
recordsArr[j] = temp;
}
}
}
printf("\n\nPERSONAL FINANCE MANAGEMENT\n");
printf("\n%-4s %-20s %-8s %-10s %-10s\n", "IDX", "Record Name", "Amount", "RType", "EType");
printf("----------------------------------------------------------\n");
for(int i = 0; i < no_of_records; i++){
printf("%-4d %-20s %-8d %-10s %-10s\n", i + 1, recordsArr[i].name, recordsArr[i].amount, recordsArr[i].recordType, recordsArr[i].expenseType);
}
printf("\n\n%d records matched\n", no_of_records);
}
// Function to display income sources
void displayIncomeSources() {
no_of_records = getNoOfRecords();
FILE *fp = fopen("File_Of_Records.txt", "r");
if(fp == NULL){
printf("\n\nError opening the file\n");
return;
}
printf("\n\nPERSONAL FINANCE MANAGEMENT\n");
printf("\n%-4s %-20s %-8s %-10s %-10s\n", "IDX", "Record Name", "Amount", "RType", "EType");
printf("----------------------------------------------------------\n");
int similarRecord = 0;
while(fscanf(fp, "%d\t %s\t %d\t %s\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
if(strcmp(record.recordType, "INCOME") == 0){
similarRecord++;
printf("%-4d %-20s %-8d %-10s %-10s\n", similarRecord, record.name, record.amount, record.recordType, record.expenseType);
}
}
if (similarRecord == 0)
printf("\n\nNo such record exist in the file\n");
else
printf("\n\n%d records matched\n", similarRecord);
fclose(fp);
}
// Function to display expenses
void displayExpenses() {
no_of_records = getNoOfRecords();
FILE *fp = fopen("File_Of_Records.txt", "r");
if(fp == NULL){
printf("\n\nError opening the file\n");
return;
}
printf("\n\nPERSONAL FINANCE MANAGEMENT\n");
printf("\n%-4s %-20s %-8s %-10s %-10s\n", "IDX", "Record Name", "Amount", "RType", "EType");
printf("----------------------------------------------------------\n");
int similarRecord = 0;
while(fscanf(fp, "%d\t %s\t %d\t %s\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
if(strcmp(record.recordType, "EXPENSE") == 0){
similarRecord++;
printf("%-4d %-20s %-8d %-10s %-10s\n", similarRecord, record.name, record.amount, record.recordType, record.expenseType);
}
}
if (similarRecord == 0)
printf("\n\nNo such record exist in the file\n");
else
printf("\n\n%d records matched\n", similarRecord);
fclose(fp);
}
// Function to calculate loan interest
void calculateLoanInterest() {
double loanAmount, annualInterestRate, monthlyInterestRate;
int loanDurationMonths;
printf("\nEnter loan amount: ");
scanf("%lf", &loanAmount);
printf("Enter annual interest rate (in percentage): ");
scanf("%lf", &annualInterestRate);
printf("Enter loan duration (in months): ");
scanf("%d", &loanDurationMonths);
// Convert annual interest rate to monthly interest rate
monthlyInterestRate = annualInterestRate / 12.0 / 100.0;
// Calculate monthly payment using formula for amortizing loan
double monthlyPayment = loanAmount * (monthlyInterestRate * pow(1 + monthlyInterestRate, loanDurationMonths)) / (pow(1 + monthlyInterestRate, loanDurationMonths) - 1);
// Calculate total payment over the loan term
double totalPayment = monthlyPayment * loanDurationMonths;
// Calculate total interest paid over the loan term
double totalInterest = totalPayment - loanAmount;
// Print results
printf("\n\nPERSONAL FINANCE MANAGEMENT\n");
printf("\nLoan Details:\n\n");
printf("Loan Amount : %.2lf\n", loanAmount);
printf("Annual Interest Rate : %.2lf%%\n", annualInterestRate);
printf("Loan Duration (months): %d\n", loanDurationMonths);
printf("\nMonthly Payment : %.2lf\n", monthlyPayment);
printf("Total Payment : %.2lf\n", totalPayment);
printf("Monthly Interest Paid : %.2lf\n", totalInterest/loanDurationMonths);
printf("Total Interest Paid : %.2lf\n", totalInterest);
}
void generateReport() {
double income = 0;
double needsExpenses = 0;
double wantsExpenses = 0;
double savingsExpenses = 0;
int no_of_records = getNoOfRecords();
FILE *fp = fopen("File_Of_Records.txt", "r");
if(fp == NULL){
printf("\n\nError opening the file\n");
return;
}
while(fscanf(fp, "%d\t %s\t %d\t %s\t %s\n", &record.idx, record.name, &record.amount, record.recordType, record.expenseType) != EOF){
if(strcmp(record.recordType, "INCOME") == 0){
income += record.amount;
} else if(strcmp(record.recordType, "EXPENSE") == 0) {
if(strcmp(record.expenseType, "NEED") == 0) {
needsExpenses += record.amount;
} else if(strcmp(record.expenseType, "WANT") == 0) {
wantsExpenses += record.amount;
} else if(strcmp(record.expenseType, "INVEST") == 0) {
savingsExpenses += record.amount;
}
}
}
fclose(fp);
double needsRatio = 0.50;
double wantsRatio = 0.30;
double savingsRatio = 0.20;
// Calculate the total expenses in each category
double totalExpenses = needsExpenses + wantsExpenses + savingsExpenses;
// Calculate the remaining income after deducting total expenses
double remainingIncome = income - totalExpenses;
// Calculate the needs, wants, and savings amount based on the remaining income
double needsAmount = income * needsRatio;
double wantsAmount = income * wantsRatio;
double savingsAmount = income * savingsRatio;
printf("\n\nFinancial Distribution Report\n\n");
printf("Income : %.2f\n", income);
printf("Total Expenses : %.2f\n", totalExpenses);
printf("Remaining Income : %.2f\n\n", remainingIncome);
printf("Needs Expenses : %.2f\n", needsExpenses);
printf("Wants Expenses : %.2f\n", wantsExpenses);
printf("Savings and Investment Expenses: %.2f\n\n", savingsExpenses);
printf("Needs Expected : %.2f\n", needsAmount);
printf("Wants Expected : %.2f\n", wantsAmount);
printf("Savings and Investment Expected: %.2f\n", savingsAmount);
if (totalExpenses > income) {
printf("\nWarning: Your total expenses exceed your income.\n");
printf("Ensure your expenses do not exceed your income to maintain financial stability.\n");
}
if (needsExpenses > needsAmount || wantsExpenses > wantsAmount || savingsExpenses < savingsAmount) {
printf("\nWarning: Your income distribution does not meet the recommended ratio.\n");
printf("Consider adjusting your spending priorities to align with the following ratio:\n");
printf("Needs: 50%%, Wants: 30%%, Investment: 20%%\n\n");
// Provide suggestions for adjusting spending priorities
if (needsExpenses > needsAmount) {
double adjustment = needsExpenses - needsAmount;
printf("-> Reduce your needs expenses by %.2f to align with the recommended ratio.\n", adjustment);
printf("-> Ensure that your essential needs are being met, but consider evaluating your expenses to see if any non-essential items can be reduced.\n\n");
}
if (wantsExpenses > wantsAmount) {
double adjustment = wantsExpenses - wantsAmount;
printf("-> Reduce your wants expenses by %.2f to align with the recommended ratio.\n", adjustment);
printf("-> Review your discretionary spending on non-essential items such as entertainment, dining out, or luxury purchases. Consider cutting back on these expenses to bring them in line with your financial goals.\n\n");
}
if (savingsExpenses < savingsAmount) {
double adjustment = savingsAmount - savingsExpenses;
printf("-> Increase your savings and Invesment expenses by %.2f to align with the recommended ratio.\n", adjustment);
printf("-> While it's important to prioritize savings, ensure that you are not neglecting your current needs and wants. Consider reallocating some funds from other categories to boost your savings.\n\n");
}
}
else {
printf("\nCongratulations! Your income distribution meets the recommended ratio of, Needs: 50%%, Wants: 30%%, Investment: 20%%\n\n");
printf("Keep up the good work! Consistently managing your finances in this way will help you achieve your financial goals.\n\n");
printf("Remember to review your finances regularly and make adjustments as needed to stay on track.\n\n");
}
}
int main() {
int choice;
int index;
do {
printMenu();
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertRecord();
break;
case 2:
displayRecord();
break;
case 3:
printf("\nEnter record name you want to find: ");
char str[30];
scanf("%s", str);
searchByName(str);
break;
case 4:
printf("\nEnter index number of the record you want to modify: ");
scanf("%d", &index);
modifyRecord(index);
break;
case 5:
printf("\nEnter index number of the record you want to delete: ");
scanf("%d", &index);
deleteRecord(index);
break;
case 6:
sortRecordsByAmount();
break;
case 7:
displayIncomeSources();
break;
case 8:
displayExpenses();
break;
case 9:
calculateLoanInterest();
break;
case 10:
generateReport();
break;
case 11:
printf("\nPROGRAM IS TERMINATED\n\n");
exit(0);
break;
default:
printf("\nInvalid choice. Please enter a valid option.\n");
}
} while (choice != 11);
return 0;
}