Introduction
The Library Management System is a software application developed using C++ programming language to manage library books and members. It provides functionalities like adding, viewing, and managing books, as well as managing member details, borrowing and returning books. This system can be used by libraries to efficiently keep track of books and members, ensuring proper inventory management.
Objective
The primary objective of this system is to allow easy management of books and members in a library, including functionalities such as:
- Adding new books to the system.
- Displaying the list of books and members.
- Allowing members to borrow and return books.
- Viewing the status of borrowed books.
Code for Library Management System
#include #include #include using namespace std; class Book { public: string title; string author; bool isAvailable; Book(string t, string a) { title = t; author = a; isAvailable = true; } }; class Member { public: string name; int id; vector<Book*> borrowedBooks; Member(string n, int i) { name = n; id = i; } void borrowBook(Book& book) { if (book.isAvailable) { borrowedBooks.push_back(&book); book.isAvailable = false; cout << "Book borrowed: " << book.title << endl; } else { cout << "Sorry, the book is not available." << endl; } } void returnBook(Book& book) { for (size_t i = 0; i < borrowedBooks.size(); ++i) { if (borrowedBooks[i] == &book) { borrowedBooks.erase(borrowedBooks.begin() + i); book.isAvailable = true; cout << "Book returned: " << book.title << endl; return; } } cout << "This book was not borrowed by you." << endl; } }; class Library { private: vector books; vector members; public: void addBook(string title, string author) { books.push_back(Book(title, author)); cout << "Book added: " << title << endl; } void addMember(string name, int id) { members.push_back(Member(name, id)); cout << "Member added: " << name << endl; } void showBooks() { cout << "Available books in the library:" << endl; for (auto& book : books) { cout << book.title << " by " << book.author; if (book.isAvailable) { cout << " (Available)"; } else { cout << " (Not Available)"; } cout << endl; } } void showMembers() { cout << "Library members:" << endl; for (auto& member : members) { cout << member.name << " (ID: " << member.id << ")" << endl; } } Book* getBookByTitle(string title) { for (auto& book : books) { if (book.title == title) { return &book; } } return nullptr; } Member* getMemberById(int id) { for (auto& member : members) { if (member.id == id) { return &member; } } return nullptr; } }; int main() { Library library; // Adding books to the library library.addBook("The Catcher in the Rye", "J.D. Salinger"); library.addBook("To Kill a Mockingbird", "Harper Lee"); // Adding members to the library library.addMember("John Doe", 1); library.addMember("Jane Smith", 2); // Display available books and members library.showBooks(); library.showMembers(); // Member borrow and return books Member* john = library.getMemberById(1); Book* book1 = library.getBookByTitle("The Catcher in the Rye"); if (john && book1) { john->borrowBook(*book1); // John borrows a book library.showBooks(); john->returnBook(*book1); // John returns a book library.showBooks(); } return 0; }
Program Explanation
The program consists of three main classes: Book, Member, and Library.
- Book Class: Represents a book with attributes such as title, author, and availability status. It provides the details of each book.
- Member Class: Represents a library member with a name, ID, and a list of borrowed books. It allows borrowing and returning of books.
- Library Class: Manages a collection of books and members. It provides functionalities to add books, members, view available books, and manage borrow/return operations.
In the main function, we create a library instance, add some books and members, and demonstrate the borrow and return operations. It also shows how to list available books and members.
How to Run the Program
To run this program:
- Save the code in a file named LibraryManagementSystem.cpp.
- Compile the program using a C++ compiler (e.g., g++):
g++ LibraryManagementSystem.cpp -o LibraryManagementSystem
- Run the program:
./LibraryManagementSystem