#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;
};
class Library {
private:
std::vector<Book> books;
std::list<Patron> patrons;
std::queue<Transaction> transactions;
std::unordered_map<int, int> bookAvailability;
public:
void addBook(const std::string& title, const std::string& author, int quantity) {
Book book;
book.id = books.size() + 1;
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;
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);
library.processTransaction(1, 2, false);
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.