Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include <iostream>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <unordered_map>

class Book {
public:
    int id;
    std::string title;
    std::string author;
    int quantity;
};

class Patron {
public:
    int id;
    std::string name;
};

class Transaction {
public:
    int bookId;
    int patronId;
    bool checkOut; // true for check-out, false for check-in
};

class Library {
private:
    std::vector<Book> books;
    std::list<Patron> patrons;
    std::queue<Transaction> transactions;
    std::unordered_map<int, int> bookAvailability; // Map bookId to available quantity

public:
    void addBook(const std::string& title, const std::string& author, int quantity) {
        Book book;
        book.id = books.size() + 1; // Simple unique identifier
        book.title = title;
        book.author = author;
        book.quantity = quantity;

        books.push_back(book);
        bookAvailability[book.id] = quantity;
    }

    void removeBook(int bookId) {
        auto it = std::find_if(books.begin(), books.end(), [bookId](const Book& book) {
            return book.id == bookId;
        });

        if (it != books.end()) {
            bookAvailability.erase(bookId);
            books.erase(it);
            std::cout << "Book removed successfully." << std::endl;
        } else {
            std::cout << "Book not found." << std::endl;
        }
    }

    void displayBooks() {
        std::cout << "Library Inventory:" << std::endl;
        for (const Book& book : books) {
            std::cout << "ID: " << book.id << ", Title: " << book.title << ", Author: " << book.author
                      << ", Quantity: " << bookAvailability[book.id] << std::endl;
        }
        std::cout << std::endl;
    }

    void addPatron(const std::string& name) {
        Patron patron;
        patron.id = patrons.size() + 1; // Simple unique identifier
        patron.name = name;

        patrons.push_back(patron);
        std::cout << "Patron added successfully." << std::endl;
    }

    void removePatron(int patronId) {
        auto it = std::find_if(patrons.begin(), patrons.end(), [patronId](const Patron& patron) {
            return patron.id == patronId;
        });

        if (it != patrons.end()) {
            patrons.erase(it);
            std::cout << "Patron removed successfully." << std::endl;
        } else {
            std::cout << "Patron not found." << std::endl;
        }
    }

    void displayPatrons() {
        std::cout << "Patron List:" << std::endl;
        for (const Patron& patron : patrons) {
            std::cout << "ID: " << patron.id << ", Name: " << patron.name << std::endl;
        }
        std::cout << std::endl;
    }

    void processTransaction(int bookId, int patronId, bool checkOut) {
        Transaction transaction;
        transaction.bookId = bookId;
        transaction.patronId = patronId;
        transaction.checkOut = checkOut;

        transactions.push(transaction);

        if (checkOut) {
            if (bookAvailability[bookId] > 0) {
                bookAvailability[bookId]--;
                std::cout << "Book checked out successfully." << std::endl;
            } else {
                std::cout << "Book not available for check-out." << std::endl;
            }
        } else {
            bookAvailability[bookId]++;
            std::cout << "Book checked in successfully." << std::endl;
        }
    }

    void generateReports() {
        std::cout << "Inventory Report:" << std::endl;
        for (const Book& book : books) {
            std::cout << "Title: " << book.title << ", Available Quantity: " << bookAvailability[book.id] << std::endl;
        }

        std::cout << "Transaction Report:" << std::endl;
        while (!transactions.empty()) {
            Transaction transaction = transactions.front();
            transactions.pop();
            std::string action = transaction.checkOut ? "Checked Out" : "Checked In";
            std::cout << "Book ID: " << transaction.bookId << ", Patron ID: " << transaction.patronId
                      << ", Action: " << action << std::endl;
        }

        std::cout << std::endl;
    }
};

int main() {
    Library library;

    library.addBook("The Great Gatsby", "F. Scott Fitzgerald", 5);
    library.addBook("To Kill a Mockingbird", "Harper Lee", 3);
    library.addBook("1984", "George Orwell", 8);

    library.displayBooks();

    library.addPatron("John Doe");
    library.addPatron("Jane Doe");

    library.displayPatrons();

    library.processTransaction(1, 1, true); // Check out The Great Gatsby to John Doe
    library.processTransaction(1, 2, false); // Check in The Great Gatsby from Jane Doe

    library.generateReports();

    return 0;
}


What I have tried:

I couldn't find the problem with this code.

the question is:
Design and implement a program using c++ to manage a library's inventory using data structures such as arrays, linked lists, queues, stacks, and trees. The program should allow librarians to add, remove, and search for books in the inventory and check books in and out to library patrons. Each book should have a unique identifier, title, author, and availability status. The program should also be able to handle multiple copies of the same book and keep track of their availability. Additionally, the program should be able to generate reports on the inventory, such as the number of copies of a particular book, the number of books checked out to a specific patron, and the overall status of the inventory.
Posted
Updated 30-Nov-23 3:32am
v2
Comments
Richard MacCutchan 30-Nov-23 9:16am    
"I couldn't find the problem with this code."
So what is your actual question?
Rick York 30-Nov-23 11:45am    
I recommend that you figure out how to read data from files. I would make three files : Library.txt, Patrons.txt, and Transactions.txt and each of those would have a list of entries for you to inject into your program. This will make your testing iterations much more flexible and thorough.

Adding the algorithm standard header made your code compile and run fine on my Linux box.
You should report exactly what is the problem you're experiencing.
That is what is the observed behavior and what is the expected one ?
 
Share this answer
 
one problem is ofcourse that Jane Doe is checking a book in, which she hasnt check out. So you need to link the outchecked books to the patron.

And you havent an unique for every book, but only for every title of the books. You cant differentiate between all "The Great Gatsby" books. There are five out it. Tip: use some hash of the string of author and title and append the number to it.
So you will have 16 books in your library and not only 3.

tip: make some println output in your code for easier debugging.
 
Share this answer
 
The program seems to compile without any problems and the process does not throw any errors.
As Karsten has already pointed out, you should know exactly which book someone has borrowed and which has been returned.
Mr. Doe borrowing a book that his wife returns would not be a problem. Usually you only remember who borrows a book - not who returns it later. It should also be possible to borrow several books with the same title, but you want to know exactly which one was returned.

Normally, a time stamp should be recorded both when the book is issued and when it is returned, as the loan period may be limited. Who then returns it, on the other hand, is not normally recorded as it is irrelevant.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900