Introduction
A Book Inventory System is essential for any bookstore to keep track of the books available, manage inventory, and ensure that stock levels are maintained effectively. In this project, we’ll be building a simple book inventory system using Python. This system will allow users to add books to the inventory, view the current inventory, update book details, and remove books as necessary.
Objective
The objective of this project is to create a simple yet functional Book Inventory System using Python. This program will allow bookstore owners or managers to:
- Track the available books in the inventory.
- Add new books to the inventory.
- Update book details such as price and quantity.
- Remove books from the inventory when they are sold or out of stock.
Python Code: Book Inventory System
# Book Inventory System in Python class Book: def __init__(self, title, author, price, quantity): self.title = title self.author = author self.price = price self.quantity = quantity def update_quantity(self, quantity): self.quantity = quantity def update_price(self, price): self.price = price def display(self): return f"Title: {self.title}, Author: {self.author}, Price: ${self.price}, Quantity: {self.quantity}" class BookInventory: def __init__(self): self.inventory = [] def add_book(self, title, author, price, quantity): book = Book(title, author, price, quantity) self.inventory.append(book) def remove_book(self, title): for book in self.inventory: if book.title == title: self.inventory.remove(book) print(f"{title} has been removed from the inventory.") return print(f"Book titled {title} not found in the inventory.") def update_book(self, title, price=None, quantity=None): for book in self.inventory: if book.title == title: if price: book.update_price(price) if quantity: book.update_quantity(quantity) print(f"Details for {title} have been updated.") return print(f"Book titled {title} not found in the inventory.") def view_inventory(self): if not self.inventory: print("No books in the inventory.") else: for book in self.inventory: print(book.display()) # Main Program Loop if __name__ == "__main__": inventory = BookInventory() while True: print("\n1. Add Book") print("2. Remove Book") print("3. Update Book") print("4. View Inventory") print("5. Exit") choice = input("Choose an option: ") if choice == '1': title = input("Enter book title: ") author = input("Enter book author: ") price = float(input("Enter book price: ")) quantity = int(input("Enter book quantity: ")) inventory.add_book(title, author, price, quantity) elif choice == '2': title = input("Enter book title to remove: ") inventory.remove_book(title) elif choice == '3': title = input("Enter book title to update: ") price = input("Enter new price (leave blank to keep current): ") quantity = input("Enter new quantity (leave blank to keep current): ") price = float(price) if price else None quantity = int(quantity) if quantity else None inventory.update_book(title, price, quantity) elif choice == '4': inventory.view_inventory() elif choice == '5': print("Exiting the system.") break else: print("Invalid choice, please try again.")
Program Explanation
This Python program consists of two main classes: Book
and BookInventory
.
The Book
class holds information about individual books, such as title, author, price, and quantity. It also contains methods to update the book’s quantity and price and to display the book’s details.
The BookInventory
class manages a collection of books. It allows for adding, removing, updating, and displaying books in the inventory. The inventory
list stores the books.
In the main program, the user is presented with a menu of options to manage the inventory, including adding a book, removing a book, updating a book, or viewing the current inventory. The program continues to run until the user chooses to exit.
How to Run the Program
To run this program:
- Ensure you have Python installed on your computer (Python 3.x recommended).
- Save the code in a file named
book_inventory.py
. - Open a terminal or command prompt and navigate to the folder where you saved the file.
- Run the program by typing
python book_inventory.py
and follow the on-screen instructions to manage the book inventory.