forked from tmont004/Virtual-Library-Management-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystem.cpp
More file actions
693 lines (584 loc) · 22.4 KB
/
Copy pathLibrarySystem.cpp
File metadata and controls
693 lines (584 loc) · 22.4 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
#include "LibrarySystem.h"
#include <fstream>
#include <iostream>
#include <iomanip>
#include <limits>
#include "json.hpp"
#include "globals.h"
#include <csignal> // For signal handling
#include <cstdlib> // For exit()
using namespace std;
using json = nlohmann::json;
string bookDB = "bookDB.json";
extern std::string bookDB;
string borrowedBooksFilename = "borrowedBooksDB.json";
string queueFilename = "bookWaitlistDB.json";
// Global flag to indicate if termination signal received
volatile sig_atomic_t terminationSignalReceived = 0;
// Constructor to initialize the userInfo
LibrarySystem::LibrarySystem(const UserInfo& userInfo) : userInfo(userInfo) {}
// Method to run the library system
void LibrarySystem::run() {
bookDatabase.loadFromFile(bookDB); // Load data from files
userOrAdminMenu(); // Display menu after successful login
bookDatabase.saveToFile(bookDB); // Save data to files
}
// Method to display the user or admin menu after login
void LibrarySystem::userOrAdminMenu() {
// Get the username and admin status from the UserInfo instance
string userName = userInfo.getUsername();
bool isAdmin = userInfo.isAdmin();
// Check if the user is an admin or a regular user
if (isAdmin) {
adminMenu();
} else {
userMenu(userName);
}
}
void LibrarySystem::userMenu(const string& userName) {
int choice;
do {
clearScreen();
cout << "Welcome, " << " User: " << userName << endl;
cout << "===============================" << endl;
cout << "User Menu" << endl;
cout << "1. Search for Books" << endl;
cout << "2. Borrow a Book" << endl;
cout << "3. Return a Book" << endl;
cout << "4. View Borrowed Books" << endl;
cout << "5. Add a Book" << endl;
cout << "6. View Library" << endl;
cout << "7. Logout" << endl;
cout << "===============================" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
searchBooks();
pressEnterToContinue();
break;
case 2:
interactiveBorrowBook();
pressEnterToContinue();
break;
case 3:
returnBook();
pressEnterToContinue();
break;
case 4:
clearScreen();
viewBorrowedBooks();
pressEnterToContinue();
break;
case 5:
addBook();
pressEnterToContinue();
break;
case 6:
viewLibrary();
pressEnterToContinue();
break;
case 7:
cout << "Logged out." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
} while (choice != 7);
}
void LibrarySystem::adminMenu() {
int choice;
do {
clearScreen();
cout << "Welcome, " << endl; //<< " Admin: " << userName << endl;
cout << "===============================" << endl;
cout << "1. Add a Book" << endl;
cout << "2. Remove a Book" << endl;
cout << "3. Update Book Information" << endl;
cout << "4. Logout" << endl;
cout << "===============================" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addBook();
pressEnterToContinue();
break;
case 2:
removeBook();
pressEnterToContinue();
break;
case 3:
updateBook();
pressEnterToContinue();
break;
case 4:
cout << "Logged out." << endl;
pressEnterToContinue();
break;
default:
cout << "Invalid choice. Please try again." << endl;
pressEnterToContinue();
break;
}
} while (choice != 4);
}
void LibrarySystem::searchBooks() {
int searchChoice;
clearScreen();
cout << "Search Options:" << endl;
cout << "===============================" << endl;
cout << "1. Search by Title" << endl;
cout << "2. Search by Author" << endl;
cout << "3. Search by ISBN" << endl;
cout << "4. Back to Menu" << endl;
cout << "===============================" << endl;
cout << "Enter your choice: ";
cin >> searchChoice;
switch (searchChoice) {
case 1: {
string title;
cin.ignore(); // Clear newline character
cout << "Enter title to search: ";
getline(cin, title);
try {
Book foundBook = bookDatabase.findBookByTitle(title);
if (!foundBook.getTitle().empty()) {
cout << "Book found:" << endl;
foundBook.print(); // Assuming print() method prints book details
// Check if book is available
if (foundBook.getCopiesInStock() > 0) {
cout << "This book is available." << endl;
char borrowChoice;
cout << "Do you want to borrow this book? (y/n): ";
cin >> borrowChoice;
if (borrowChoice == 'y' || borrowChoice == 'Y') {
borrowBook(foundBook);
}
} else {
cout << "This book is currently not available." << endl;
}
} else {
cout << "Book with title '" << title << "' not found." << endl;
}
} catch (const runtime_error& e) {
cerr << "Error: " << e.what() << endl;
}
break;
}
case 2: {
string author;
cin.ignore(); // Clear newline character
cout << "Enter author to search: ";
getline(cin, author);
cout << "Yet to be Implemented" << endl;
// Implement searching books by author
// Iterate through database or use appropriate method in BookDatabase
// Example: bookDatabase.findBooksByAuthor(author);
break;
}
case 3: {
string isbn;
cout << "Enter ISBN to search: ";
cin >> isbn;
try {
Book foundBook = bookDatabase.getBookByISBN(isbn);
if (!foundBook.getTitle().empty()) {
cout << "Book found:" << endl;
foundBook.print(); // Assuming print() method prints book details
// Check if book is available
if (foundBook.getCopiesInStock() > 0) {
cout << "This book is available." << endl;
char borrowChoice;
cout << "Do you want to borrow this book? (y/n): ";
cin >> borrowChoice;
if (borrowChoice == 'y' || borrowChoice == 'Y') {
borrowBook(foundBook);
}
} else {
cout << "This book is currently not available." << endl;
}
} else {
cout << "Book with ISBN '" << isbn << "' not found." << endl;
}
} catch (const runtime_error& e) {
cerr << "Error: " << e.what() << endl;
}
break;
}
case 4:
// Return to previous menu
return;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
}
void LibrarySystem::returnBook() {
clearScreen();
string isbn;
cout << "Enter the ISBN of the book to return: ";
cin >> isbn;
// Find the book in the book database
Book book;
try {
book = bookDatabase.getBookByISBN(isbn);
} catch (const runtime_error& e) {
cerr << "Error: " << e.what() << endl;
return;
}
// Check if the book was found
if (book.getISBN().empty()) {
return;
}
// Increase the number of copies in stock
try {
bookDatabase.updateBook(isbn, book.getTitle(), book.getAuthor(), book.getCopiesInStock() + 1);
cout << "Book with ISBN '" << isbn << "' returned successfully." << endl;
// Remove the book from the user's borrowed books list
string username = userInfo.getUsername();
json borrowedBooks;
json bookWaitlist;
ifstream inFile(borrowedBooksFilename);
if (inFile.is_open()) {
inFile >> borrowedBooks;
inFile.close();
if (borrowedBooks.contains(username)) {
auto& booksList = borrowedBooks[username];
booksList.erase(std::remove(booksList.begin(), booksList.end(), isbn), booksList.end());
// Save the updated borrowed books JSON file
ofstream outFile(borrowedBooksFilename);
if (outFile.is_open()) {
outFile << borrowedBooks.dump(4); // Pretty print with 4 spaces
outFile.close();
} else {
cerr << "Failed to open file for writing: " << borrowedBooksFilename << endl;
}
} else {
cerr << "Error: User '" << username << "' not found in borrowed books database." << endl;
}
} else {
cerr << "Failed to open file for reading: " << borrowedBooksFilename << endl;
}
//Update the book's waitlist
ifstream queueFile(queueFilename);
if (queueFile.is_open()) {
queueFile >> bookWaitlist;
queueFile.close();
} else {
throw runtime_error("Failed to open file for reading: " + borrowedBooksFilename);
}
if (!bookWaitlist[book.getISBN()].empty()) { //If there are users in the queue
//Get the username of the next user in the waitlist
auto nextInLine = bookWaitlist[book.getISBN()].at(0);
bookWaitlist[book.getISBN()].erase(0);
// Save the updated waitlist JSON file
ofstream waitlistFile(queueFilename);
if (waitlistFile.is_open()) {
waitlistFile << bookWaitlist.dump(4); // Pretty print with 4 spaces
waitlistFile.close();
} else {
cerr << "Failed to open file for writing: " << borrowedBooksFilename << endl;
}
//Borrow the book for the user specified by nextInLine
// Decrease the number of copies in stock
bookDatabase.updateBook(book.getISBN(), book.getTitle(), book.getAuthor(), book.getCopiesInStock() - 1);
// Load the borrowed books JSON file
ifstream inFile(borrowedBooksFilename);
if (inFile.is_open()) {
inFile >> borrowedBooks;
inFile.close();
} else {
throw runtime_error("Failed to open file for reading: " + borrowedBooksFilename);
}
// Add the book to the next in line user's borrowed books list
borrowedBooks[nextInLine].push_back(book.getISBN());
// Save the updated borrowed books JSON file
ofstream outFile(borrowedBooksFilename);
if (outFile.is_open()) {
outFile << borrowedBooks.dump(4); // Pretty print with 4 spaces
outFile.close();
} else {
throw runtime_error("Failed to open file for writing: " + borrowedBooksFilename);
}
} else {
cerr << "Failed to open file for reading: " << borrowedBooksFilename << endl;
}
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
}
void LibrarySystem::addBook() {
cout << "Enter book information\n";
string title, author, isbn;
int copiesInStock;
cin.ignore(); // To clear the newline character left by previous input
cout << "Enter book title: ";
getline(cin, title);
cout << "Enter author: ";
getline(cin, author);
cout << "Enter ISBN: ";
getline(cin, isbn);
cout << "Enter number of copies: ";
cin >> copiesInStock;
Book newBook(title, author, isbn, copiesInStock); //this is a book object, we have to reference this object
if (bookDatabase.isBookAvailable(isbn)) {
cout << "Book with this ISBN already exists.\n";
} else {
bookDatabase.addBook(newBook);
//Add the book's ISBN to the waitlist as a new entry
try{
json bookWaitlist;
ifstream queueFile(queueFilename);
if (queueFile.is_open()) {
queueFile >> bookWaitlist;
queueFile.close();
} else {
throw runtime_error("Failed to open file for reading: " + borrowedBooksFilename);
}
//Add the ISBN to the end of the list
bookWaitlist.push_back({isbn, {""}});
bookWaitlist[isbn].erase(0);
// Save the updated waitlist JSON file
ofstream waitlistFile(queueFilename);
if (waitlistFile.is_open()) {
waitlistFile << bookWaitlist.dump(4); // Pretty print with 4 spaces
waitlistFile.close();
} else {
cerr << "Failed to open file for writing: " << borrowedBooksFilename << endl;
}
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
cout << "Book added successfully.\n";
}
}
void LibrarySystem::borrowBook(const Book& book) {
// Find the book in the book database
// Borrow the book logic here
// Update book copies in stock, save borrowed books to file, etc.
try {
// Decrease the number of copies in stock
bookDatabase.updateBook(book.getISBN(), book.getTitle(), book.getAuthor(), book.getCopiesInStock() - 1);
// Load the borrowed books JSON file
json borrowedBooks;
ifstream inFile(borrowedBooksFilename);
if (inFile.is_open()) {
inFile >> borrowedBooks;
inFile.close();
} else {
throw runtime_error("Failed to open file for reading: " + borrowedBooksFilename);
}
// Get the username
string username = userInfo.getUsername();
// Add the book to the user's borrowed books list
borrowedBooks[username].push_back(book.getISBN());
// Save the updated borrowed books JSON file
ofstream outFile(borrowedBooksFilename);
if (outFile.is_open()) {
outFile << borrowedBooks.dump(4); // Pretty print with 4 spaces
outFile.close();
} else {
throw runtime_error("Failed to open file for writing: " + borrowedBooksFilename);
}
cout << "Book '" << book.getTitle() << "' borrowed successfully." << endl;
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
}
void LibrarySystem::interactiveBorrowBook() {
string bookTitle;
cout << "Enter the title of the book to borrow: ";
cin.ignore();
getline(cin, bookTitle);
cout << "Borrowing book: " << bookTitle << endl;
// Find the book in the book database
Book book;
try {
book = bookDatabase.findBookByTitle(bookTitle);
} catch (const runtime_error& e) {
cerr << "Error: " << e.what() << endl;
return;
}
// Check if the book was found
if (book.getTitle().empty()) {
cout << "Error: Book with title '" << bookTitle << "' not found." << endl;
return;
}
// Check if there are copies available
if (book.getCopiesInStock() <= 0) { //If all copies are already borrowed
cout << "Error: No copies available for book '" << bookTitle << "'." << endl;
try {
// Load the FIFO Queue JSON file
json bookWaitlist;
ifstream queueFile(queueFilename);
if (queueFile.is_open()) {
queueFile >> bookWaitlist; //Possibly change to input values into the queue member variable
queueFile.close();
} else {
throw runtime_error("Failed to open file for reading: " + queueFilename);
}
// Get the username
string username = userInfo.getUsername();
// Add the user to the book's waitlist
bookWaitlist[book.getISBN()].push_back(username);
// Save the updated waitlist JSON file
ofstream waitlistFile(queueFilename);
if (waitlistFile.is_open()) {
waitlistFile << bookWaitlist.dump(4); // Pretty print with 4 spaces
waitlistFile.close();
} else {
throw runtime_error("Failed to open file for writing: " + queueFilename);
}
cout << "Added to waitlist queue for '" << bookTitle << ".'" << endl;
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
}
else{
borrowBook(book);
}
return;
}
void LibrarySystem::viewBorrowedBooks() const {
string username = userInfo.getUsername();
// Load the borrowed books JSON file
json borrowedBooks;
ifstream inFile(borrowedBooksFilename);
if (inFile.is_open()) {
inFile >> borrowedBooks;
inFile.close();
} else {
cerr << "Failed to open file for reading: " << borrowedBooksFilename << endl;
return;
}
// Check if user has borrowed any books
if (borrowedBooks.contains(username)) {
cout << "Borrowed Books for User: " << username << endl;
cout << "===============================" << endl;
// Iterate through borrowed books
for (const auto& isbn : borrowedBooks[username]) {
try {
Book borrowedBook = bookDatabase.getBookByISBN(isbn);
borrowedBook.print(); // Assuming print() method prints book details
cout << "-------------------------" << endl;
} catch (const runtime_error& e) {
cerr << "Error: " << e.what() << endl;
cout << "-------------------------" << endl;
}
}
} else {
cout << "No books currently borrowed by user: " << username << endl;
}
}
void LibrarySystem::removeBook() {
string isbn;
cout << "Enter ISBN of the book to remove: ";
cin >> isbn;
try {
// Check if the book exists
if (bookDatabase.isBookAvailable(isbn)) {
bookDatabase.removeBook(isbn);
//Remove the book's ISBN from the waitlist
json bookWaitlist;
ifstream queueFile(queueFilename);
if (queueFile.is_open()) {
queueFile >> bookWaitlist;
queueFile.close();
} else {
throw runtime_error("Failed to open file for reading: " + borrowedBooksFilename);
}
//Erase the ISBN and its associated objects
bookWaitlist.erase(isbn);
// Save the updated waitlist JSON file
ofstream waitlistFile(queueFilename);
if (waitlistFile.is_open()) {
waitlistFile << bookWaitlist.dump(4); // Pretty print with 4 spaces
waitlistFile.close();
} else {
cerr << "Failed to open file for writing: " << borrowedBooksFilename << endl;
}
cout << "Book with ISBN " << isbn << " removed successfully.\n";
} else {
cout << "Error: Book with ISBN " << isbn << " not found.\n";
}
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
}
void LibrarySystem::updateBook() {
string isbn, title, author;
int copiesInStock;
// Input ISBN and validate it
cout << "Enter ISBN of the book to update: ";
cin >> isbn;
try {
// Check if the book exists
if (!bookDatabase.isBookAvailable(isbn)) {
cout << "Error: Book with ISBN " << isbn << " not found." << endl;
return;
}
// Input new title
cin.ignore(); // Clear the input buffer
cout << "Enter new title: ";
getline(cin, title);
// Input new author
cout << "Enter new author: ";
getline(cin, author);
// Input number of copies in stock
cout << "Enter number of copies in stock: ";
cin >> copiesInStock;
// Update the book in the database
bookDatabase.updateBook(isbn, title, author, copiesInStock);
cout << "Book with ISBN " << isbn << " updated successfully." << endl;
} catch (const exception& e) {
cerr << "Error: " << e.what() << endl;
}
}
bool LibrarySystem::operator==(const string &title) const {
return this->getTitle() == title;
}
string LibrarySystem::getTitle() const {
string title = "a title";
return title;
}
void LibrarySystem::viewLibrary() const {
bookDatabase.printAllBooks();
}
int LibrarySystem::getNoOfCopiesInStock() const {
return copiesInStock;
}
void LibrarySystem::updateInStock(int num) {
copiesInStock += num;
}
void LibrarySystem::setCopiesInStock(int num) {
copiesInStock = num;
}
// Utility function to clear the screen
void LibrarySystem::clearScreen() {
#ifdef _WIN32
system("cls");
#else
// Assume POSIX compliant system
system("clear");
#endif
}
// Utility function to prompt user to press Enter to continue
void LibrarySystem::pressEnterToContinue() {
cout << "Press Enter to continue...";
cin.ignore(); // Clear input buffer
cin.get(); // Wait for Enter key press
}
// Signal handler function
void LibrarySystem::signalHandler(int signal) {
if (signal == SIGINT || signal == SIGTERM) {
terminationSignalReceived = 1;
std::cout << "Termination signal received. Saving data..." << std::endl;
// Save data to files before exiting
// Example: Save book database
bookDatabase.saveToFile(bookDB);
// You can add more save operations for other databases if needed
std::exit(EXIT_SUCCESS); // Exit gracefully
}
}