-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartLibraryManagementSystem.cpp
More file actions
228 lines (189 loc) Β· 5.66 KB
/
SmartLibraryManagementSystem.cpp
File metadata and controls
228 lines (189 loc) Β· 5.66 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
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
using namespace std;
// Base class for a Book
class Book {
protected:
string title;
string author;
string category;
bool isAvailable;
public:
// Constructor to initialize book details
Book(string t, string a, string c) : title(t), author(a), category(c), isAvailable(true) {}
// Virtual function for displaying book information
virtual void displayBookInfo() {
cout << "π Title: " << title << ", Author: " << author << ", Category: " << category;
cout << (isAvailable ? " [Available]" : " [Not Available]") << endl;
}
// Mark the book as borrowed
void borrowBook() {
if (isAvailable) {
isAvailable = false;
cout << "π You have successfully borrowed '" << title << "'!" << endl;
} else {
cout << "β Sorry, this book is not available for borrowing." << endl;
}
}
// Mark the book as returned
void returnBook() {
if (!isAvailable) {
isAvailable = true;
cout << "π Thank you for returning '" << title << "'!" << endl;
} else {
cout << "β This book was not borrowed." << endl;
}
}
// Getters and Setters
bool getAvailability() {
return isAvailable;
}
string getTitle() {
return title;
}
string getCategory() {
return category;
}
};
// Derived class for a Novel (can be extended further)
class Novel : public Book {
public:
Novel(string t, string a) : Book(t, a, "Novel") {}
void displayBookInfo() override {
cout << "π Novel - ";
Book::displayBookInfo();
}
};
// Derived class for a Research Paper (can be extended further)
class ResearchPaper : public Book {
public:
ResearchPaper(string t, string a) : Book(t, a, "Research Paper") {}
void displayBookInfo() override {
cout << "π Research Paper - ";
Book::displayBookInfo();
}
};
// User class (base class for different users)
class User {
protected:
string name;
string userType;
public:
// Constructor to initialize user details
User(string n, string type) : name(n), userType(type) {}
// Virtual function to borrow a book
virtual void borrow(Book &book) {
cout << "β You cannot borrow books as a " << userType << "." << endl;
}
// Virtual function to return a book
virtual void returnBook(Book &book) {
cout << "β You cannot return books as a " << userType << "." << endl;
}
};
// Derived class for Student (inherits from User)
class Student : public User {
public:
Student(string n) : User(n, "Student") {}
void borrow(Book &book) override {
if (book.getAvailability()) {
book.borrowBook();
} else {
cout << "β Sorry, the book is unavailable." << endl;
}
}
void returnBook(Book &book) override {
book.returnBook();
}
};
// Derived class for Staff (inherits from User)
class Staff : public User {
public:
Staff(string n) : User(n, "Staff") {}
void borrow(Book &book) override {
if (book.getAvailability()) {
book.borrowBook();
} else {
cout << "β Sorry, the book is unavailable." << endl;
}
}
void returnBook(Book &book) override {
book.returnBook();
}
};
// Library Management class
class Library {
private:
vector<Book*> books;
vector<User*> users;
public:
// Add a book to the library
void addBook(Book* book) {
books.push_back(book);
}
// Register a user
void registerUser(User* user) {
users.push_back(user);
}
// Display all available books in the library
void displayAllBooks() {
cout << "\nπ List of Books in the Library: " << endl;
for (auto book : books) {
book->displayBookInfo();
}
}
// Find and borrow a book by title
void borrowBook(string title, User* user) {
for (auto book : books) {
if (book->getTitle() == title) {
user->borrow(*book);
return;
}
}
cout << "β Book not found." << endl;
}
// Find and return a book by title
void returnBook(string title, User* user) {
for (auto book : books) {
if (book->getTitle() == title) {
user->returnBook(*book);
return;
}
}
cout << "β Book not found." << endl;
}
};
int main() {
// Create books
Book* book1 = new Novel("The Great Gatsby", "F. Scott Fitzgerald");
Book* book2 = new ResearchPaper("Quantum Computing: A New Era", "John Doe");
// Create users
User* student1 = new Student("Alice");
User* staff1 = new Staff("Bob");
// Create library
Library library;
// Add books to the library
library.addBook(book1);
library.addBook(book2);
// Register users
library.registerUser(student1);
library.registerUser(staff1);
// Display available books
library.displayAllBooks();
// Student borrows a book
cout << "\n--- Student borrows a book ---" << endl;
library.borrowBook("The Great Gatsby", student1);
// Staff tries to borrow a book
cout << "\n--- Staff borrows a book ---" << endl;
library.borrowBook("Quantum Computing: A New Era", staff1);
// Student returns the book
cout << "\n--- Student returns a book ---" << endl;
library.returnBook("The Great Gatsby", student1);
// Cleanup: Deleting dynamically allocated objects
delete book1;
delete book2;
delete student1;
delete staff1;
return 0;
}