Library Management System in C++

1. Introduction

In this blog post, we'll develop a simple Library Management System in C++. This system will handle operations including adding books to the library collection, borrowing books, returning books, and listing all the books. Through this project, you'll learn how to apply object-oriented programming concepts in C++ to solve real-world problems.

Library Management System in C++

2. Program Steps

1. Define a Book class with attributes like title, author, and status (borrowed/available).

2. Implement functions to add books to the library, borrow books, return books, and display all books.

3. Use a vector to store and manage the collection of books.

4. Develop a user-friendly menu-driven interface for users to interact with the library system.

3. Code Program

#include <iostream>
#include <vector>
#include <string>

using namespace std;

class Book {
public:
    string title;
    string author;
    bool isBorrowed;

    Book(string title, string author): title(title), author(author), isBorrowed(false) {}

    void borrowBook() {
        if(!isBorrowed) {
            isBorrowed = true;
            cout << "Book borrowed successfully." << endl;
        } else {
            cout << "This book is already borrowed." << endl;
        }
    }

    void returnBook() {
        if(isBorrowed) {
            isBorrowed = false;
            cout << "Book returned successfully." << endl;
        } else {
            cout << "This book was not borrowed." << endl;
        }
    }

    void displayBook() const {
        cout << "Title: " << title << ", Author: " << author << ", Status: " << (isBorrowed ? "Borrowed" : "Available") << endl;
    }
};

vector<Book> library;

void addBook() {
    string title, author;
    cout << "Enter the title of the book: ";
    getline(cin, title);
    cout << "Enter the author of the book: ";
    getline(cin, author);
    library.emplace_back(title, author);
    cout << "Book added to the library." << endl;
}

void borrowBook() {
    string title;
    cout << "Enter the title of the book you want to borrow: ";
    getline(cin, title);
    for(auto &book : library) {
        if(book.title == title && !book.isBorrowed) {
            book.borrowBook();
            return;
        }
    }
    cout << "Book not found or already borrowed." << endl;
}

void returnBook() {
    string title;
    cout << "Enter the title of the book you are returning: ";
    getline(cin, title);
    for(auto &book : library) {
        if(book.title == title && book.isBorrowed) {
            book.returnBook();
            return;
        }
    }
    cout << "Book not found or was not borrowed." << endl;
}

void displayBooks() {
    for(const auto &book : library) {
        book.displayBook();
    }
}

int main() {
    int choice;
    do {
        cout << "Library Management System\n";
        cout << "1. Add Book\n";
        cout << "2. Borrow Book\n";
        cout << "3. Return Book\n";
        cout << "4. List All Books\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice; cin.ignore();

        switch(choice) {
            case 1:
                addBook();
                break;
            case 2:
                borrowBook();
                break;
            case 3:
                returnBook();
                break;
            case 4:
                displayBooks();
                break;
            case 5:
                cout << "Exiting the system." << endl;
                break;
            default:
                cout << "Invalid choice, please try again." << endl;
        }
    } while(choice != 5);

    return 0;
}

Output:

Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 1
Enter the title of the book: Learn C++
Enter the author of the book: Ramesh
Book added to the library.
Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 1
Enter the title of the book: Learn Python
Enter the author of the book: Ramesh
Book added to the library.
Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 2
Enter the title of the book you want to borrow: Learn Python
Book borrowed successfully.
Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 3
Enter the title of the book you are returning: Learn Python
Book returned successfully.
Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 4
Title: Learn C++, Author: Ramesh, Status: Available
Title: Learn Python, Author: Ramesh, Status: Available
Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 2
Enter the title of the book you want to borrow: Learn Python
Book borrowed successfully.
Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 4
Title: Learn C++, Author: Ramesh, Status: Available
Title: Learn Python, Author: Ramesh, Status: Borrowed
Library Management System
1. Add Book
2. Borrow Book
3. Return Book
4. List All Books
5. Exit
Enter your choice: 5
Exiting the system.

Explanation:

1. Book Class: This class represents a book in the library, with methods for borrowing and returning the book and for displaying book details.

2. Library Collection: A vector named library holds all the books in the library, allowing for dynamic management of the collection.

3. Adding Books: Users can add new books to the library by specifying the title and author.

4. Borrowing Books: Allows users to borrow a book if it's available. The status of the book is updated accordingly.

5. Returning Books: Users can return borrowed books, updating the book's status back to available.

6. Displaying All Books: Lists all books in the library along with their title, author, and availability status.

7. User Interface: The application features a menu-driven interface, enabling users to interact with the library system through a console.

This simple Library Management System in C++ demonstrates the application of object-oriented programming principles to create a practical, user-friendly application.

Comments