cplusplus
cplusplus

 

Introduction

In a bookstore, managing the inventory of books is crucial to ensure efficient sales operations. A Book Inventory System allows the bookstore to keep track of books in stock, manage their details, and even perform searches for specific books. This system enables easy access to the current inventory status, updates, and deletions of book records, and can be integrated with other software for better management.

Objective

The goal of this project is to develop a simple Book Inventory System using C++ programming language. The program will allow the user to add, view, search, and delete books in the inventory. By managing the book records, the bookstore can effectively control the stock, helping to prevent inventory issues and enhance operations.

Book Inventory System Code in C++

#include 
#include 
#include 
using namespace std;

class Book {
public:
    string title;
    string author;
    int quantity;
    float price;

    Book(string t, string a, int q, float p) {
        title = t;
        author = a;
        quantity = q;
        price = p;
    }
};

class BookstoreInventory {
private:
    vector inventory;

public:
    void addBook(string title, string author, int quantity, float price) {
        Book newBook(title, author, quantity, price);
        inventory.push_back(newBook);
        cout << "Book added successfully!" << endl;
    }

    void viewBooks() {
        if (inventory.empty()) {
            cout << "No books in inventory." << endl;
            return;
        }
        cout << "Books in Inventory: " << endl;
        for (int i = 0; i < inventory.size(); i++) {
            cout << "Title: " << inventory[i].title << ", Author: " << inventory[i].author
                 << ", Quantity: " << inventory[i].quantity << ", Price: $" << inventory[i].price << endl;
        }
    }

    void searchBook(string title) {
        bool found = false;
        for (int i = 0; i < inventory.size(); i++) {
            if (inventory[i].title == title) {
                cout << "Book found: " << inventory[i].title << ", Author: " << inventory[i].author
                     << ", Quantity: " << inventory[i].quantity << ", Price: $" << inventory[i].price << endl;
                found = true;
                break;
            }
        }
        if (!found) {
            cout << "Book not found!" << endl;
        }
    }

    void deleteBook(string title) {
        for (int i = 0; i < inventory.size(); i++) {
            if (inventory[i].title == title) {
                inventory.erase(inventory.begin() + i);
                cout << "Book deleted successfully!" << endl;
                return;
            }
        }
        cout << "Book not found!" << endl;
    }
};

int main() {
    BookstoreInventory store;
    int choice;
    string title, author;
    int quantity;
    float price;

    while (true) {
        cout << "\nBookstore Inventory System\n";
        cout << "1. Add Book\n";
        cout << "2. View Books\n";
        cout << "3. Search Book\n";
        cout << "4. Delete Book\n";
        cout << "5. Exit\n";
        cout << "Enter your choice: "; cin >> choice;

        switch (choice) {
            case 1:
                cout << "Enter book title: ";
                cin.ignore();
                getline(cin, title);
                cout << "Enter author name: ";
                getline(cin, author);
                cout << "Enter quantity: "; cin >> quantity;
                cout << "Enter price: "; cin >> price;
                store.addBook(title, author, quantity, price);
                break;
            case 2:
                store.viewBooks();
                break;
            case 3:
                cout << "Enter book title to search: ";
                cin.ignore();
                getline(cin, title);
                store.searchBook(title);
                break;
            case 4:
                cout << "Enter book title to delete: ";
                cin.ignore();
                getline(cin, title);
                store.deleteBook(title);
                break;
            case 5:
                cout << "Exiting the system..." << endl;
                return 0;
            default:
                cout << "Invalid choice. Please try again." << endl;
        }
    }

    return 0;
}

Program Structure and Explanation

This Book Inventory System is built using the C++ programming language. It has a class Book that defines the properties of a book, such as title, author, quantity, and price. The BookstoreInventory class manages the book collection and provides functionalities like adding a new book, viewing the list of books, searching for a specific book by title, and deleting a book from the inventory.

Steps to Run the Program

  1. Make sure you have a C++ compiler installed (e.g., GCC or MSVC).
  2. Copy and paste the code into a C++ file, such as book_inventory.cpp.
  3. Open a terminal/command prompt and navigate to the directory where the file is saved.
  4. Compile the program using the following command (for GCC compiler): g++ book_inventory.cpp -o book_inventory.
  5. Run the program with: ./book_inventory on Linux/macOS or book_inventory.exe on Windows.
  6. Follow the on-screen menu to interact with the system.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)