-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentSystem.cpp
More file actions
1677 lines (1515 loc) · 56.2 KB
/
Copy pathCommentSystem.cpp
File metadata and controls
1677 lines (1515 loc) · 56.2 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
// CommentSystem.cpp
// ===============================
// Ad, Soyad : Hagar Al-gabri
// Ogrenci No : B241200553
// BOOK MANAGER MODULE
// Handles operations related to book records in the library.
// ===============================
#include <iostream> // Basic input/output stream
#include <string> // String support for handling text
#include <ctime> // Time utilities for tracking book addition and borrowing dates
#include <vector> // STL vector for sorting and managing book lists
#include <algorithm> // Algorithms such as sort
#include <iomanip> // To format output (e.g., timestamps)
#include <sstream> // String stream for time formatting
#include <unordered_map> // Hash maps for ratings
#include <queue> // For priority queue logic
#pragma warning(disable : 4996) // Disable unsafe warning for localtime on Visual Studio
using namespace std;
//======================================================
// MODULE 3: RequestQueue
// Purpose: Manages book requests (normal and academic)
//======================================================
class RequestQueue {
private:
// Structure to store each book request
struct BookRequest {
string title;
string author;
bool isAcademic;
int priority; // 1 = academic, 0 = normal
BookRequest* next;
BookRequest(string t, string a, bool academic)
: title(t), author(a), isAcademic(academic), priority(academic ? 1 : 0), next(nullptr) {}
};
BookRequest* front; // Points to the front of the queue
BookRequest* rear; // Points to the rear of the queue
public:
// Constructor: Initializes an empty queue
RequestQueue() : front(nullptr), rear(nullptr) {}
// Adds a new book request to the queue based on priority
void enqueue(string title, string author, bool isAcademic) {
BookRequest* newRequest = new BookRequest(title, author, isAcademic);
// Insert with higher priority (academic) at the front
if (!front || newRequest->priority > front->priority) {
newRequest->next = front;
front = newRequest;
if (!rear) rear = newRequest;
}
else {
// Traverse to insert based on priority order
BookRequest* current = front;
while (current->next && current->next->priority >= newRequest->priority) {
current = current->next;
}
newRequest->next = current->next;
current->next = newRequest;
if (!newRequest->next) rear = newRequest;
}
cout << "Request added to queue!\n";
}
// Prints all current requests
void printRequests() const {
if (!front) {
cout << "No requests in the queue.\n";
return;
}
BookRequest* current = front;
while (current) {
cout << "- " << current->title << " by " << current->author;
if (current->isAcademic) cout << " [Academic Priority]";
cout << endl;
current = current->next;
}
}
// Processes (removes) the request at the front
void fulfillRequest() {
if (!front) {
cout << "No requests to fulfill.\n";
return;
}
BookRequest* temp = front;
front = front->next;
cout << "Fulfilling request: " << temp->title << " by " << temp->author << endl;
delete temp;
if (!front) rear = nullptr;
}
};
// ===============================
// BOOK MANAGER MODULE
// Handles operations related to book records in the library.
// Uses a singly linked list to store and manage books.
// ===============================
class BookManager {
public:
struct Book {
int id;
string title;
string author;
int pages;
int year;
string publisher; // ("TR", "EN")
string damageNote;
time_t addDate; // Date and time when the book was added
Book* next; // Pointer to the next book in the linked list
// Constructor to initialize book attributes
Book(int i, const string& t, const string& a, int p, int y, const string& pub)
: id(i), title(t), author(a), pages(p), year(y), publisher(pub),
damageNote(""), addDate(time(nullptr)), next(nullptr) {}
};
private:
Book* head; // Pointer to the first book in the list
int count; // Total number of books
// Constructor to initialize an empty list
public:
BookManager() : head(nullptr), count(0) {}
void addBook(int id, const string& title, const string& author, int pages, int year) {
if (findBook(id) != nullptr) {
cout << "Error: Book ID already exists!\n";
return;
}
// Auto assign publisher based on even/odd ID
string publisher = (id % 2 == 0) ? "TR" : "EN";
// Create and insert the new book at the beginning of the list
Book* newBook = new Book(id, title, author, pages, year, publisher);
newBook->next = head;
head = newBook;
count++;
}
// Find a book by ID
Book* findBook(int id) const {
Book* current = head;
while (current != nullptr) {
if (current->id == id) return current;
current = current->next;
}
return nullptr;
}
// Display all books sorted by ID in ascending order
void listBooks() const {
vector<Book*> books;
Book* current = head;
while (current != nullptr) {
books.push_back(current);
current = current->next;
}
sort(books.begin(), books.end(), [](Book* a, Book* b) {
return a->id < b->id;
});
for (Book* book : books) {
tm* timeinfo = localtime(&(book->addDate));
cout << book->id << " | " << book->title
<< " by " << book->author
<< " (" << book->year << ")"
<< " | Pages: " << book->pages
<< " | Added: " << put_time(timeinfo, "%Y-%m-%d %H:%M:%S") << endl;
}
}
void listBooksByPageCount() const {
vector<Book*> books;
Book* current = head;
while (current != nullptr) {
books.push_back(current);
current = current->next;
}
sort(books.begin(), books.end(), [](Book* a, Book* b) {
return a->pages < b->pages;
});
cout << "\n--- Books Sorted by Pages ---\n";
for (Book* book : books) {
cout << book->pages << " pages | " << book->title << endl;
}
}
void listOldestFiveBooks() const {
vector<Book*> books;
Book* current = head;
while (current != nullptr) {
books.push_back(current);
current = current->next;
}
if (books.empty()) {
cout << "No books available.\n";
return;
}
// Sort books by ID
sort(books.begin(), books.end(), [](Book* a, Book* b) {
return a->year < b->year;
});
cout << "\n--- Oldest 5 Books ---\n";
int limit = min(5, (int)books.size());
for (int i = 0; i < limit; ++i) {
cout << books[i]->id << " | " << books[i]->title
<< " (" << books[i]->year << ")\n";
}
}
bool deleteBook(int id) {
if (head == nullptr) return false;
if (head->id == id) {
Book* temp = head;
head = head->next;
delete temp;
count--;
return true;
}
// Search for the book to delete
Book* current = head;
while (current->next != nullptr && current->next->id != id) {
current = current->next;
}
if (current->next != nullptr) {
Book* temp = current->next;
current->next = current->next->next;
delete temp;
count--;
return true;
}
return false;
}
void editBookByTitle(const string& title) {
Book* current = head;
while (current != nullptr) {
if (current->title == title) {
cout << "Editing Book: " << current->title << endl;
cout << "Enter new title (or press enter to keep current): ";
string newTitle;
getline(cin, newTitle);
if (!newTitle.empty()) current->title = newTitle;
cout << "Enter new author (or press enter to keep current): ";
string newAuthor;
getline(cin, newAuthor);
if (!newAuthor.empty()) current->author = newAuthor;
cout << "Enter new pages (or -1 to keep current): ";
int newPages;
cin >> newPages;
cin.ignore();
if (newPages != -1) current->pages = newPages;
cout << "Enter new year (or -1 to keep current): ";
int newYear;
cin >> newYear;
cin.ignore();
if (newYear != -1) current->year = newYear;
cout << "Book updated successfully!\n";
return;
}
current = current->next;
}
cout << "Book with title \"" << title << "\" not found.\n";
}
void addDamageNote(int id, const string& note) {
Book* book = findBook(id);
if (book != nullptr) {
book->damageNote = note;
cout << "Damage note added successfully!\n";
}
else {
cout << "Book with ID " << id << " not found.\n";
}
}
void listDamagedBooks() const {
Book* current = head;
bool found = false;
cout << "\n--- Damaged Books ---\n";
while (current != nullptr) {
if (!current->damageNote.empty()) {
cout << "ID: " << current->id << " | "
<< "Title: " << current->title << " | "
<< "Damage: " << current->damageNote << endl;
found = true;
}
current = current->next;
}
if (!found) {
cout << "No damaged books found.\n";
}
}
// Return the total number of books
int getBookCount() const {
return count;
}
};
// ====== Comment System ======
// This class allows users to add comments to books and view them.
// It uses a simple singly linked list to store comments for each book.
class CommentSystem {
private:
struct Comment {
int bookID;
string text;
Comment* next; // Pointer to the next comment
};
Comment* head;
public:
CommentSystem() {
head = nullptr;
}
// Adds a new comment to a book by pushing it to the front of the list
void addComment(int bookID, const string& commentText) {
Comment* newComment = new Comment{ bookID, commentText, head };
head = newComment;
}
// Displays all comments for a specific book ID
void showComments(int bookID) const {
Comment* temp = head;
bool found = false;
cout << "\n--- Comments for Book ID " << bookID << " ---\n";
while (temp) {
if (temp->bookID == bookID) {
cout << "- " << temp->text << endl;
found = true;
}
temp = temp->next;
}
if (!found) {
cout << "No comments for this book.\n";
}
}
};
// ====== User Book Collection ======
// This module represents the user's personal book collection.
// It is implemented as a circular doubly linked list.
class UserCollection {
private:
struct UserBook {
int id;
string title;
string author;
int pages;
int year;
bool isRead; // Read status (true = read)
UserBook* next;
UserBook* prev;
UserBook(int i, const string& t, const string& a, int p, int y)
: id(i), title(t), author(a), pages(p), year(y),
isRead(false), next(nullptr), prev(nullptr) {}
};
UserBook* head; // Head of the list
bool collectionCreated; // Whether the collection is initialized
public:
UserCollection() : head(nullptr), collectionCreated(false) {}
// Creates the user's collection if not already created
void createCollection() {
if (collectionCreated) {
cout << "You already have a collection.\n";
return;
}
collectionCreated = true;
cout << "Your personal book collection has been created!\n";
}
// Adds a book to the user's collection (at the end of the circular list)
void addBook(int id, const string& title, const string& author, int pages, int year) {
if (!collectionCreated) {
cout << "Please create a collection first.\n";
return;
}
UserBook* newBook = new UserBook(id, title, author, pages, year);
if (head == nullptr) {
head = newBook;
head->next = head;
head->prev = head;
}
else {
newBook->next = head;
newBook->prev = head->prev;
head->prev->next = newBook;
head->prev = newBook;
}
cout << "Book added to your collection!\n";
}
// Displays all books in the user's collection
void listBooks() const {
if (!collectionCreated || head == nullptr) {
cout << "No books in your collection.\n";
return;
}
cout << "\n--- Your Book Collection ---\n";
UserBook* current = head;
do {
cout << current->id << " | " << current->title << " by " << current->author
<< " (" << current->year << ")"
<< " | Pages: " << current->pages
<< " | Status: " << (current->isRead ? "Read" : "Not Read") << endl;
current = current->next;
} while (current != head);
}
// Edits the read/unread status of a book
void editBookStatus(int id) {
if (!collectionCreated || head == nullptr) {
cout << "No books in your collection.\n";
return;
}
UserBook* current = head;
do {
if (current->id == id) {
cout << "Current status: " << (current->isRead ? "Read" : "Not Read") << endl;
cout << "Enter new status (1 = Read, 0 = Not Read): ";
int status;
cin >> status;
cin.ignore();
current->isRead = (status == 1);
cout << "Status updated!\n";
return;
}
current = current->next;
} while (current != head);
cout << "Book with ID " << id << " not found.\n";
}
// Deletes a book from the user's collection by ID
void deleteBook(int id) {
if (!collectionCreated || head == nullptr) {
cout << "No books in your collection.\n";
return;
}
UserBook* current = head;
do {
if (current->id == id) {
if (current->next == current) {
delete current;
head = nullptr;
}
else {
current->prev->next = current->next;
current->next->prev = current->prev;
if (current == head) {
head = current->next;
}
delete current;
}
cout << "Book deleted from your collection!\n";
return;
}
current = current->next;
} while (current != head);
cout << "Book with ID " << id << " not found.\n";
}
};
// ====== CircularList: Single Circular Linked List ======
// This class is used to manage circular lists such as borrowed, returned, or available books.
// ===============================
class CircularList {
private:
// Node represents a single book in the circular list.
struct Node {
int id;
string title;
string author;
int pages;
int year;
string borrowDate;
string returnDate;
Node* next;
// Constructor initializes node attributes
Node(int i, const string& t, const string& a, int p, int y)
: id(i), title(t), author(a), pages(p), year(y),
borrowDate("N/A"), returnDate("N/A"), next(nullptr) {}
};
Node* head; // Head pointer of the circular list
public:
void removeBook(int id);
// Returns the borrow date for a given book ID
string getBorrowDate(int id) const {
if (!head) return "N/A";
Node* current = head;
do {
if (current->id == id)
return current->borrowDate;
current = current->next;
} while (current != head);
return "N/A";
}
CircularList() : head(nullptr) {}
// Adds a borrowed book to the list and records the current date/time
void addBorrowedBook(int id, const string& title, const string& author, int pages, int year) {
Node* newNode = new Node(id, title, author, pages, year);
if (!head) {
head = newNode;
head->next = head;
}
else {
Node* temp = head;
while (temp->next != head) temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
// Set current time as borrow date
time_t now = time(0);
tm* nowTime = localtime(&now);
ostringstream oss;
oss << put_time(nowTime, "%Y-%m-%d %H:%M:%S");
newNode->borrowDate = oss.str();
}
// Adds a returned book to the list and records the return time
void addReturnedBook(int id, const string& title, const string& author, int pages, int year, const string& borrowDate) {
Node* newNode = new Node(id, title, author, pages, year);
if (!head) {
head = newNode;
head->next = head;
}
else {
Node* temp = head;
while (temp->next != head) temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
newNode->borrowDate = borrowDate;
// Set current time as return date
time_t now = time(0);
tm* nowTime = localtime(&now);
ostringstream oss;
oss << put_time(nowTime, "%Y-%m-%d %H:%M:%S");
newNode->returnDate = oss.str();
}
// Adds an available book to the list
void addAvailableBook(int id, const string& title, const string& author, int pages, int year) {
Node* newNode = new Node(id, title, author, pages, year);
if (!head) {
head = newNode;
head->next = head;
}
else {
Node* temp = head;
while (temp->next != head) temp = temp->next;
temp->next = newNode;
newNode->next = head;
}
}
void displayBorrowedBooks() const {
if (!head) {
cout << "No borrowed books.\n";
return;
}
cout << "\n--- Borrowed Books ---\n";
Node* temp = head;
do {
cout << temp->id << " | " << temp->title
<< " | Borrowed On: " << temp->borrowDate << endl;
temp = temp->next;
} while (temp != head);
}
void displayReturnedBooks() const {
if (!head) {
cout << "No returned books.\n";
return;
}
cout << "\n--- Returned Books ---\n";
Node* temp = head;
do {
cout << temp->id << " | " << temp->title
<< " | Borrowed On: " << temp->borrowDate
<< " | Returned On: " << temp->returnDate << endl;
temp = temp->next;
} while (temp != head);
}
void displayAvailableBooks() const {
if (!head) {
cout << "No available books.\n";
return;
}
cout << "\n--- Available Books ---\n";
Node* temp = head;
do {
cout << temp->id << " | " << temp->title
<< " by " << temp->author << endl;
temp = temp->next;
} while (temp != head);
}
};
//====== Stack for User Borrowed/Returned Books ======
class UserStack {
public:
// Structure representing a node in the stack
struct StackNode {
int id;
string title;
string author;
StackNode* next;
// Constructor to initialize a book node
StackNode(int i, const string& t, const string& a)
: id(i), title(t), author(a), next(nullptr) {}
};
StackNode* top;
public:
// Constructor initializes the top to null
UserStack() : top(nullptr) {}
// Add a book to the top of the stack
void push(int id, const string& title, const string& author) {
StackNode* newNode = new StackNode(id, title, author);
newNode->next = top;
top = newNode;
}
// Remove the top book from the stack
void pop() {
if (top == nullptr) {
cout << "Stack is empty.\n";
return;
}
StackNode* temp = top;
top = top->next;
delete temp;
}
// Return the top book without removing it
StackNode* peek() const {
return top;
}
// Check if the stack is empty
bool isEmpty() const {
return top == nullptr;
}
// Display all books in the stack
void display() const {
if (top == nullptr) {
cout << "Stack is empty.\n";
return;
}
StackNode* temp = top;
cout << "\n--- Stack Books ---\n";
while (temp) {
cout << temp->id << " | " << temp->title
<< " by " << temp->author << endl;
temp = temp->next;
}
}
};
// ====== Global Tree Node Structure ======
// Structure used in the Binary Search Tree for reporting
struct TreeNode {
int id;
string title;
string author;
int pages;
int year;
string publisher;
string language;
time_t borrowDate;
TreeNode* left;
TreeNode* right;
// Constructor to initialize a tree node
TreeNode(int i, string t, string a, int p, int y, string pub, string lang, time_t date)
: id(i), title(t), author(a), pages(p), year(y),
publisher(pub), language(lang), borrowDate(date),
left(nullptr), right(nullptr) {}
};
// ====== Hash Table for Book Ratings ======
class RatingHashTable {
private:
// Structure for a single rating entry in the hash table
struct RatingEntry {
int bookID;
string title;
int rating; // 1 to 5
RatingEntry* next;
// Constructor to initialize a rating entry
RatingEntry(int id, const string& t, int r)
: bookID(id), title(t), rating(r), next(nullptr) {}
};
static const int SIZE = 10; // Size of the hash table
RatingEntry* table[SIZE]; // Array of pointers to linked list buckets
// Hash function based on book ID
int hashFunction(int id) const {
return id % SIZE;
}
public:
// Constructor initializes all buckets to null
RatingHashTable() {
for (int i = 0; i < SIZE; ++i)
table[i] = nullptr;
}
// Insert a new rating into the hash table
void insert(int id, const string& title, int rating) {
if (rating < 1 || rating > 5) {
cout << " Invalid rating. Must be between 1 and 5.\n";
return;
}
int index = hashFunction(id);
RatingEntry* newEntry = new RatingEntry(id, title, rating);
newEntry->next = table[index];
table[index] = newEntry;
}
// Display all books with rating 4 or higher
void displayHighRatedBooks() const {
cout << "\n--- Books with Rating >= 4 ---\n";
for (int i = 0; i < SIZE; ++i) {
RatingEntry* temp = table[i];
while (temp) {
if (temp->rating >= 4) {
cout << temp->bookID << " | " << temp->title << " | Rating: " << temp->rating << endl;
}
temp = temp->next;
}
}
}
// Display all books with their ratings
void displayAllRatings() const {
cout << "\n--- All Rated Books ---\n";
for (int i = 0; i < SIZE; ++i) {
RatingEntry* temp = table[i];
while (temp) {
cout << temp->bookID << " | " << temp->title << " | Rating: " << temp->rating << endl;
temp = temp->next;
}
}
}
};
// ====== Library System Controller ======
class LibrarySystem {
private:
BookManager bookManager;
CommentSystem commentSystem;
UserCollection userCollection;
CircularList borrowedList;
CircularList returnedList;
CircularList availableList;
UserStack userBorrowStack;
UserStack userReturnStack;
RequestQueue requestQueue;
bool userCollectionCreated;
RatingHashTable staffRatings;
RatingHashTable userRatings;
RatingHashTable ratingTable;
RatingHashTable staffRatingsHashTable;
TreeNode* borrowTreeRoot = nullptr;
void insertToTree(TreeNode*& root, int id, const string& title, const string& author,
int pages, int year, const string& publisher, const string& language, time_t borrowDate) {
if (!root) {
root = new TreeNode(id, title, author, pages, year, publisher, language, borrowDate);
return;
}
if (id < root->id) {
insertToTree(root->left, id, title, author, pages, year, publisher, language, borrowDate);
}
else {
insertToTree(root->right, id, title, author, pages, year, publisher, language, borrowDate);
}
}
void addBorrowedBooksToTree() {
UserStack::StackNode* temp = userBorrowStack.peek();
while (temp) {
BookManager::Book* book = bookManager.findBook(temp->id);
if (book) {
string publisher = (book->id % 2 == 0) ? "TR" : "EN";
string language = (book->id % 2 == 0) ? "tr" : "en";
time_t now = time(0);
insertToTree(borrowTreeRoot, book->id, book->title, book->author,
book->pages, book->year, publisher, language, now);
}
temp = temp->next;
}
cout << "Books transferred from stack to tree successfully.\n";
}
void reportBooksByPublicationYear(TreeNode* node) {
if (!node) return;
reportBooksByPublicationYear(node->left);
if (node->year < 1950)
cout << "[Before 1950] ";
else
cout << "[1950 or After] ";
cout << node->id << " | " << node->title << " | Year: " << node->year << endl;
reportBooksByPublicationYear(node->right);
}
void reportBooksByBorrowDuration(TreeNode* node) {
if (!node) return;
time_t now = time(0);
double days = difftime(now, node->borrowDate) / (60 * 60 * 24);
cout << " Book ID: " << node->id << " | Title: " << node->title;
if (days <= 30)
cout << " Borrowed < 30 days";
else
cout << " Borrowed ≥ 30 days";
cout << endl;
reportBooksByBorrowDuration(node->left);
reportBooksByBorrowDuration(node->right);
}
void reportBooksByPublicationBeforeOrAfter1950(TreeNode* node) {
if (!node) return;
if (node->year < 1950)
cout << " Book ID: " << node->id << " | Title: " << node->title << " | Year: " << node->year << " (Before 1950)\n";
else
cout << " Book ID: " << node->id << " | Title: " << node->title << " | Year: " << node->year << " (1950 or After)\n";
reportBooksByPublicationBeforeOrAfter1950(node->left);
reportBooksByPublicationBeforeOrAfter1950(node->right);
}
void reportBooksByYearAndDuration(TreeNode* node) {
if (!node) return;
reportBooksByYearAndDuration(node->left);
time_t now = time(0);
double days = difftime(now, node->borrowDate) / (60 * 60 * 24);
if (node->year < 1950 && days > 30) {
cout << "Book ID: " << node->id
<< " | Title: " << node->title
<< " | Year: " << node->year
<< " | Borrowed Days: " << days << endl;
}
reportBooksByYearAndDuration(node->right);
}
void transferUserBorrowedToTree() {
UserStack::StackNode* temp = userBorrowStack.peek();
if (!temp) {
cout << "No borrowed books in stack.\n";
return;
}
while (temp) {
BookManager::Book* book = bookManager.findBook(temp->id);
if (!book) {
cout << "Book ID " << temp->id << " not found in library.\n";
temp = temp->next;
continue;
}
string publisher = (book->id % 2 == 0) ? "TR" : "EN";
string language = (book->id % 2 == 0) ? "tr" : "en";
time_t now = time(0);
insertToTree(borrowTreeRoot, book->id, book->title, book->author,
book->pages, book->year, publisher, language, now);
temp = temp->next;
}
cout << " All borrowed books transferred to tree using stored data.\n";
}
void reportBooksByLanguage(TreeNode* node, const string& lang) {
if (!node) return;
reportBooksByLanguage(node->left, lang);
if (lang == "both" || node->language == lang) {
cout << "Book ID: " << node->id
<< " | Title: " << node->title
<< " | Language: " << node->language << endl;
}
reportBooksByLanguage(node->right, lang);
}
void reportBooksByPageCount(TreeNode* node, int threshold) {
if (!node) return;
reportBooksByPageCount(node->left, threshold);
if (node->pages < threshold) {
cout << "[< " << threshold << " pages] ";
}
else {
cout << "[>= " << threshold << " pages] ";
}
cout << "Book ID: " << node->id
<< " | Title: " << node->title
<< " | Pages: " << node->pages << endl;
reportBooksByPageCount(node->right, threshold);
}
void reportBooksByPublisher(TreeNode* node, const string& pub) {
if (!node) return;
reportBooksByPublisher(node->left, pub);
if (node->publisher == pub) {
cout << "Book ID: " << node->id
<< " | Title: " << node->title
<< " | Publisher: " << node->publisher << endl;
}
reportBooksByPublisher(node->right, pub);
}
public:
// Constructor to initialize system state
LibrarySystem() : userCollectionCreated(false) {}
// This function initializes the library with some predefined books
void initializeAvailableBooks() {
// Adding books to the main book manager
bookManager.addBook(1, "Veri Yapilari ve Algoritmalar", "Rifat Colkesen", 480, 2021);
bookManager.addBook(2, "Object-Oriented Programming in C++", "Robert Lafore", 925, 1999);
bookManager.addBook(3, "C++ Pocket Reference", "Kyle Loudon", 140, 2002);
bookManager.addBook(4, "Clean Code", "Robert C. Martin", 464, 2008);
bookManager.addBook(5, "Design Patterns", "Erich Gamma", 395, 1994);
bookManager.addBook(6, "Effective Modern C++", "Scott Meyers", 334, 2014);
// Adding the same books to the available circular list
availableList.addAvailableBook(1, "Veri Yapilari ve Algoritmalar", "Rifat Colkesen", 480, 2021);
availableList.addAvailableBook(2, "Object-Oriented Programming in C++", "Robert Lafore", 925, 1999);
availableList.addAvailableBook(3, "C++ Pocket Reference", "Kyle Loudon", 140, 2002);
availableList.addAvailableBook(4, "Clean Code", "Robert C. Martin", 464, 2008);
availableList.addAvailableBook(5, "Design Patterns", "Erich Gamma", 395, 1994);
availableList.addAvailableBook(6, "Effective Modern C++", "Scott Meyers", 334, 2014);
}
// Main loop of the program where user chooses between staff/user or exits
void run() {
initializeAvailableBooks(); // Prepare initial library state
while (true) {
// Main menu interface
cout << "\n=== LIBRARY SYSTEM ===\n"
<< "1. Staff Login\n"
<< "2. User Login\n"
<< "0. Exit\n"
<< "Choice: ";
string input;
getline(cin, input);
// Validate input is numeri
if (input.empty() || !all_of(input.begin(), input.end(), ::isdigit)) {
cout << "Invalid choice. Please enter a number.\n";
continue;
}