#include <iostream>
#include <string>
using namespace std;
class Book {
private:
int id;
string title;
string author;
bool available;
int patronId;
int numCopies;
public:
Book(int id, const string& title, const string& author, int numCopies) {
this->id = id;
this->title = title;
this->author = author;
this->available = true;
this->patronId = -1;
this->numCopies = numCopies;
}
int getId() const {
return id;
}
string getTitle() const {
return title;
}
string getAuthor() const {
return author;
}
bool isAvailable() const {
return available;
}
void setAvailable(bool available) {
this->available = available;
}
int getPatronId() const {
return patronId;
}
void setPatronId(int patronId) {
this->patronId = patronId;
}
};
class LibraryInventory {
private:
static const int MAX_BOOKS = 100; // maximum number of books in inventory
Book* books[MAX_BOOKS]; // array of Book pointers
int numBooks; // number of books in inventory
struct Node {
Book* book;
Node* next;
Node(Book* b, Node* n = NULL) : book(b), next(n) {}
};
Node* head; // pointer to the first node in the linked list
public:
LibraryInventory() {
numBooks = 0;
head = NULL;
}
~LibraryInventory() {
for (int i = 0; i < numBooks; i++) {
delete books[i];
}
Node* curr = head;
while (curr != NULL) {
Node* temp = curr;
curr = curr->next;
delete temp->book;
delete temp;
}
}
void addBook(int id, const string& title, const string& author, int numCopies) {
for (int i = 0; i < numCopies; i++) {
Book* book = new Book(id, title, author, numCopies);
books[numBooks] = book;
numBooks++;
head = new Node(book, head);
}
cout << numCopies << " copies of book added successfully." << endl;
}
void removeBook(int id) {
int index = findBookIndex(id);
if (index == -1) {
cout << "Book not found." << endl;
return;
}
// shift remaining books to fill the gap
Node* prev = NULL;
Node* curr = head;
while (curr != NULL && curr->book->getId() != id) {
prev = curr;
curr = curr->next;
}
if (curr == NULL) {
cout << "Book not found." << endl;
return;
}
if (prev == NULL) {
head = curr->next;
}
else {
prev->next = curr->next;
}
for (int i = index; i < numBooks; i++) {
books[i] = books[i + 1];
}
delete books[numBooks - 1];
numBooks--;
cout << "Book removed successfully." << endl;
}
void searchBook(int id) {
int index = findBookIndex(id);
if (index == -1) {
cout << "Book not found." << endl;
return;
}
Book* book = books[index];
cout << "Book found:" << endl;
cout << "ID: " << book->getId() << endl;
cout << "Title: " << book->getTitle() << endl;
cout << "Author: " << book->getAuthor() << endl;
cout << "Availability: " << (book->isAvailable() ? "Available" : "Not available") << endl;
}
void checkOutBook(int id, int patronId) {
int numCheckedOut = 0;
int index = findBookIndex(id);
if (index == -1) {
cout << "Book not found." << endl;
return;
}
Book* book = books[index];
book->setAvailable(true);
book->setPatronId(patronId);
if (!book->isAvailable()) {
cout << "Book is already checked out." << endl;
return;
}
book->setAvailable(false);
book->setPatronId(patronId);
cout << "Book checked out successfully." << endl;
numBooks--;
numCheckedOut++;
}
void checkInBook(int id) {
int index = findBookIndex(id);
if (index == -1) {
cout << "Book not found." << endl;
return;
}
Book* book = books[index];
book->setAvailable(false);
book->setPatronId(-1);
if (book->isAvailable()) {
cout << "Book is already checked in." << endl;
return;
}
book->setAvailable(true);
book->setPatronId(-1);
cout << "Book checked in successfully." << endl;
numBooks++;
}
void displayInventoryStatus() {
int numAvailable = 0;
int numCheckedOut = 0;
int numCopies = 0;
int i=0;
cout << "Inventory:" << endl;
Book* book = books[i];
for ( i ; i < numBooks; i++){
numCopies++;
if(books[i]->isAvailable()){
numAvailable++;
}else{
numCheckedOut++;
}
if(i==numBooks-1||books[i]->getId()!=books[i+1]->getId()){
cout << "Book ID :" << books[i]->getId()<< ", " << numCopies << " copies, " << ", Title: " << books[i]->getTitle() << " , Author : "<< books[i]->getAuthor() << endl;
numCopies = 0;
numAvailable=0;
}
}
cout << "Inventory status:" << endl;
cout << "Total books: " << numBooks << endl;
cout << "Available books: " << numAvailable << endl;
cout << "Checked out books: " << numCheckedOut << endl;
}
int numCopies(int id) {
int count = 0;
for (int i = 0; i < numBooks; i++) {
if (books[i]->getId() == id) {
count++;
}
}
return count;
}
int numCheckedOut(int patronId) {
int count = 0;
for (int i = 0; i < numBooks; i++) {
if (books[i]->getPatronId() == patronId && !books[i]->isAvailable()) {
count++;
}
}
return count;
}
void inventoryStatus() {
int numAvailable = 0;
int numCheckedOut = 0;
for (int i = 0; i < numBooks; i++) {
if (books[i]->isAvailable()) {
numAvailable++;
}
else {
numCheckedOut++;
}
}
cout << "Inventory status:" << endl;
cout << "Total books: " << numBooks << endl;
cout << "Available books: " << numAvailable << endl;
cout << "Checked out books: " << numCheckedOut << endl;
}
private:
int findBookIndex(int id) {
for (int i = 0; i < numBooks; i++) {
if (books[i]->getId() == id) {
return i;
}
}
return -1;
}
};
int main() {
LibraryInventory inventory;
int choice;
do {
cout << "Library Inventory Management System" << endl;
cout << "1. Add a book" << endl;
cout << "2. Remove a book" << endl;
cout << "3. Search for a book" << endl;
cout << "4. Check out a book" << endl;
cout << "5. Check in a book" << endl;
cout << "6. Display inventory" << endl;
cout << "7. Number of books checked out by a patron" << endl;
cout << "0. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1: {
int id;
string title, author;
int numCopies;
cout << "Enter book ID: ";
cin >> id;
cout << "Enter book title: ";
cin.ignore();
getline(cin, title);
cout << "Enter book author: ";
getline(cin, author);
cout << "Enter number of copies: ";
cin >> numCopies;
inventory.addBook(id, title, author, numCopies);
break;
}
case 2: {
int id;
cout << "Enter book ID: ";
cin >> id;
inventory.removeBook(id);
break;
}
case 3: {
int id;
cout << "Enter book ID: ";
cin >> id;
inventory.searchBook(id);
break;
}
case 4: {
int id, patronId;
cout << "Enter book ID: ";
cin >> id;
cout << "Enter patron ID: ";
cin >> patronId;
inventory.checkOutBook(id, patronId);
break;
}
case 5: {
int id;
cout << "Enter book ID: ";
cin >> id;
inventory.checkInBook(id);
break;
}
case 6:
inventory.displayInventoryStatus();
break;
case 7: {
int patronId;
cout << "Enter patron ID: ";
cin >> patronId;
int numCheckedOut = inventory.numCheckedOut(patronId);
cout << "Number of books checked out by patron who has patronID : " << patronId << " Number of books :" << numCheckedOut << endl;
break;
}
case 0:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
break;
}
cout << endl;
} while (choice != 0);
return 0;
inventory.~LibraryInventory();
}