Intro – Library Management System
A Library Management System (LMS) is software designed to automate the management of a library’s operations, including cataloging, borrowing, returning, and managing books and other materials. Implementing an Library Management System in C++ involves using data structure to store information about books, users, ands transaction as well as providing functionalities to interact with this data.
Features of the Project
Here is the features of the Project,
- Book Management
- User Management
- Transaction Management
- Reporting
Codes for The Project
Code for the Project
#include <iostream> #include <vector> #include <string> #include <ctime> // Book class class Book { public: int id; std::string title; std::string author; std::string genre; bool available; Book(int id, const std::string& title, const std::string& author, const std::string& genre) : id(id), title(title), author(author), genre(genre), available(true) {} }; // User class class User { public: int id; std::string name; std::string contactInfo; std::vector<int> borrowedBooks; User(int id, const std::string& name, const std::string& contactInfo) : id(id), name(name), contactInfo(contactInfo) {} }; // Transaction class class Transaction { public: int userId; int bookId; time_t issueDate; time_t returnDate; Transaction(int userId, int bookId) : userId(userId), bookId(bookId) { issueDate = std::time(nullptr); returnDate = 0; } }; // Library class class Library { private: std::vector<Book> books; std::vector<User> users; std::vector<Transaction> transactions; public: void addBook(int id, const std::string& title, const std::string& author, const std::string& genre) { books.push_back(Book(id, title, author, genre)); std::cout << "Book added successfully.\n"; } void addUser(int id, const std::string& name, const std::string& contactInfo) { users.push_back(User(id, name, contactInfo)); std::cout << "User added successfully.\n"; } void issueBook(int userId, int bookId) { // Check if the book is available for (auto& book : books) { if (book.id == bookId && book.available) { book.available = false; transactions.push_back(Transaction(userId, bookId)); users[userId].borrowedBooks.push_back(bookId); std::cout << "Book issued successfully.\n"; return; } } std::cout << "Book not available or invalid book ID.\n"; } void returnBook(int userId, int bookId) { // Check if the user has borrowed the book for (auto& transaction : transactions) { if (transaction.userId == userId && transaction.bookId == bookId && transaction.returnDate == 0) { transaction.returnDate = std::time(nullptr); // Mark the book as available again for (auto& book : books) { if (book.id == bookId) { book.available = true; break; } } std::cout << "Book returned successfully.\n"; return; } } std::cout << "Book not borrowed by this user or invalid book ID.\n"; } void displayBooks() { std::cout << "\n--- List of Books ---\n"; for (const auto& book : books) { std::cout << "ID: " << book.id << ", Title: " << book.title << ", Author: " << book.author; std::cout << ", Genre: " << book.genre << ", Available: " << (book.available ? "Yes" : "No") << "\n"; } } void displayUsers() { std::cout << "\n--- List of Users ---\n"; for (const auto& user : users) { std::cout << "ID: " << user.id << ", Name: " << user.name << ", Contact Info: " << user.contactInfo << "\n"; std::cout << "Borrowed Books: "; for (int bookId : user.borrowedBooks) { std::cout << bookId << " "; } std::cout << "\n"; } } }; int main() { Library library; int choice, id, userId, bookId; std::string title, author, genre, name, contactInfo; while (true) { std::cout << "\n1. Add Book\n2. Add User\n3. Issue Book\n4. Return Book\n5. Display Books\n6. Display Users\n7. Exit\n"; std::cout << "Enter your choice: "; std::cin >> choice; switch (choice) { case 1: std::cout << "Enter Book ID: "; std::cin >> id; std::cout << "Enter Title: "; std::cin.ignore(); std::getline(std::cin, title); std::cout << "Enter Author: "; std::getline(std::cin, author); std::cout << "Enter Genre: "; std::getline(std::cin, genre); library.addBook(id, title, author, genre); break; case 2: std::cout << "Enter User ID: "; std::cin >> id; std::cout << "Enter Name: "; std::cin.ignore(); std::getline(std::cin, name); std::cout << "Enter Contact Info: "; std::getline(std::cin, contactInfo); library.addUser(id, name, contactInfo); break; case 3: std::cout << "Enter User ID: "; std::cin >> userId; std::cout << "Enter Book ID: "; std::cin >> bookId; library.issueBook(userId, bookId); break; case 4: std::cout << "Enter User ID: "; std::cin >> userId; std::cout << "Enter Book ID: "; std::cin >> bookId; library.returnBook(userId, bookId); break; case 5: library.displayBooks(); break; case 6: library.displayUsers(); break; case 7: std::cout << "Exiting...\n"; return 0; default: std::cout << "Invalid choice. Please try again.\n"; } } return 0; }