DSA (CPP) Project
Name : Govind Pratap Singh
Topic : Library Management System
Language used : CPP
Code:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
// Book Class
class Book {
public:
int id;
string title;
string author;
bool isIssued;
Book(int id, string title, string author)
: id(id), title(title), author(author), isIssued(false) {}
};
// IssuedBook Class
class IssuedBook {
public:
int bookId;
string studentName;
IssuedBook(int bookId, string studentName)
: bookId(bookId), studentName(studentName) {}
};
// Library Class
class Library {
private:
vector<Book> books;
vector<IssuedBook> issuedBooks;
public:
void addBook(int id, string title, string author);
void searchBookByTitle(const string& title);
void searchBookById(int id);
void issueBook(int id, const string& studentName);
void returnBook(int id);
void listAllBooks();
void deleteBook(int id);
};
// Implementation of Library Methods
void Library::addBook(int id, string title, string author) {
books.push_back(Book(id, title, author));
cout << "Book added successfully.\n";
}
void Library::searchBookByTitle(const string& title) {
auto it = find_if([Link](), [Link](), [&title](const Book& b) {
return [Link] == title;
});
if (it != [Link]()) {
cout << "Book Found: " << it->title << ", Author: " << it->author << ", ID: " << it->id << "\n";
} else {
cout << "Book not found.\n";
void Library::searchBookById(int id) {
auto it = find_if([Link](), [Link](), [id](const Book& b) {
return [Link] == id;
});
if (it != [Link]()) {
cout << "Book Found: " << it->title << ", Author: " << it->author << ", ID: " << it->id << "\n";
} else {
cout << "Book not found.\n";
void Library::issueBook(int id, const string& studentName) {
auto it = find_if([Link](), [Link](), [id](const Book& b) {
return [Link] == id;
});
if (it != [Link]() && !it->isIssued) {
it->isIssued = true;
issuedBooks.push_back(IssuedBook(id, studentName));
cout << "Book issued successfully.\n";
} else {
cout << "Book is either already issued or not found.\n";
void Library::returnBook(int id) {
auto it = find_if([Link](), [Link](), [id](const Book& b) {
return [Link] == id;
});
if (it != [Link]() && it->isIssued) {
it->isIssued = false;
[Link](remove_if([Link](), [Link](), [id](const IssuedBook& ib) {
return [Link] == id;
}), [Link]());
cout << "Book returned successfully.\n";
} else {
cout << "Book is either not issued or not found.\n";
void Library::listAllBooks() {
sort([Link](), [Link](), [](const Book& a, const Book& b) {
return [Link] < [Link];
});
for (const auto& book : books) {
cout << "ID: " << [Link] << ", Title: " << [Link] << ", Author: " << [Link]
<< ", Status: " << ([Link] ? "Issued" : "Available") << "\n";
void Library::deleteBook(int id) {
[Link](remove_if([Link](), [Link](), [id](const Book& b) {
return [Link] == id;
}), [Link]());
cout << "Book deleted successfully.\n";
// Main Function
int main() {
Library library;
// Adding books during initialization
[Link](1, "To Kill a Mockingbird", "Harper Lee");
[Link](2, "1984", "George Orwell");
[Link](3, "Pride and Prejudice", "Jane Austen");
[Link](4, "The Great Gatsby", "F. Scott Fitzgerald");
[Link](5, "Moby Dick", "Herman Melville");
[Link](6, "The Catcher in the Rye", "J.D. Salinger");
[Link](7, "Brave New World", "Aldous Huxley");
[Link](8, "The Grapes of Wrath", "John Steinbeck");
[Link](9, "The Hobbit", "J.R.R. Tolkien");
[Link](10, "One Hundred Years of Solitude", "Gabriel Garcia Marquez");
[Link](11, "The Lord of the Rings", "J.R.R. Tolkien");
[Link](12, "Crime and Punishment", "Fyodor Dostoevsky");
[Link](13, "The Picture of Dorian Gray", "Oscar Wilde");
[Link](14, "Jane Eyre", "Charlotte Bronte");
[Link](15, "War and Peace", "Leo Tolstoy");
[Link](16, "Anna Karenina", "Leo Tolstoy");
[Link](17, "Frankenstein", "Mary Shelley");
[Link](18, "Wuthering Heights", "Emily Bronte");
[Link](19, "The Odyssey", "Homer");
[Link](20, "The Adventures of Huckleberry Finn", "Mark Twain");
int choice, id;
string title, author, studentName;
while (true) {
cout << "Library Management System\n";
cout << "1. Add Book\n2. Search Book by Title\n3. Search Book by ID\n4. Issue Book\n5. Return
Book\n6. List All Books\n7. Delete Book\n8. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter Book ID: ";
cin >> id;
[Link](); // Ignore the newline character left in the buffer
cout << "Enter Title: ";
getline(cin, title);
cout << "Enter Author: ";
getline(cin, author);
[Link](id, title, author);
break;
case 2:
cout << "Enter Title: ";
[Link]();
getline(cin, title);
[Link](title);
break;
case 3:
cout << "Enter Book ID: ";
cin >> id;
[Link](id);
break;
case 4:
cout << "Enter Book ID: ";
cin >> id;
[Link]();
cout << "Enter Student Name: ";
getline(cin, studentName);
[Link](id, studentName);
break;
case 5:
cout << "Enter Book ID: ";
cin >> id;
[Link](id);
break;
case 6:
[Link]();
break;
case 7:
cout << "Enter Book ID: ";
cin >> id;
[Link](id);
break;
case 8:
return 0;
default:
cout << "Invalid choice. Please try again.\n";
return 0;