-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBookManager.c
485 lines (416 loc) · 11.8 KB
/
BookManager.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
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "BookManager.h"
#include "LoanManager.h"
BookManager* initBookManager()
{
BookManager* manager = (BookManager*)malloc(sizeof(BookManager));
if (!manager)
{
return NULL;
}
manager->bookPtrArr = NULL;
manager->count = 0;
return manager;
}
Book* initBook(BookManager* manager)
{
Book* newBook = (Book*)malloc(sizeof(Book));
if (!newBook)
{
return NULL;
}
printf("Please enter book name: ");
newBook->name = getStr();
if (!strcmp(newBook->name, ""))
{
return NULL;
}
printf("Please enter book genre: \n");
newBook->genre = getGenre();
getchar();
printf("Please enter number of copies: ");
scanf_s("%d", &newBook->copiesAvailable);
getchar();
newBook->totalCopies = newBook->copiesAvailable;
newBook->author = initAuthor(manager);
insert(newBook->author, initAuthorBook(newBook));
return newBook;
}
int freeBook(Book* title)
{
free(title);
return 1;
}
int addNewBook(BookManager* manager)
{
printf("\n========================================== Book adding ==========================================\n\n");
manager->bookPtrArr = (Book**)realloc(manager->bookPtrArr, sizeof(Book*) * (manager->count + 1));
if (!manager->bookPtrArr)
{
handleError("failed to add book");
return 0;
}
Book* add = initBook(manager);
if (!add)
{
handleError("failed to add book");
return 0;
}
for (size_t i = 0; i < manager->count; i++)
{
if (compareBookByName(&add, &manager->bookPtrArr[i]) == 0)
{
handleError("Book already exists! \n\n");
return 0;
}
}
manager->bookPtrArr[manager->count] = add;
manager->count++;
printf("\n===================================== Book added succesfully! ===================================\n\n");
return 1;
}
Genre getGenre()
{
do
{
printf("[1] - Science fiction\n");
printf("[2] - Mystery\n");
printf("[3] - Fantasy\n");
printf("[4] - Romance\n");
printf("[5] - Historical fiction\n");
printf("Your answer: ");
int choice;
scanf("%d", &choice);
switch (choice)
{
case SCIENCE_FICTION:
return SCIENCE_FICTION;
case MYSTERY:
return MYSTERY;
case FANTASY:
return FANTASY;
case ROMANCE:
return ROMANCE;
case HISTORICAL_FICTION:
return HISTORICAL_FICTION;
default:
handleError("Not valid choice! Try again.");
break;
}
} while (1);
}
int removeBook(BookManager* bookManager, LoanManager* loanManager)
{
printf("\n========================================== Book removing ========================================\n\n");
if (!printBookArr(bookManager->bookPtrArr, bookManager->count))
return 0;
printf("Please enter name of the book you want to remove: ");
char* bookName = getStr();
for (int i = 0; i < bookManager->count; i++)
{
if (isInLoanList(loanManager, bookName))
{
handleError("Book is loaned by one of the members");
return 0;
}
if (strcmp(bookName, bookManager->bookPtrArr[i]->name) == 0)
{
swap(bookManager->bookPtrArr[i], bookManager->bookPtrArr[bookManager->count - 1]);
freeBook(bookManager->bookPtrArr[bookManager->count - 1]);
bookManager->count--;
bookManager->bookPtrArr = (Book**)realloc(bookManager->bookPtrArr, bookManager->count * sizeof(Book*));
printf("\n========================================= Book removed! =========================================\n\n");
return 1;
}
}
printf("\n====================================== Failed to remove book ====================================\n\n");
return 0;
}
int printBookArr(const Book** bookPtrArr, int count)
{
if (count == 0)
{
handleError("No books in the system!");
return 0;
}
printf("# |Book name |Genre |Author |Available\n");
generalArrayFunction(bookPtrArr, count, printBook);
return 1;
}
void printBook(const Book* title)
{
if (!title)
{
return;
}
printf("%-20s|%-20d|%-20s|%d/%d\n", title->name, title->genre, title->author->name, title->copiesAvailable, title->totalCopies);
}
void swap(Book* bookA, Book* bookB) {
Book temp = *bookA;
*bookA = *bookB;
*bookB = temp;
}
void sortBooks(BookManager* manager) {
int choice;
printf("\n[1] - Sort books by name\n");
printf("[2] - Sort books by genre\n");
printf("[3] - Sort books by author\n");
printf("Please enter your choice: ");
scanf_s("%d", &choice);
printf("\n");
switch (choice) {
case 1:
qsort(manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByName);
break;
case 2:
qsort(manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByGenre);
break;
case 3:
qsort(manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByAuthor);
break;
default:
handleError("Invalid choice!");
break;
}
printBookArr(manager->bookPtrArr, manager->count);
}
Book* searchBook(BookManager* manager)
{
Book* temp = (Book*)malloc(sizeof(Book));
if (!temp)
{
handleError("Memory allocation failed for temp!");
return NULL;
}
temp->author = (Author*)malloc(sizeof(Author));
if (!temp->author)
{
free(temp);
handleError("Memory allocation failed for author!");
return NULL;
}
int choice;
Book** res = NULL;
printf("\n[1] - Search book by name\n");
printf("[2] - Search book by genre\n");
printf("[3] - Search book by author\n");
printf("Please enter your choice: ");
scanf_s("%d", &choice);
getchar();
switch (choice)
{
case 1:
printf("Please enter the name of the book: ");
temp->name = getStr();
qsort(manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByName);
res = (Book**)bsearch(&temp, manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByName);
break;
case 2:
printf("Please enter the genre of the book: \n");
temp->genre = getGenre();
getchar();
qsort(manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByGenre);
res = (Book**)bsearch(&temp, manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByGenre);
break;
case 3:
printf("Please enter the author of the book: ");
temp->author->name = getStr();
qsort(manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByAuthor);
res = (Book**)bsearch(&temp, manager->bookPtrArr, manager->count, sizeof(Book*), compareBookByAuthor);
break;
default:
handleError("Invalid choice!");
free(temp->name);
free(temp->author);
free(temp);
return NULL;
}
if (res)
{
printf("\nBook found!\n");
printf("Book name |Genre |Author |Available\n");
printBook(*res);
// Clean up the temporary Book object
free(temp->author);
free(temp);
return *res;
}
handleError("Book not found!");
return NULL;
}
int compareBookByName(const void* a, const void* b) {
Book* bookA = *(Book**)a;
Book* bookB = *(Book**)b;
return strcmp(bookA->name, bookB->name);
}
int compareBookByGenre(const void* a, const void* b) {
Book* bookA = *(Book**)a;
Book* bookB = *(Book**)b;
return bookA->genre - bookB->genre;
}
int compareBookByAuthor(const void* a, const void* b)
{
Book* bookA = *(Book**)a;
Book* bookB = *(Book**)b;
return strcmp(bookA->author->name, bookB->author->name);
}
Book* getBookByTitle(BookManager* manager, char* bookTitle)
{
for (int i = 0; i < manager->count; i++)
{
if (!strcmp(manager->bookPtrArr[i]->name, bookTitle))
{
return manager->bookPtrArr[i];
}
}
return NULL;
}
void printPopularBooks(BookManager* manager)
{
printf("\n=========================================== popular books =======================================\n\n");
int isPrined = 0;
printf("Book name |Genre |Author |Available\n");
for (int i = 0; i < manager->count; i++)
{
if (ISPOPULARBOOK(manager->bookPtrArr[i]->totalCopies, manager->bookPtrArr[i]->copiesAvailable))
{
printBook(manager->bookPtrArr[i]);
isPrined = 1;
}
}
if (!isPrined)
{
handleError("Not enough books loaned to detemine popular books\n");
}
printf("\n=================================================================================================\n");
}
int writeBookManagerToText(FILE* file, BookManager* manager) {
Book** arr = manager->bookPtrArr;
fprintf(file, "%d\n", manager->count);
fprintf(file, "Book name |Genre |Author |Available | Total Copies\n");
for (size_t i = 0; i < manager->count; i++) {
fprintf(file, "%s\n%d\n%s\n%d\n%d\n", arr[i]->name, arr[i]->genre, arr[i]->author->name, arr[i]->copiesAvailable, arr[i]->totalCopies);
}
return 1;
}
int readBookManagerFromText(FILE* file, BookManager* manager) {
int count = 0;
fscanf(file, "%d", &count);
manager->bookPtrArr = (Book**)malloc(count * sizeof(Book*));
if (!manager->bookPtrArr) {
return 0;
}
manager->count = count;
char buffer[256];
fgets(buffer, sizeof(buffer), file);
fgets(buffer, sizeof(buffer), file);
for (size_t i = 0; i < count; i++) {
Book* temp = (Book*)malloc(sizeof(Book));
if (!temp) {
return 0;
}
temp->author = (Author*)malloc(sizeof(Author));
if (!temp->author) {
free(temp);
return 0;
}
temp->author->headBook = NULL;
char name[256];
char authorName[256];
int genre, copiesAvailable, totalCopies;
fgets(name, 256, file);
name[strcspn(name, "\n")] = 0;
fscanf(file, "%d", &genre);
fgetc(file);
fgets(authorName, 256, file);
authorName[strcspn(authorName, "\n")] = 0;
fscanf(file, "%d%d", &copiesAvailable, &totalCopies);
fgetc(file);
temp->name = (char*)malloc(strlen(name) + 1);
if (temp->name) {
strcpy(temp->name, name);
}
temp->author->name = (char*)malloc(strlen(authorName) + 1);
if (temp->author->name) {
strcpy(temp->author->name, authorName);
}
temp->genre = genre;
temp->copiesAvailable = copiesAvailable;
temp->totalCopies = totalCopies;
insert(temp->author, initAuthorBook(temp));
manager->bookPtrArr[i] = temp;
}
return 1;
}
int writeBookManagerToBinary(FILE* file, BookManager* manager)
{
fwrite(&manager->count, sizeof(int), 1, file);
for (int i = 0; i < manager->count; i++)
{
Book* book = manager->bookPtrArr[i];
fwrite(&book->genre, sizeof(int), 1, file);
fwrite(&book->copiesAvailable, sizeof(int), 1, file);
fwrite(&book->totalCopies, sizeof(int), 1, file);
int nameLength = (int)strlen(book->name);
fwrite(&nameLength, sizeof(int), 1, file);
fwrite(book->name, sizeof(char), nameLength, file);
int authorNameLength = (int)strlen(book->author->name);
fwrite(&authorNameLength, sizeof(int), 1, file);
fwrite(book->author->name, sizeof(char), authorNameLength, file);
}
return 1;
}
int readBookManagerFromBinary(FILE* file, BookManager* manager)
{
int count = 0;
fread(&count, sizeof(int), 1, file);
manager->bookPtrArr = (Book**)malloc(count * sizeof(Book*));
if (!manager->bookPtrArr) {
return 0;
}
manager->count = count;
for (int i = 0; i < count; i++)
{
Book* tempBook = (Book*)malloc(sizeof(Book));
if (!tempBook) {
return 0;
}
fread(&tempBook->genre, sizeof(int), 1, file);
fread(&tempBook->copiesAvailable, sizeof(int), 1, file);
fread(&tempBook->totalCopies, sizeof(int), 1, file);
int nameLength = 0;
fread(&nameLength, sizeof(int), 1, file);
tempBook->name = (char*)malloc(nameLength + 1);
if (!tempBook->name) {
free(tempBook);
return 0;
}
fread(tempBook->name, sizeof(char), nameLength, file);
tempBook->name[nameLength] = '\0';
tempBook->author = (Author*)malloc(sizeof(Author));
if (!tempBook->author) {
free(tempBook->name);
free(tempBook);
return 0;
}
int authorNameLength = 0;
fread(&authorNameLength, sizeof(int), 1, file);
tempBook->author->name = (char*)malloc(authorNameLength + 1);
if (!tempBook->author->name) {
free(tempBook->author);
free(tempBook->name);
free(tempBook);
return 0;
}
fread(tempBook->author->name, sizeof(char), authorNameLength, file);
tempBook->author->name[authorNameLength] = '\0';
tempBook->author->headBook = NULL;
insert(tempBook->author, initAuthorBook(tempBook));
manager->bookPtrArr[i] = tempBook;
}
return 1;
}