Untitled

mail@pastecode.io avatar
unknown
c_cpp
23 days ago
7.0 kB
1
Indexable
Never
/*

Project: LIBRARY MANAGEMENT SYSTEM

Features:
Add New Book
Delete Book
Display All Book
Search for a Book
Issue a Book
Return a Book

*/

#include <iostream>
#include <string>
#include <vector>
#include <limits>
using namespace std;

struct book
{
    unsigned int id = 0;
    string title = "";
    string author = "";
    bool isIssued = false;
};

static void add_book(vector<book> &books);
static void delete_book(vector<book> &books);
static void display_book(vector<book> &books);
static void search_book(vector<book> &books);
static void issue_book(vector<book> &books, unsigned int id);
static void return_book(vector<book> &books);
static bool redundancy_checker(vector<book> &books);

int main()
{
    static vector<book> books;
    unsigned int choice = 0;

    while (true)
    {
        cout << "\n-----WELCOME TO LIBRARY MANAGEMENT SYSTEM-----\n";
        cout << "\nPlease select from the below options:\n";
        cout << "1. Add Book" << endl;
        cout << "2. Delete Book" << endl;
        cout << "3. Display Book List" << endl;
        cout << "4. Search/Issue Book" << endl;
        cout << "5. Return Book" << endl;
        cout << "6. Exit Portal" << endl;
        cout << "\nEnter your choice: ";
        while (!(cin >> choice))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Please enter a valid input : ";
        }
        if (choice == 1)
        {
            add_book(books);
        }

        else if (choice == 2)
        {
            delete_book(books);
        }
        else if (choice == 3)
        {
            display_book(books);
        }
        else if (choice == 4)
        {
            search_book(books);
        }
        else if (choice == 5)
        {
            return_book(books);
        }
        else if (choice == 6)
        {
            return 0;
        }
        else
        {
            cout << "Please enter a valid choice out of the given options" << endl;
        }
    }

    add_book(books);
    add_book(books);
    delete_book(books);
    display_book(books);
    search_book(books);
    return 0;
}

static bool redundancy_checker(vector<book> &books, unsigned int id)
{
    for (auto &book : books)
    {
        if (book.id == id)
        {
            return true;
        }
    }
    return false;
}
static void add_book(vector<book> &books)
{

    book newBook;
    while (true)
    {
        cout << "Enter ID : ";
        while (!(cin >> newBook.id))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Enter a valid value : ";
        }
        if (redundancy_checker(books, newBook.id))
        {
            cout << "A book with same ID has already been registered.\n"
                 << endl;
        }
        else
        {
            break;
        }
    }

    cin.ignore();
    cout << "Enter the title of the book : ";
    getline(cin, newBook.title);
    cout << "Enter the author name : ";
    getline(cin, newBook.author);

    books.push_back(newBook);
    cout << "\nAdded Successfully !!" << endl;
    cout << "Book ID : " << newBook.id << endl;
    cout << "Book Name : " << newBook.title << endl;
    cout << "Author : " << newBook.author << endl;
    for (int i = 0; i < (newBook.title).length() + 9; i++)
    {
        cout << "-";
    }

    cout << "\n"
         << endl;
}
static void delete_book(vector<book> &books)
{
    unsigned int id;
    cout << "Enter the ID of the book which you want to delete : ";
    while (!(cin >> id))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter a valid value : ";
    }

    for (auto it = books.begin(); it != books.end();)
    {
        if (it->id == id)
        {
            it = books.erase(it);
            cout << "Book deleted successfully!\n";
            return; // Exit after deleting the book
        }
        else
        {
            ++it;
        }
    }
    cout << "Book not found!\n";
}
static void display_book(vector<book> &books)
{
    if (books.size() == 0)
    {
        cout << "No Books Available !!" << endl;
    }
    else
    {
        cout << "\nID     Title" << endl;
        cout << "-------------------" << endl;
        for (auto &book : books)
        {
            cout << book.id << "    " << book.title << endl;
        }
        cout << "\n"
             << endl;
    }
}
static void search_book(vector<book> &books)
{
    unsigned int id;
    cout << "Enter the book ID : ";
    while (!(cin >> id))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter a valid value : ";
    }
    for (auto &book : books)
    {
        if (book.id == id)
        {
            cout << "Book Found ";
            if (book.isIssued == false)
            {
                cout << "and can be issued" << endl;
                issue_book(books, id);
            }
            else
            {
                cout << "but it is issued already." << endl;
            }
            return;
        }
        else
        {
            cout << "Sorry book with given ID: " << id << " could not be found." << endl;
        }
    }
}
static void issue_book(vector<book> &books, unsigned int id)
{
    string choice = "";
    cout << "Do you want to issues this book ?\nEnter your choice in yes or no : ";
    cin >> choice;
    while ((choice != "yes") && (choice != "no"))
    {
        cout << "Please enter yes or no : ";
        cin >> choice;
    }
    if (choice == "yes")
    {
        for (auto &book : books)
        {
            if (book.id == id)
            {
                book.isIssued == true;
                cout << "Book with ID: " << book.id << " has been issued successfully." << endl;
                return;
            }
        }
    }
    return;
}
static void return_book(vector<book> &books)
{
    unsigned int id;
    cout << "Enter the ID of the book you want to return : ";
    while (!(cin >> id))
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Enter a valid ID : ";
    }
    for (auto &book : books)
    {
        if (book.id == id)
        {
            if (book.isIssued == true)
            {
                book.isIssued == false;
                cout << "Book with ID: " << book.id << " has been returned successfully." << endl;
                return;
            }
            else
            {
                cout << "This book has not been issued" << endl;
                return;
            }
        }
    }
    cout << "Book with ID: " << id << " does not exists." << endl;
}
Leave a Comment