A library management system, often used to manage and organize books in a library, is an essential tool for both librarians and patrons. Here, we’ll create a simple library management system using the object-oriented features of C++.
Key Features:
Book Management: Add, view, update, or delete books.
Member Management: Register a new member, update member details, or delete a member.
Issue & Return Books: Members can borrow and return books.
Implementation
1. Class Definitions:
We will start with the classes Book and Member.
#include <iostream>
#include <vector>
#include <string>
class Book {
private:
int id;
std::string title;
std::string author;
bool isAvailable;
public:
Book(int id, const std::string& title, const std::string& author) : id(id), title(title), author(author), isAvailable(true) {}
int getId() const { return id; }
std::string getTitle() const { return title; }
std::string getAuthor() const { return author; }
bool available() const { return isAvailable; }
void setAvailability(bool avail) { isAvailable = avail; }
};
class Member {
private:
int memberId;
std::string memberName;
std::vector<Book> borrowedBooks;
public:
Member(int id, const std::string& name) : memberId(id), memberName(name) {}
int getId() const { return memberId; }
std::string getName() const { return memberName; }
void borrowBook(const Book& book) { borrowedBooks.push_back(book); }
void returnBook(int bookId);
};
void Member::returnBook(int bookId) {
for (size_t i = 0; i < borrowedBooks.size(); ++i) {
if (borrowedBooks[i].getId() == bookId) {
borrowedBooks.erase(borrowedBooks.begin() + i);
break;
}
}
}
2. Library Class:
This is where the main logic of our library system will reside.
class Library {
private:
std::vector<Book> books;
std::vector<Member> members;
public:
void addBook(const Book& book) { books.push_back(book); }
void registerMember(const Member& member) { members.push_back(member); }
Book* findBook(int bookId);
Member* findMember(int memberId);
void issueBook(int memberId, int bookId);
void returnBook(int memberId, int bookId);
};
Book* Library::findBook(int bookId) {
for (auto& book : books) {
if (book.getId() == bookId && book.available()) {
return &book;
}
}
return nullptr;
}
Member* Library::findMember(int memberId) {
for (auto& member : members) {
if (member.getId() == memberId) {
return &member;
}
}
return nullptr;
}
void Library::issueBook(int memberId, int bookId) {
Member* member = findMember(memberId);
Book* book = findBook(bookId);
if (member && book) {
member->borrowBook(*book);
book->setAvailability(false);
}
}
void Library::returnBook(int memberId, int bookId) {
Member* member = findMember(memberId);
Book* book = findBook(bookId);
if (member && book) {
member->returnBook(bookId);
book->setAvailability(true);
}
}
3. Main Function:
Here, we’ll provide an interface for users to interact with the library.
int main() {
Library lib;
int choice;
do {
std::cout << "\n1. Add Book\n2. Register Member\n3. Issue Book\n4. Return Book\n5. Exit\nEnter choice: ";
std::cin >> choice;
switch (choice) {
case 1: {
int id;
std::string title, author;
std::cout << "Enter Book ID, Title, and Author: ";
std::cin >> id >> title >> author;
lib.addBook(Book(id, title, author));
break;
}
case 2: {
int id;
std::string name;
std::cout << "Enter Member ID and Name: ";
std::cin >> id >> name;
lib.registerMember(Member(id, name));
break;
}
case 3: {
int memberId, bookId;
std::cout << "Enter Member ID and Book ID to issue: ";
std::cin >> memberId >> bookId;
lib.issueBook(memberId, bookId);
break;
}
case 4: {
int memberId, bookId;
std::cout << "Enter Member ID and Book ID to return: ";
std::cin >> memberId >> bookId;
lib.returnBook(memberId, bookId);
break;
}
case 5: std::cout << "Goodbye!\n"; break;
default: std::cout << "Invalid choice!\n"; break;
}
} while (choice != 5);
return 0;
}
Complete Source Code
#include <iostream>
#include <vector>
#include <string>
// Book class
class Book {
private:
int id;
std::string title;
std::string author;
bool isAvailable;
public:
Book(int id, const std::string& title, const std::string& author)
: id(id), title(title), author(author), isAvailable(true) {}
int getId() const { return id; }
std::string getTitle() const { return title; }
std::string getAuthor() const { return author; }
bool available() const { return isAvailable; }
void setAvailability(bool avail) { isAvailable = avail; }
};
// Member class
class Member {
private:
int memberId;
std::string memberName;
std::vector<Book> borrowedBooks;
public:
Member(int id, const std::string& name) : memberId(id), memberName(name) {}
int getId() const { return memberId; }
std::string getName() const { return memberName; }
void borrowBook(const Book& book) { borrowedBooks.push_back(book); }
void returnBook(int bookId);
};
void Member::returnBook(int bookId) {
for (size_t i = 0; i < borrowedBooks.size(); ++i) {
if (borrowedBooks[i].getId() == bookId) {
borrowedBooks.erase(borrowedBooks.begin() + i);
break;
}
}
}
// Library class
class Library {
private:
std::vector<Book> books;
std::vector<Member> members;
public:
void addBook(const Book& book) { books.push_back(book); }
void registerMember(const Member& member) { members.push_back(member); }
Book* findBook(int bookId);
Member* findMember(int memberId);
void issueBook(int memberId, int bookId);
void returnBook(int memberId, int bookId);
};
Book* Library::findBook(int bookId) {
for (auto& book : books) {
if (book.getId() == bookId && book.available()) {
return &book;
}
}
return nullptr;
}
Member* Library::findMember(int memberId) {
for (auto& member : members) {
if (member.getId() == memberId) {
return &member;
}
}
return nullptr;
}
void Library::issueBook(int memberId, int bookId) {
Member* member = findMember(memberId);
Book* book = findBook(bookId);
if (member && book) {
member->borrowBook(*book);
book->setAvailability(false);
std::cout << "Book issued successfully!" << std::endl;
} else {
std::cout << "Failed to issue the book." << std::endl;
}
}
void Library::returnBook(int memberId, int bookId) {
Member* member = findMember(memberId);
Book* book = findBook(bookId);
if (member && book) {
member->returnBook(bookId);
book->setAvailability(true);
std::cout << "Book returned successfully!" << std::endl;
} else {
std::cout << "Failed to return the book." << std::endl;
}
}
// Main function
int main() {
Library lib;
int choice;
do {
std::cout << "\n1. Add Book\n2. Register Member\n3. Issue Book\n4. Return Book\n5. Exit\nEnter choice: ";
std::cin >> choice;
std::cin.ignore(); // Clear input buffer
switch (choice) {
case 1: {
int id;
std::string title, author;
std::cout << "Enter Book ID: ";
std::cin >> id;
std::cin.ignore();
std::cout << "Enter Book Title: ";
getline(std::cin, title);
std::cout << "Enter Book Author: ";
getline(std::cin, author);
lib.addBook(Book(id, title, author));
break;
}
case 2: {
int id;
std::string name;
std::cout << "Enter Member ID: ";
std::cin >> id;
std::cin.ignore();
std::cout << "Enter Member Name: ";
getline(std::cin, name);
lib.registerMember(Member(id, name));
break;
}
case 3: {
int memberId, bookId;
std::cout << "Enter Member ID: ";
std::cin >> memberId;
std::cout << "Enter Book ID to issue: ";
std::cin >> bookId;
lib.issueBook(memberId, bookId);
break;
}
case 4: {
int memberId, bookId;
std::cout << "Enter Member ID: ";
std::cin >> memberId;
std::cout << "Enter Book ID to return: ";
std::cin >> bookId;
lib.returnBook(memberId, bookId);
break;
}
case 5: std::cout << "Goodbye!\n"; break;
default: std::cout << "Invalid choice!\n"; break;
}
} while (choice != 5);
return 0;
}
That's it! This simple library management system allows for basic operations. To enhance this project further, you can integrate features like searching, book categorization, member history, late fee calculations, and more. Using C++, the opportunities to expand this system are vast and limited only by your imagination!
Comments
Post a Comment