-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlibrary.cpp
More file actions
202 lines (174 loc) · 5.12 KB
/
library.cpp
File metadata and controls
202 lines (174 loc) · 5.12 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
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm> // Needed for transform
using namespace std;
class Book {
private:
static int nextBookID;
int bookID;
string title;
string author;
bool isBorrowed;
public:
Book(const string &title, const string &author) : title(title), author(author), isBorrowed(false) {
bookID = nextBookID++;
}
virtual void displayDetails() const {
cout << "Book ID: " << bookID << "\nTitle: " << title << "\nAuthor: " << author << "\nStatus: "
<< (isBorrowed ? "Borrowed" : "Available") << endl;
}
virtual string getType() const = 0;
int getBookID() const {
return bookID;
}
bool getBorrowStatus() const {
return isBorrowed;
}
void borrowBook() {
if (!isBorrowed) {
isBorrowed = true;
cout << "Book borrowed successfully!" << endl;
} else {
cout << "Book is already borrowed!" << endl;
}
}
void returnBook() {
if (isBorrowed) {
isBorrowed = false;
cout << "Book returned successfully!" << endl;
} else {
cout << "Book is not borrowed!" << endl;
}
}
};
int Book::nextBookID = 1001;
class FictionBook : public Book {
public:
FictionBook(const string &title, const string &author) : Book(title, author) {}
void displayDetails() const override {
Book::displayDetails();
cout << "Genre: Fiction" << endl;
}
string getType() const override {
return "Fiction";
}
};
class NonFictionBook : public Book {
public:
NonFictionBook(const string &title, const string &author) : Book(title, author) {}
void displayDetails() const override {
Book::displayDetails();
cout << "Genre: Non-Fiction" << endl;
}
string getType() const override {
return "Non-Fiction";
}
};
class Library {
private:
vector<Book*> books;
public:
void addBook() {
string title, author, type;
cout << "Enter Book Title: ";
cin.ignore();
getline(cin, title);
cout << "Enter Author Name: ";
getline(cin, author);
cout << "Enter Book Type (Fiction/Non-Fiction): ";
cin >> type;
// Convert the type to lowercase
transform(type.begin(), type.end(), type.begin(), ::tolower);
Book* newBook = nullptr;
if (type == "fiction") {
newBook = new FictionBook(title, author);
} else if (type == "non-fiction") {
newBook = new NonFictionBook(title, author);
} else {
cout << "Invalid book type!" << endl;
return;
}
books.push_back(newBook);
cout << "Book added successfully!" << endl;
}
void removeBook(int bookID) {
for (auto it = books.begin(); it != books.end(); ++it) {
if ((*it)->getBookID() == bookID) {
delete *it;
books.erase(it);
cout << "Book removed successfully!" << endl;
return;
}
}
cout << "Book not found!" << endl;
}
void borrowBook(int bookID) {
for (Book* book : books) {
if (book->getBookID() == bookID) {
book->borrowBook();
return;
}
}
cout << "Book not found!" << endl;
}
void returnBook(int bookID) {
for (Book* book : books) {
if (book->getBookID() == bookID) {
book->returnBook();
return;
}
}
cout << "Book not found!" << endl;
}
void displayAllBooks() const {
cout << "Library Book List\n----------------------------\n";
for (const Book* book : books) {
book->displayDetails();
cout << "----------------------------\n";
}
}
~Library() {
for (Book* book : books) {
delete book;
}
}
};
int main() {
Library library;
int choice, bookID;
while (true) {
cout << "\nLibrary Management System\n";
cout << "1. Add Book\n2. Remove Book\n3. Borrow Book\n4. Return Book\n5. Display All Books\n6. Exit\nChoose an option: ";
cin >> choice;
switch (choice) {
case 1:
library.addBook();
break;
case 2:
cout << "Enter Book ID to remove: ";
cin >> bookID;
library.removeBook(bookID);
break;
case 3:
cout << "Enter Book ID to borrow: ";
cin >> bookID;
library.borrowBook(bookID);
break;
case 4:
cout << "Enter Book ID to return: ";
cin >> bookID;
library.returnBook(bookID);
break;
case 5:
library.displayAllBooks();
break;
case 6:
cout << "Exiting..." << endl;
return 0;
default:
cout << "Invalid option! Please try again." << endl;
}
}
}