-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary_sys_management.cpp
More file actions
274 lines (235 loc) · 7.22 KB
/
library_sys_management.cpp
File metadata and controls
274 lines (235 loc) · 7.22 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
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm> // For std::find
using namespace std;
// Book class
class Book {
private:
int bookID;
string title, author;
bool isAvailable;
public:
// Constructor
Book(int id, string t, string a) : bookID(id), title(t), author(a), isAvailable(true) {}
// Getters
int getBookID() const { return bookID; }
string getTitle() const { return title; }
string getAuthor() const { return author; }
bool getAvailability() const { return isAvailable; }
// Methods to update availability
void borrowBook() { isAvailable = false; }
void returnBook() { isAvailable = true; }
// Display book details
void display() const {
cout << "Book ID: " << bookID
<< ", Title: " << title
<< ", Author: " << author
<< ", Available: " << (isAvailable ? "Yes" : "No") << endl;
}
};
// Base class for User
class User {
protected:
int userID;
string name;
vector<int> borrowedBooks;
public:
User(int id, string n) : userID(id), name(n) {}
virtual void borrowBook(int bookID) = 0; // Pure virtual function
virtual void returnBook(int bookID) = 0;
// Getters
string getName() const { return name; }
int getUserID() const { return userID; }
vector<int> getBorrowedBooks() const { return borrowedBooks; }
// Display borrowed books
void displayBorrowedBooks() const {
cout << name << "'s Borrowed Books: ";
for (int id : borrowedBooks)
cout << id << " ";
cout << endl;
}
};
// Derived class for Student
class Student : public User {
private:
const int borrowLimit = 3;
public:
Student(int id, string n) : User(id, n) {}
void borrowBook(int bookID) override {
if (borrowedBooks.size() >= borrowLimit) {
cout << "Borrow limit reached for " << name << ". Cannot borrow more books.\n";
} else {
borrowedBooks.push_back(bookID);
cout << name << " borrowed book with ID " << bookID << endl;
}
}
void returnBook(int bookID) override {
auto it = find(borrowedBooks.begin(), borrowedBooks.end(), bookID);
if (it != borrowedBooks.end()) {
borrowedBooks.erase(it);
cout << name << " returned book with ID " << bookID << endl;
} else {
cout << "Book ID " << bookID << " not found in " << name << "'s borrowed list.\n";
}
}
};
// Derived class for Faculty
class Faculty : public User {
private:
const int borrowLimit = 5;
public:
Faculty(int id, string n) : User(id, n) {}
void borrowBook(int bookID) override {
if (borrowedBooks.size() >= borrowLimit) {
cout << "Borrow limit reached for " << name << ". Cannot borrow more books.\n";
} else {
borrowedBooks.push_back(bookID);
cout << name << " borrowed book with ID " << bookID << endl;
}
}
void returnBook(int bookID) override {
auto it = find(borrowedBooks.begin(), borrowedBooks.end(), bookID);
if (it != borrowedBooks.end()) {
borrowedBooks.erase(it);
cout << name << " returned book with ID " << bookID << endl;
} else {
cout << "Book ID " << bookID << " not found in " << name << "'s borrowed list.\n";
}
}
};
// Library system
class Library {
private:
vector<Book> books;
map<int, User*> users;
public:
// Add a new book
void addBook() {
int id;
string title, author;
cout << "Enter Book ID: ";
cin >> id;
cin.ignore();
cout << "Enter Book Title: ";
getline(cin, title);
cout << "Enter Author Name: ";
getline(cin, author);
books.push_back(Book(id, title, author));
cout << "Book added successfully!\n";
}
// Display all books
void displayBooks() const {
cout << "\nAvailable Books:\n";
for (const auto& book : books) {
book.display();
}
}
// Find book by ID
Book* findBook(int bookID) {
for (auto& book : books) {
if (book.getBookID() == bookID)
return &book;
}
return nullptr;
}
// Add a user
void addUser() {
int id, userType;
string name;
cout << "Enter User ID: ";
cin >> id;
cin.ignore();
cout << "Enter User Name: ";
getline(cin, name);
cout << "Select User Type (1-Student, 2-Faculty): ";
cin >> userType;
if (userType == 1) {
users[id] = new Student(id, name);
cout << "Student added successfully!\n";
} else if (userType == 2) {
users[id] = new Faculty(id, name);
cout << "Faculty added successfully!\n";
} else {
cout << "Invalid user type!\n";
}
}
// Borrow a book
void borrowBook() {
int userID, bookID;
cout << "Enter User ID: ";
cin >> userID;
cout << "Enter Book ID: ";
cin >> bookID;
if (users.find(userID) == users.end()) {
cout << "User ID not found.\n";
return;
}
Book* book = findBook(bookID);
if (!book || !book->getAvailability()) {
cout << "Book not available for borrowing.\n";
return;
}
users[userID]->borrowBook(bookID);
book->borrowBook();
}
// Return a book
void returnBook() {
int userID, bookID;
cout << "Enter User ID: ";
cin >> userID;
cout << "Enter Book ID: ";
cin >> bookID;
if (users.find(userID) == users.end()) {
cout << "User ID not found.\n";
return;
}
Book* book = findBook(bookID);
if (!book) {
cout << "Invalid book ID.\n";
return;
}
users[userID]->returnBook(bookID);
book->returnBook();
}
};
// Main function
int main() {
Library library;
int choice;
while (true) {
cout << "\nLibrary Management System\n";
cout << "1. Add Book\n";
cout << "2. Add User\n";
cout << "3. Display Books\n";
cout << "4. Borrow Book\n";
cout << "5. Return Book\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
library.addBook();
break;
case 2:
library.addUser();
break;
case 3:
library.displayBooks();
break;
case 4:
library.borrowBook();
break;
case 5:
library.returnBook();
break;
case 6:
cout << "Exiting... Goodbye!\n";
return 0;
default:
cout << "Invalid choice! Please try again.\n";
}
}
return 0;
}