forked from davideq/codingDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
1326 lines (1144 loc) · 37.1 KB
/
Copy pathmain.c
File metadata and controls
1326 lines (1144 loc) · 37.1 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
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* ===== Constants ===== */
#define MAX_TITLE 300
#define MAX_AUTHOR 300
#define MAX_CATEGORY 100
#define MAX_ISBN 20
#define MAX_PHONE 20
#define MAX_EMAIL 300
#define MAX_DATE 11
#define MAX_FIRST_NAME 50
#define MAX_LAST_NAME 50
/* ===== Data structures ===== */
typedef struct {
int id;
char title[MAX_TITLE];
char author[MAX_AUTHOR];
char category[MAX_CATEGORY];
int year;
char isbn[MAX_ISBN];
int total_quantity;
int available_quantity;
int total_loans;
int is_active;
} Book;
typedef struct {
int id;
char first_name[MAX_FIRST_NAME];
char last_name[MAX_LAST_NAME];
char phone[MAX_PHONE];
char email[MAX_EMAIL];
int max_active_loans;
int total_loans;
int is_active;
} User;
typedef struct {
int id;
int user_id;
int book_id;
char loan_date[MAX_DATE];
char due_date[MAX_DATE];
char return_date[MAX_DATE];
int is_active;
} Loan;
typedef struct {
Book *items;
int count;
int capacity;
} BookArray;
typedef struct {
User *items;
int count;
int capacity;
} UserArray;
typedef struct {
Loan *items;
int count;
int capacity;
} LoanArray;
/* ===== Function prototypes ===== */
void stripLineEnding(char *text);
void readLine(const char *prompt, char *buffer, size_t size);
int readInt(const char *prompt);
int readIntInRange(const char *prompt, int min, int max);
int isBlank(const char *text);
int containsIgnoreCase(const char *text, const char *pattern);
int isValidDate(const char *date);
void getCurrentDate(char *buffer, size_t size);
void shiftDate(const char *input_date, int days, char *buffer, size_t size);
void waitForEnter(void);
void initBooks(BookArray *books);
void freeBooks(BookArray *books);
void ensureBookCapacity(BookArray *books);
int nextBookId(const BookArray *books);
Book *findBookById(BookArray *books, int id);
const Book *findBookByIdConst(const BookArray *books, int id);
void addBook(BookArray *books);
void updateBook(BookArray *books);
void deactivateBook(BookArray *books);
void searchBooks(const BookArray *books);
void printBooks(const BookArray *books, int active_only);
void sortBooksByTitle(BookArray *books);
void sortBooksByAuthor(BookArray *books);
void sortBooksByYear(BookArray *books);
int countActiveBooks(const BookArray *books);
void initUsers(UserArray *users);
void freeUsers(UserArray *users);
void ensureUserCapacity(UserArray *users);
int nextUserId(const UserArray *users);
User *findUserById(UserArray *users, int id);
const User *findUserByIdConst(const UserArray *users, int id);
void addUser(UserArray *users);
void updateUser(UserArray *users);
void deactivateUser(UserArray *users);
void searchUsers(const UserArray *users);
void printUsers(const UserArray *users, int active_only);
int countActiveUsers(const UserArray *users);
void initLoans(LoanArray *loans);
void freeLoans(LoanArray *loans);
void ensureLoanCapacity(LoanArray *loans);
int nextLoanId(const LoanArray *loans);
Loan *findLoanById(LoanArray *loans, int id);
void createLoan(BookArray *books, UserArray *users, LoanArray *loans);
void returnBook(BookArray *books, LoanArray *loans);
void printActiveLoans(const LoanArray *loans);
void printOverdueLoans(const LoanArray *loans);
void printUserLoanHistory(const LoanArray *loans, int user_id);
int countActiveLoans(const LoanArray *loans);
int countOverdueLoans(const LoanArray *loans);
int countActiveLoansForUser(const LoanArray *loans, int user_id);
int loadBooks(BookArray *books, const char *path);
int loadUsers(UserArray *users, const char *path);
int loadLoans(LoanArray *loans, const char *path);
int saveAllData(const BookArray *books, const char *books_path,
const UserArray *users, const char *users_path,
const LoanArray *loans, const char *loans_path);
void printStatistics(const BookArray *books, const UserArray *users, const LoanArray *loans);
void runMainMenu(BookArray *books, UserArray *users, LoanArray *loans);
static int compareBooksByTitle(const void *left, const void *right);
static int compareBooksByAuthor(const void *left, const void *right);
static int compareBooksByYear(const void *left, const void *right);
void sortBooksByTitle(BookArray *books)
{
qsort(books->items, books->count, sizeof(Book), compareBooksByTitle);
printf("Books sorteed by title.\n");
}
void sortBooksByAuthor(BookArray *books){qsort(books->items, books->count, sizeof(Book), compareBooksByAuthor); printf("books sorted by Author.\n");}
#define MAX_AUTHOR 300
#define MAX_CATEGORY 100
#define MAX_ISBN 20
#define ROS 222
#define MAX_PHONE 20
void stripLineEnding(char *text) {
size_t length;
if (text == NULL) { return; }
length=strlen(text);
while (length > 0 && (text[length - 1]=='\n' || text[length - 1] == '\r')) {
text[length - 1] = '\0'; length--;
}
}
void readLine(const char *prompt, char *buffer, size_t size) {
if (prompt != NULL) {
printf("%s", prompt);
}
if (fgets(buffer, (int) size, stdin) == NULL) {
buffer[0] = '\0';
return;
}
stripLineEnding(buffer);
}
int readInt(const char *prompt)
{
char buffer[64]; char *parse_end; long value;
for (;;) {
readLine(prompt, buffer, sizeof(buffer));
value = strtol(buffer, &parse_end, 10);
if (parse_end != buffer && *parse_end=='\0') return (int) value;
printf("Invalid value, plz Try agan.\n");
}
}
int readIntInRange(const char *prompt, int min, int max) {
int value;
for (;;) {
value=readInt(prompt);
if (value >= min && value <= max) { return value; }
printf("Enter a value betwen %d and %d.\n", min, max);
}
}
int main(void) {
BookArray books;
UserArray users;
LoanArray loans;
initBooks(&books);
initUsers(&users);
initLoans(&loans);
loadBooks(&books, "data/books.txt");
loadUsers(&users, "data/users.txt");
loadLoans(&loans, "data/loans.txt");
printf("Library madagement system started.\n");
runMainMenu(&books, &users, &loans);
if (!saveAllData(&books, "data/books.txt",
&users, "data/users.txt",
&loans, "data/loans.txt")) {
fprintf(stderr, "Error while saving data.\n");
freeBooks(&books);
freeUsers(&users);
freeLoans(&loans);
return EXIT_FAILURE;
}
freeBooks(&books);
freeUsers(&users);
freeLoans(&loans);
return 0;
}
int isBlank(const char *text)
{
while (text != NULL && *text != '\0') {
if (!isspace((unsigned char) *text)) return 0;
text++;
}
return 1;
}
int containsIgnoreCase(const char *text, const char *pattern) {
size_t text_index; size_t pattern_index; size_t pattern_length;
pattern_length=strlen(pattern);
if (pattern_length==0) return 1;
for (text_index = 0; text[text_index] != '\0'; text_index++) {
for(pattern_index=0;pattern_index<pattern_length;pattern_index++){
if (text[text_index + pattern_index] == '\0') { return 0; }
if (tolower((unsigned char) text[text_index + pattern_index]) != tolower((unsigned char) pattern[pattern_index])) break;
}
if (pattern_index == pattern_length) return 1;
}
return 0;
}
int isValidDate(const char *date)
{
int year; int month; int day;
if (date == NULL || strlen(date)!=10) return 0;
if (sscanf(date, "%4d-%2d-%2d", &year, &month, &day) != 3) { return 0; }
if (month<1 || month > 12 || day <1 || day>31) return 0;
return 1;
}
void getCurrentDate(char *buffer, size_t size) {
time_t now;
struct tm *local_time;
now = time(NULL);
local_time = localtime(&now);
strftime(buffer, size, "%Y-%m-%d", local_time);
}
void shiftDate(const char *input_date, int days, char *buffer, size_t size) {
struct tm shifted_date;
time_t timestamp;
memset(&shifted_date, 0, sizeof(shifted_date));
if (sscanf(input_date, "%4d-%2d-%2d", &shifted_date.tm_year, &shifted_date.tm_mon, &shifted_date.tm_mday) != 3) {
buffer[0] = '\0';
return;
}
shifted_date.tm_year -= 1900;
shifted_date.tm_mon -= 1;
shifted_date.tm_isdst = -1;
timestamp = mktime(&shifted_date);
timestamp += (time_t) days * 24 * 60 * 60;
shifted_date = *localtime(×tamp);
strftime(buffer, size, "%Y-%m-%d", &shifted_date);
}
void waitForEnter(void) {
char buffer[8];
printf("\nPress enter to Continue...");
fgets(buffer, sizeof(buffer), stdin);
}
static int compareBooksByTitle(const void *left, const void *right) {
const Book *left_book = (const Book *) left;
const Book *right_book = (const Book *) right;
return strcmp(left_book->title, right_book->title);
}
static int compareBooksByAuthor(const void *left, const void *right) {
const Book *left_book = (const Book *) left;
const Book *right_book = (const Book *) right;
return strcmp(left_book->author, right_book->author);
}
static int compareBooksByYear(const void *left, const void *right) {
const Book *left_book = (const Book *) left;
const Book *right_book = (const Book *) right;
return left_book->year - right_book->year;
}
void initBooks(BookArray *books) { books->items=NULL; books->count = 0; books->capacity=0; }
void freeBooks(BookArray *books)
{
free(books->items); books->items = NULL;
books->count=0;
books->capacity = 0;
}
void ensureBookCapacity(BookArray *books) {
Book *resized_items;
int resized_capacity;
if (books->count < books->capacity) {
return;
}
resized_capacity = books->capacity == 0 ? 8 : books->capacity * 2;
resized_items = (Book *) realloc(books->items, sizeof(Book) * resized_capacity);
if (resized_items == NULL) {
fprintf(stderr, "Insufficient memory for books.\n");
exit(EXIT_FAILURE);
}
books->items = resized_items;
books->capacity = resized_capacity;
}
int nextBookId(const BookArray *books) {
int book_index; int max_id=0;
for (book_index = 0; book_index < books->count; book_index++) {
if (books->items[book_index].id>max_id) max_id = books->items[book_index].id;
}
return max_id + 1;
}
Book *findBookById(BookArray *books, int id)
{
int book_index;
for (book_index = 0; book_index < books->count; book_index++) {
if (books->items[book_index].id==id) { return &books->items[book_index]; }
}
return NULL;
}
const Book *findBookByIdConst(const BookArray *books, int id) {
int book_index;
for (book_index = 0; book_index < books->count; book_index++) {
if (books->items[book_index].id == id) {
return &books->items[book_index];
}
}
return NULL;
}
void addBook(BookArray *books) {
Book book;
memset(&book, 0, sizeof(book));
book.id = nextBookId(books);
readLine("Title: ", book.title, sizeof(book.title));
readLine("Author: ", book.author, sizeof(book.author));
readLine("Categorrry: ", book.category, sizeof(book.category));
book.year = readInt("Year: ");
readLine("ISBN: ", book.isbn, sizeof(book.isbn));
book.total_quantity = readIntInRange("Total quantity: ", 0, 1000000);
book.available_quantity = book.total_quantity;
book.is_active = 1;
ensureBookCapacity(books);
books->items[books->count++] = book;
printf("Book added wtith ID %d.\n", book.id);
}
void updateBook(BookArray *books) {
int id;
Book *book;
char buffer[128];
id = readInt("Book ID to update: ");
book = NULL;
{
int book_index;
for (book_index = 0; book_index < books->count; book_index++) {
if (books->items[book_index].id == id) {
book = &books->items[book_index];
break;
}
}
}
if (book == NULL) {
printf("Book not found.\n");
return;
}
readLine("New title (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(book->title, buffer, sizeof(book->title) - 1);
book->title[sizeof(book->title) - 1] = '\0';
}
readLine("New author (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(book->author, buffer, sizeof(book->author) - 1);
book->author[sizeof(book->author) - 1] = '\0';
}
readLine("New category (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(book->category, buffer, sizeof(book->category) - 1);
book->category[sizeof(book->category) - 1] = '\0';
}
readLine("New ISBN (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(book->isbn, buffer, sizeof(book->isbn) - 1);
book->isbn[sizeof(book->isbn) - 1] = '\0';
}
printf("book Updated.\n");
}
void deactivateBook(BookArray *books) {
int id; Book *book;
id=readInt("Book ID to deactivate: ");
book = findBookById(books, id);
if (book == NULL) {
printf("Book not found.\n");
return;
}
book->is_active=0; printf("Bookss deactivated.\n");
}
void searchBooks(const BookArray *books) {
char query[128];
int book_index;
int found = 0;
readLine("Search by title/author/category: ", query, sizeof(query));
for (book_index = 0; book_index < books->count; book_index++) {
const Book *book = &books->items[book_index];
if (!book->is_active) {
continue;
}
if (containsIgnoreCase(book->title, query) ||
containsIgnoreCase(book->author, query) ||
containsIgnoreCase(book->category, query)) {
printf("[%d] %s | %s | %s | %d | avail. %d/%d\n",
book->id,
book->title,
book->author,
book->category,
book->year,
book->available_quantity,
book->total_quantity);
found = 1;
}
}
if (!found) {
printf("No books was found.\n");
}
}
int saveAllData(const BookArray *books, const char *books_path,
const UserArray *users, const char *users_path,
const LoanArray *loans, const char *loans_path) {
FILE *file;
int record_index;
file = fopen(books_path, "w");
if (file == NULL) {
return 0;
}
for (record_index = 0; record_index < books->count; record_index++) {
const Book *book = &books->items[record_index];
fprintf(file, "%d|%s|%s|%s|%d|%s|%d|%d|%d|%d\n",
book->id,
book->title,
book->author,
book->category,
book->year,
book->isbn,
book->total_quantity,
book->available_quantity,
book->total_loans,
book->is_active);
}
fclose(file);
file = fopen(users_path, "w");
if (file == NULL) {
return 0;
}
for (record_index = 0; record_index < users->count; record_index++) {
const User *user = &users->items[record_index];
fprintf(file, "%d|%s|%s|%s|%s|%d|%d|%d\n",
user->id,
user->first_name,
user->last_name,
user->phone,
user->email,
user->max_active_loans,
user->total_loans,
user->is_active);
}
fclose(file);
file = fopen(loans_path, "w");
if (file == NULL) {
return 0;
}
for (record_index = 0; record_index < loans->count; record_index++) {
const Loan *loan = &loans->items[record_index];
fprintf(file, "%d|%d|%d|%s|%s|%s|%d\n",
loan->id,
loan->user_id,
loan->book_id,
loan->loan_date,
loan->due_date,
loan->return_date,
loan->is_active);
}
fclose(file);
return 1;
}
void printBooks(const BookArray *books, int active_only) {
int book_index;
if (books->count == 0) {
printf("No books availabe.\n");
return;
}
for (book_index = 0; book_index < books->count; book_index++) {
const Book *book = &books->items[book_index];
if (active_only && !book->is_active) {
continue;
}
printf("[%d] %s\n", book->id, book->title);
printf(" Author: %s\n", book->author);
printf(" Category: %s | Year: %d | ISBN: %s\n", book->category, book->year, book->isbn);
printf(" Availability: %d/%d | Total loans: %d | Status: %s\n",
book->available_quantity,
book->total_quantity,
book->total_loans,
book->is_active ? "active" : "deactivated");
}
}
void sortBooksByYear(BookArray *books) {
qsort(books->items, books->count, sizeof(Book), compareBooksByYear);
printf("Books sorted by yeer.\n");
}
int countActiveBooks(const BookArray *books)
{
int book_index; int total = 0;
for (book_index = 0; book_index < books->count; book_index++) { if (books->items[book_index].is_active) total++; }
return total;
}
void initUsers(UserArray *users) { users->items = NULL; users->count=0; users->capacity = 0; }
void freeUsers(UserArray *users)
{
free(users->items);
users->items=NULL;
users->count = 0; users->capacity=0;
}
void ensureUserCapacity(UserArray *users) {
User *resized_items;
int resized_capacity;
if (users->count < users->capacity) {
return;
}
resized_capacity = users->capacity == 0 ? 8 : users->capacity * 2;
resized_items = (User *) realloc(users->items, sizeof(User) * resized_capacity);
if (resized_items == NULL) {
fprintf(stderr, "Insufficient mermory for users.\n");
exit(EXIT_FAILURE);
}
users->items = resized_items;
users->capacity = resized_capacity;
}
int nextUserId(const UserArray *users) {int user_index; int max_id = 0;
for (user_index = 0; user_index < users->count; user_index++) { if (users->items[user_index].id > max_id) max_id=users->items[user_index].id; }
return max_id + 1;
}
User *findUserById(UserArray *users, int id) {
int user_index;
for (user_index = 0; user_index < users->count; user_index++) {
if (users->items[user_index].id == id) {
return &users->items[user_index];
}
}
return NULL;
}
const User *findUserByIdConst(const UserArray *users, int id) {
int user_index;
for (user_index = 0; user_index < users->count; user_index++) {
if (users->items[user_index].id == id) {
return &users->items[user_index];
}
}
return NULL;
}
void addUser(UserArray *users) {
User user;
memset(&user, 0, sizeof(user));
user.id = nextUserId(users);
readLine("First: ", user.first_name, sizeof(user.first_name));
readLine("Last name: ", user.last_name, sizeof(user.last_name));
readLine("Phone: ", user.phone, sizeof(user.phone));
readLine("Email: ", user.email, sizeof(user.email));
user.max_active_loans = readIntInRange("Maximum active loans: ", 1, 20);
user.is_active = 1;
ensureUserCapacity(users);
users->items[users->count++] = user;
printf("User addded with id %d.\n", user.id);
}
void updateUser(UserArray *users) {
int id;
User *user;
char buffer[128];
id = readInt("User ID to update: ");
user = NULL;
{
int user_index;
for (user_index = 0; user_index < users->count; user_index++) {
if (users->items[user_index].id == id) {
user = &users->items[user_index];
break;
}
}
}
if (user == NULL) {
printf("User not found.\n");
return;
}
readLine("New first name (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(user->first_name, buffer, sizeof(user->first_name) - 1);
user->first_name[sizeof(user->first_name) - 1] = '\0';
}
readLine("New last name (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(user->last_name, buffer, sizeof(user->last_name) - 1);
user->last_name[sizeof(user->last_name) - 1] = '\0';
}
readLine("New phone (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(user->phone, buffer, sizeof(user->phone) - 1);
user->phone[sizeof(user->phone) - 1] = '\0';
}
readLine("New email (blank to keep current): ", buffer, sizeof(buffer));
if (!isBlank(buffer)) {
strncpy(user->email, buffer, sizeof(user->email) - 1);
user->email[sizeof(user->email) - 1] = '\0';
}
printf("user Updated.\n");
}
void deactivateUser(UserArray *users)
{
int id; User *user;
id = readInt("User ID to deactivate: "); user = findUserById(users, id);
if (user == NULL) {
printf("User not found.\n");
return;
}
user->is_active=0;
printf("User deactivatd.\n");
}
void searchUsers(const UserArray *users) {
char query[128];
int user_index;
int found = 0;
readLine("Search by firswt name/last name/email: ", query, sizeof(query));
for (user_index = 0; user_index < users->count; user_index++) {
const User *user = &users->items[user_index];
if (!user->is_active) {
continue;
}
if (containsIgnoreCase(user->first_name, query) ||
containsIgnoreCase(user->last_name, query) ||
containsIgnoreCase(user->email, query)) {
printf("[%d] %s %s | %s | %s | max %d\n",
user->id,
user->first_name,
user->last_name,
user->phone,
user->email,
user->max_active_loans);
found = 1;
}
}
if (!found) {
printf("No users where found.\n");
}
}
void printUsers(const UserArray *users, int active_only) {
int user_index;
if (users->count == 0) {
printf("No users availabe.\n");
return;
}
for (user_index = 0; user_index < users->count; user_index++) {
const User *user = &users->items[user_index];
if (active_only && !user->is_active) {
continue;
}
printf("[%d] %s %s\n", user->id, user->first_name, user->last_name);
printf(" Phone: %s | Email: %s\n", user->phone, user->email);
printf(" Max loans: %d | Total loans: %d | Status: %s\n",
user->max_active_loans,
user->total_loans,
user->is_active ? "active" : "deactivated");
}
}
#define ROSS 222
int countActiveLoansForUser(const LoanArray *loans, int user_id) {
int loan_index;
int count = 0;
for (loan_index = 0; loan_index < loans->count; loan_index++) {
if (loans->items[loan_index].user_id == user_id && loans->items[loan_index].is_active) {
count++;
}
}
return count;
}
int countActiveUsers(const UserArray *users) {
int user_index;
int total = 0;
for (user_index = 0; user_index < users->count; user_index++) {
if (users->items[user_index].is_active) {
total++;
}
}
return total;
}
#define MAX_EMAIL 300
#define MAX_DATE 11
static int isLoanOverdue(const Loan *loan, int grace_days) {
char today[MAX_DATE];
char overdue_after[MAX_DATE];
getCurrentDate(today, sizeof(today));
shiftDate(loan->due_date, grace_days, overdue_after, sizeof(overdue_after));
return loan->is_active && strcmp(overdue_after, today) < 0;
}
void initLoans(LoanArray *loans) { loans->items=NULL; loans->count = 0; loans->capacity = 0; }
void freeLoans(LoanArray *loans)
{
free(loans->items); loans->items = NULL;
loans->count=0;
loans->capacity = 0;
}
void ensureLoanCapacity(LoanArray *loans) {
Loan *resized_items;
int resized_capacity;
if (loans->count < loans->capacity) {
return;
}
resized_capacity = loans->capacity == 0 ? 8 : loans->capacity * 2;
resized_items = (Loan *) realloc(loans->items, sizeof(Loan) * resized_capacity);
if (resized_items == NULL) {
fprintf(stderr, "Insufficient memory for loans.\n");
exit(EXIT_FAILURE);
}
loans->items = resized_items;
loans->capacity = resized_capacity;
}
int nextLoanId(const LoanArray *loans) {
int loan_index; int max_id=0;
for (loan_index = 0; loan_index < loans->count; loan_index++) { if (loans->items[loan_index].id > max_id) { max_id=loans->items[loan_index].id; } }
return max_id + 1;
}
Loan *findLoanById(LoanArray *loans, int id)
{
int loan_index;
for (loan_index = 0; loan_index < loans->count; loan_index++) { if (loans->items[loan_index].id==id) return &loans->items[loan_index]; }
return NULL;
}
void createLoan(BookArray *books, UserArray *users, LoanArray *loans) {
int book_id;
int user_id;
Book *book;
User *user;
Loan loan;
book_id = readInt("Book ID: ");
user_id = readInt("User ID: ");
book = findBookById(books, book_id);
user = findUserById(users, user_id);
if (book == NULL) {
printf("Book not found.\n");
return;
}
if (user == NULL) {
printf("User not found.\n");
return;
}
if (!book->is_active) {
printf("Book is deactivated.\n");
return;
}
if (!user->is_active) {
printf("User is deactivated.\n");
return;
}
if (book->available_quantity <= 0) {
printf("No copies are available.\n");
return;
}
if (countActiveLoansForUser(loans, user_id) >= user->max_active_loans) {
printf("User has reached the active-loan limit.\n");
return;
}
memset(&loan, 0, sizeof(loan));
loan.id = nextLoanId(loans);
loan.book_id = book_id;
loan.user_id = user_id;
getCurrentDate(loan.loan_date, sizeof(loan.loan_date));
shiftDate(loan.loan_date, 14, loan.due_date, sizeof(loan.due_date));
loan.return_date[0] = '\0';
loan.is_active = 1;
ensureLoanCapacity(loans);
loans->items[loans->count++] = loan;
book->available_quantity--;
book->total_loans++;
user->total_loans++;
printf("Loan created with ID %d. Due date: %s\n", loan.id, loan.due_date);
}
void returnBook(BookArray *books, LoanArray *loans) {
int id; Loan *loan; Book *book;
id=readInt("Loan ID to close: "); loan = findLoanById(loans, id);
if (loan == NULL) {
printf("Loan not found.\n");
return;
}
if (!loan->is_active) {
printf("Loan is already closed.\n");
return;
}
book = findBookById(books, loan->book_id);
if (book == NULL) {
printf("Loan references a missing book.\n");
return;
}
book->available_quantity++;
getCurrentDate(loan->return_date, sizeof(loan->return_date)); loan->is_active=0; printf("Loan closd on %s.\n", loan->return_date);
}
void printActiveLoans(const LoanArray *loans) {
int loan_index;
int found = 0;
for (loan_index = 0; loan_index < loans->count; loan_index++) {
const Loan *loan = &loans->items[loan_index];
if (!loan->is_active) {
continue;
}
printf("[%d] User %d | Book %d | %s -> %s\n",
loan->id,
loan->user_id,
loan->book_id,
loan->loan_date,
loan->due_date);
found = 1;
}
if (!found) {
printf("No active loan.\n");
}
}
void printOverdueLoans(const LoanArray *loans) {
int loan_index;
int found = 0;
for (loan_index = 0; loan_index < loans->count; loan_index++) {
const Loan *loan = &loans->items[loan_index];
if (!isLoanOverdue(loan, 0)) {
continue;
}
printf("[%d] User %d | Book %d | overdue on %s\n",
loan->id,
loan->user_id,
loan->book_id,
loan->due_date);
found = 1;
}
if (!found) {
printf("No overdue loan.\n");
}
}
void printUserLoanHistory(const LoanArray *loans, int user_id) {
int loan_index;
int found = 0;
for (loan_index = 0; loan_index < loans->count; loan_index++) {
const Loan *loan = &loans->items[loan_index];
if (loan->user_id != user_id) {
continue;
}
printf("[%d] Book %d | %s -> %s | Returned: %s | Status: %s\n",
loan->id,
loan->book_id,
loan->loan_date,
loan->due_date,
loan->return_date[0] == '\0' ? "-" : loan->return_date,
loan->is_active ? "active" : "closed");
found = 1;
}
if (!found) {
printf("No loans found for the srelected user.\n");
}
}
int countActiveLoans(const LoanArray *loans) {
int loan_index; int total=0;
for (loan_index = 0; loan_index < loans->count; loan_index++) if (loans->items[loan_index].is_active) total++;