Skip to content

Instantly share code, notes, and snippets.

@ramanasai
Created June 11, 2023 06:17
Show Gist options
  • Save ramanasai/f6e80f17799430e2ce6d9347ae6cd0d2 to your computer and use it in GitHub Desktop.
Save ramanasai/f6e80f17799430e2ce6d9347ae6cd0d2 to your computer and use it in GitHub Desktop.
Book Inventory Management - Python Program
"""
author: Sai Ramana P B
email: [email protected]
Problem: Book Inventory Management
You are tasked with developing a program to manage a bookstore's inventory using dictionaries and lists. The program should allow the user to perform the following operations:
1. Add a book: The user should be able to enter the details of a book (title, author, genre, and quantity) and add it to the inventory.
2. Search for a book: Given a book title, the program should search the inventory and display the details of the book if it exists, including the quantity available.
3. Update book quantity: The user should be able to update the quantity of a book in the inventory. If the book does not exist, an appropriate message should be displayed.
4. Remove a book: The user should be able to remove a book from the inventory. If the book does not exist, an appropriate message should be displayed.
5. Display all books: The program should display a list of all books in the inventory, along with their details.
6. Optional You can expand this problem by incorporating additional functionalities like sorting the books, handling multiple genres, or implementing advanced search options.
Implement the program using appropriate data structures such as dictionaries and lists to store the book information. Use functions to modularize the code and allow for easier maintenance.
"""
def add_book(inventory):
title = input("Enter the book title: ")
author = input("Enter the author name: ")
genre = input("Enter the genre: ")
try:
quantity = int(input("Enter the quantity: "))
except ValueError:
print("Invalid quantity. Please try again.")
return
book = {
"title": title,
"author": author,
"genre": genre,
"quantity": quantity
}
inventory.append(book)
print("Book added successfully!")
def search_book(inventory):
title = input("Enter the book title to search: ")
for book in inventory:
if book["title"].lower() == title.lower():
print("\nBook Found:")
print("Title:", book["title"])
print("Author:", book["author"])
print("Genre:", book["genre"])
print("Quantity:", book["quantity"])
return
print("Book not found in the inventory.")
def update_quantity(inventory):
title = input("Enter the book title to update quantity: ")
for book in inventory:
if book["title"].lower() == title.lower():
quantity = int(input("Enter the new quantity: "))
book["quantity"] = quantity
print("Quantity updated successfully!")
return
print("Book not found in the inventory.")
def remove_book(inventory):
title = input("Enter the book title to remove: ")
for book in inventory:
if book["title"].lower() == title.lower():
inventory.remove(book)
print("Book removed successfully!")
return
print("Book not found in the inventory.")
def display_books(inventory):
if len(inventory) == 0:
print("Inventory is empty.")
else:
print("\nBook Inventory:")
print("Title\t\t Author\t\t Genre\t\t Quantity")
for book in inventory:
print("{0}\t\t {1}\t\t {2}\t\t {3}".format(book["title"], book["author"], book["genre"], book["quantity"]))
def main():
inventory = []
while True:
print("\n=== Book Inventory Management ===")
print("1. Add a book")
print("2. Search for a book")
print("3. Update book quantity")
print("4. Remove a book")
print("5. Display all books")
print("6. Quit")
choice = input("Enter your choice (1-6): ")
if choice == "1":
add_book(inventory)
elif choice == "2":
search_book(inventory)
elif choice == "3":
update_quantity(inventory)
elif choice == "4":
remove_book(inventory)
elif choice == "5":
display_books(inventory)
elif choice == "6":
print("Thank you for using the Book Inventory Management system.")
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment