-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookery.c
614 lines (520 loc) · 20.3 KB
/
bookery.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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <sqlite3.h>
#include <math.h>
#include <time.h>
#include <unistd.h>
#include <openssl/evp.h>
#include "lib/user.h"
void friendlyCLI();
/**
* @brief Initializes the database tables if they do not exist.
*
* @details This function initializes the books, users, and rents tables in the database if they do not already exist.
*
* @param None.
*
* @return An integer representing the status of the operation:
* - 0: If the operation was successful.
* - Non-zero: If an error occurred during initialization.
*/
int initializeDatabase() {
sqlite3 *db;
char *errMsg = 0;
int return_code;
// Open database connection.
return_code = sqlite3_open(DATABASE_FILE, &db);
if (return_code != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return return_code;
}
// SQL statement to create books table.
const char *sql_books = "CREATE TABLE IF NOT EXISTS books ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"title TEXT NOT NULL,"
"author TEXT NOT NULL,"
"genre TEXT,"
"price REAL,"
"quantity_available INTEGER,"
"quantity_rented INTEGER,"
"quantity_sold INTEGER,"
"quantity_rented_all INTEGER,"
"quantity_rented_days INTEGER"
");";
// Execute SQL statement to create books table.
return_code = sqlite3_exec(db, sql_books, 0, 0, &errMsg);
if (return_code != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", errMsg);
sqlite3_free(errMsg);
// return return_code;
}
// SQL statement to create users table.
const char *sql_users = "CREATE TABLE IF NOT EXISTS users ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"username TEXT NOT NULL,"
"password TEXT NOT NULL,"
"email TEXT NOT NULL,"
"role INTEGER NOT NULL"
");";
// Execute SQL statement to create users table.
return_code = sqlite3_exec(db, sql_users, 0, 0, &errMsg);
if (return_code != SQLITE_OK) {
fprintf(stderr, "SQL Error: %s\n", errMsg);
sqlite3_free(errMsg);
// return return_code;
}
// SQL statement to create rents table.
const char *sql_rents = "CREATE TABLE IF NOT EXISTS rents ("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"title TEXT NOT NULL,"
"Name TEXT NOT NULL,"
"Phone TEXT NOT NULL,"
"quantity_rented INTEGER,"
"rented_for_days INTEGER,"
"rent_date TEXT NOT NULL,"
"return_date TEXT"
");";
// Execute SQL statement to create rents table.
return_code = sqlite3_exec(db, sql_rents, 0, 0, &errMsg);
if (return_code != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", errMsg);
sqlite3_free(errMsg);
return return_code;
}
// Close database connection.
sqlite3_close(db);
return 0;
}
//***********************************************************************************************************************************
/**
*@brief Generate a sales report including top 5 books and total revenue.
*@param None.
*@return void.
*/
void generateSalesReport() {
sqlite3 *db; // SQLite database object.
sqlite3_stmt *stmt; // SQLite statement object.
int return_code; // Return code for SQLite functions.
// Open the database connection.
return_code = sqlite3_open(DATABASE_FILE, &db);
if (return_code) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return;
}
// Print header for the sales report.
printf("\n%s************ Sales Report ************%s\n\n",PINK,RESET);
printf("\n%s********* Top 5 Books *********%s\n",YELLOW,RESET);
// SQL query to retrieve top 5 books based on quantity sold.
const char *sql = "SELECT title, author, genre, price, quantity_available, quantity_rented, quantity_sold FROM books ORDER BY quantity_sold DESC LIMIT 5;";
const char *sql2 = "SELECT price, quantity_sold FROM books;"; // SQL query to retrieve price and quantity sold for all books.
return_code = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (return_code != SQLITE_OK) {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return;
}
// Calculate maximum widths for each column.
int max_title_width = 0;
int max_author_width = 0;
int max_genre_width = 0;
double max_price = 0.0;
int max_qty_sold = 0;
while ((return_code = sqlite3_step(stmt)) == SQLITE_ROW) {
max_title_width = fmax(max_title_width, (int)strlen((const char *)sqlite3_column_text(stmt, 0)));
max_author_width = fmax(max_author_width, (int)strlen((const char *)sqlite3_column_text(stmt, 1)));
max_genre_width = fmax(max_genre_width, (int)strlen((const char *)sqlite3_column_text(stmt, 2)));
max_price = fmax(max_price, sqlite3_column_double(stmt, 3));
max_qty_sold = fmax(max_qty_sold, sqlite3_column_int(stmt, 6));
}
// Print horizontal separator line.
printf("%s", BLUE);
for (int i = 0; i < (max_title_width + max_author_width + max_genre_width + 53); i++) {
printf("-");
}
printf("%s\n", RESET);
// Print column headers.
printf("%s%-*s | %-*s | %-*s | %-10s | %-13s | %-13s | %s\n",
BLUE,
max_title_width, "Title",
max_author_width, "Author",
max_genre_width, "Genre",
"Price",
"Quantity Sold",
"Revenue",
RESET);
// Print horizontal separator line.
printf("%s", BLUE);
for (int i = 0; i < (max_title_width + max_author_width + max_genre_width + 53); i++) {
printf("-");
}
printf("%s\n", RESET);
// Print sales report data.
sqlite3_reset(stmt); // Reset the statement to re-execute.
float totalRevenueTop_5 = 0;
while ((return_code = sqlite3_step(stmt)) == SQLITE_ROW) {
const char *title = (const char *)sqlite3_column_text(stmt, 0);
const char *author = (const char *)sqlite3_column_text(stmt, 1);
const char *genre = (const char *)sqlite3_column_text(stmt, 2);
double price = sqlite3_column_double(stmt, 3);
int quantitySold = sqlite3_column_int(stmt, 6);
float revenue = price * quantitySold;
printf("%-*s | %-*s | %-*s | $%-9.2f | %-13d | $%-12.2f |\n",
max_title_width, title,
max_author_width, author,
max_genre_width, genre,
price,
quantitySold,
revenue);
totalRevenueTop_5 += revenue;
for (int i = 0; i < (max_title_width + max_author_width + max_genre_width + 53); i++) {
printf("-");
}
printf("\n");
}
float totalRevenue = 0;
// Retrieve total revenue for all books.
return_code = sqlite3_prepare_v2(db, sql2, -1, &stmt, 0);
if (return_code != SQLITE_OK) {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return;
}
while ((return_code = sqlite3_step(stmt)) == SQLITE_ROW) {
totalRevenue+= sqlite3_column_double(stmt, 0) * sqlite3_column_int(stmt, 1);
}
// Print total revenue.
printf("\n%s*********** Revenue ***********%s\n\n",YELLOW,RESET);
printf("Total Revenue of Top 5: %s$%.2f%s\n",GREEN, totalRevenueTop_5,RESET);
printf("Total Revenue of All: %s$%.2f%s\n\n",GREEN, totalRevenue,RESET);
sqlite3_finalize(stmt); // Finalize the statement.
sqlite3_close(db); // Close the database connection.
}
/**
@brief Generate a rental report including top 5 rented books.
@param None.
@return void.
*/
void generateRentalReport() {
sqlite3 *db; // SQLite database object.
sqlite3_stmt *stmt; // SQLite statement object.
int return_code; // Return code for SQLite functions.
// Open the database connection.
return_code = sqlite3_open(DATABASE_FILE, &db);
if (return_code) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return;
}
// Print header for the rental report.
printf("\n%s*********** Rental Report ************%s\n\n",PINK,RESET);
printf("\n%s******* Top 5 Rented Books *********%s\n",YELLOW,RESET);
// SQL query to retrieve top 5 rented books based on total quantity rented.
const char *sql = "SELECT title, author, genre, quantity_rented_all, quantity_rented_days FROM books ORDER BY quantity_rented_all DESC LIMIT 5;";
// Prepare the SQL statement.
return_code = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
if (return_code != SQLITE_OK) {
fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return;
}
// Calculate maximum widths for each column.
int max_title_width = 0;
int max_author_width = 0;
int max_genre_width = 0;
int max_qty_rented = 0;
// Iterate over the result set to find maximum widths.
while ((return_code = sqlite3_step(stmt)) == SQLITE_ROW) {
max_title_width = fmax(max_title_width, (int)strlen((const char *)sqlite3_column_text(stmt, 0)));
max_author_width = fmax(max_author_width, (int)strlen((const char *)sqlite3_column_text(stmt, 1)));
max_genre_width = fmax(max_genre_width, (int)strlen((const char *)sqlite3_column_text(stmt, 2)));
max_qty_rented = fmax(max_qty_rented, sqlite3_column_double(stmt, 5));
}
// Print horizontal separator line.
printf("%s", BLUE);
for (int i = 0; i < (max_title_width + max_author_width + max_genre_width + 53); i++) {
printf("-");
}
printf("%s\n", RESET);
// Print column headers.
printf("%s%-*s | %-*s | %-*s | %-13s | %-14s |%s\n",
BLUE,
max_title_width, "Title",
max_author_width, "Author",
max_genre_width, "Genre",
"Quantity Rented All",
"Quantity Rented Days",
RESET);
// Print horizontal separator line.
printf("%s", BLUE);
for (int i = 0; i < (max_title_width + max_author_width + max_genre_width + 53); i++) {
printf("-");
}
printf("%s\n", RESET);
float totalRevenue;
// Print rental report data.
sqlite3_reset(stmt); // Reset the statement to re-execute.
while ((return_code = sqlite3_step(stmt)) == SQLITE_ROW) {
const char *title = (const char *)sqlite3_column_text(stmt, 0);
const char *author = (const char *)sqlite3_column_text(stmt, 1);
const char *genre = (const char *)sqlite3_column_text(stmt, 2);
int quantityRentedAll = sqlite3_column_int(stmt, 3);
int quantityRentedDays = sqlite3_column_int(stmt, 4);
printf("%-*s | %-*s | %-*s | %-19d | %-20d |\n",
max_title_width, title,
max_author_width, author,
max_genre_width, genre,
quantityRentedAll,
quantityRentedDays);
totalRevenue+=quantityRentedDays;
for (int i = 0; i < (max_title_width + max_author_width + max_genre_width + 53); i++) {
printf("-");
}
printf("\n");
}
// Print total revenue.
printf("\n%s*********** Revenue ***********%s\n\n",YELLOW,RESET);
printf("Total Revenue of All: %s$%.2f%s\n\n",GREEN, totalRevenue,RESET);
sqlite3_finalize(stmt); // Finalize the statement.
sqlite3_close(db); // Close the database connection.
}
void advancedCLI() {
printf("\033c");
char command[100];
do {
printf("bms-> ");
fgets(command, sizeof(command), stdin);
// Remove newline character if present.
if (strlen(command) > 0 && command[strlen(command) - 1] == '\n') {
command[strlen(command) - 1] = '\0';
}
if (strcmp(command, "add user") == 0) {
// Call function to add user.
addUser();
} else if (strcmp(command, "add book") == 0) {
// Call function to add book.
addBook();
} else if (strcmp(command, "login") == 0) {
// Call function to login.
authenticateUser();
} else if (strcmp(command, "update book") == 0) {
// Call function to update book.
updateBook();
} else if (strcmp(command, "update user") == 0) {
// Call function to update user.
updateUser();
} else if (strcmp(command, "sell book") == 0) {
// Call function to sell book.
sellBook();
} else if (strcmp(command, "del user") == 0) {
// Call function to delete user.
delUser();
} else if (strcmp(command, "del book") == 0) {
// Call function to delete book.
delBook(1);
} else if (strcmp(command, "del allbooks") == 0) {
// Call function to delete all books.
delBook(0);
} else if (strcmp(command, "rent book") == 0){
// Call function to rent books.
rentBook();
} else if (strcmp(command, "rent recall") == 0){
// Call function to recall rented books.
rentRecall();
} else if (strcmp(command, "rent late") == 0){
// Call function to recall rented books.
rentLate();
}
else if (strcmp(command, "show rents") == 0){
// Call function to display rents.
displayRent();
} else if (strcmp(command, "show books") == 0) {
// Call function to display all books.
displayBooks();
} else if (strcmp(command, "show users") == 0) {
// Call function to display all users.
displayUsers();
} else if (strcmp(command, "search rent") == 0){
// Call function to search rented books.
searchRent();
} else if (strcmp(command, "search book") == 0) {
// Call function to search for books.
searchBook();
} else if (strcmp(command, "report sales") == 0) {
// Call function to generate sales report.
generateSalesReport();
} else if (strcmp(command, "report rents") == 0) {
// Call function to generate rental report.
generateRentalReport();
} else if (strcmp(command, "whoami") == 0) {
// Call function to display current user information.
whoami();
} else if (strcmp(command, "help add") == 0) {
// Display help for add commands.
help("add");
} else if (strcmp(command, "help del") == 0) {
// Display help for del commands.
help("del");
} else if (strcmp(command, "help show") == 0) {
// Display help for show commands.
help("show");
} else if (strcmp(command, "help login") == 0) {
// Display help for login command.
help("login");
} else if (strcmp(command, "help search") == 0) {
// Display help for search commands.
help("search");
} else if (strcmp(command, "help update") == 0) {
// Display help for update commands.
help("update");
} else if (strcmp(command, "help sell") == 0) {
// Display help for sell command.
help("sell");
} else if (strcmp(command, "help rent") == 0) {
// Display help for rent command.
help("rent");
} else if (strcmp(command, "help report") == 0) {
// Display help for rent command.
help("report");
} else if (strcmp(command, "help whoami") == 0) {
// Display help for rent command.
help("whoami");
} else if (strcmp(command, "help clear") == 0) {
// Display help for rent command.
help("clear");
}
else if (strcmp(command, "search") == 0) {
// Display help for search command.
help("search");
} else if (strcmp(command, "update") == 0) {
// Display help for update command.
help("update");
} else if (strcmp(command, "rent") == 0) {
// Display help for rent command.
help("rent");
} else if (strcmp(command, "del") == 0) {
// Display help for del command.
help("del");
} else if (strcmp(command, "add") == 0) {
// Display help for add command.
help("add");
} else if (strcmp(command, "show") == 0) {
// Display help for show command.
help("show");
} else if (strcmp(command, "sell") == 0) {
// Display help for sell command.
help("sell");
} else if (strcmp(command, "report") == 0) {
// Display help for report command.
help("report");
}
else if (strcmp(command, "help") == 0) {
// Display general help.
help("all");
} else if (strcmp(command, "back") == 0) {
// Return to the friendly CLI.
friendlyCLI();
} else if (strcmp(command, "clear") == 0) {
// Clear the screen and reset the advanced CLI.
advancedCLI();
} else if (strcmp(command, "exit") == 0) {
// Clear the screen and reset the advanced CLI.
printf("\n\nbye!\n");
exit(0);
}
else {
// Invalid command.
if (strlen(command) > 0) {
printf("%sInvalid command:%s %s\n",RED,RESET,command);
}
}
} while (true);
}
/**
* @brief Main function for the friendly command-line interface (CLI) of the book management system.
*
* This function provides a user-friendly interface for interacting with the book management system.
* Users can perform various operations such as adding, displaying, updating, selling, and renting books,
* as well as generating sales and rental reports.
*/
void friendlyCLI() {
printf("\033c"); // Clear the screen.
// Initialize database.
if (initializeDatabase() != 0) {
fprintf(stderr, "Failed to initialize database.\n");
}
int choice;
bool validInput;
do {
validInput = false;
printMenu(); // Display menu options.
// Validate input to ensure it is a number.
while (!validInput) {
printf("bms-> ");
if (scanf("%d", &choice) != 1) {
printf("%sInvalid input.%s\n", RED, RESET);
clearInputBuffer();
} else {
validInput = true;
}
}
switch(choice) {
case 1:
addBook();
break;
case 2:
displayBooks();
break;
case 3:
searchBook();
break;
case 4:
updateBook();
break;
case 5:
sellBook();
break;
case 6:
generateSalesReport();
break;
case 7:
rentBook();
break;
case 8:
rentRecall();
break;
case 9:
displayRent();
break;
case 10:
rentLate();
break;
case 11:
searchRent();
break;
case 12:
generateRentalReport();
break;
case 13:
advancedCLI();
break;
case 0:
printf("Exiting program. Goodbye!\n");
exit(0);
break;
default:
printf("%sInvalid number.%s\n", RED, RESET);
}
} while(choice != 0);
}
int bms() {
login();
friendlyCLI();
}
int main(){
bms();
return 0;
}