Skip to content

Instantly share code, notes, and snippets.

@ramanasai
Created June 11, 2023 06:22
Show Gist options
  • Save ramanasai/56998b77d3aec3880259bc855a7aa4c9 to your computer and use it in GitHub Desktop.
Save ramanasai/56998b77d3aec3880259bc855a7aa4c9 to your computer and use it in GitHub Desktop.
Book Inventory Management - Python Code - Without Functions
"""
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.
"""
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":
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.")
continue
book = {
"title": title,
"author": author,
"genre": genre,
"quantity": quantity
}
inventory.append(book)
print("Book added successfully!")
elif choice == "2":
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"])
break
else:
print("Book not found in the inventory.")
elif choice == "3":
title = input("Enter the book title to update quantity: ")
for book in inventory:
if book["title"].lower() == title.lower():
try:
quantity = int(input("Enter the new quantity: "))
except ValueError:
print("Invalid quantity. Please try again.")
break
else:
book["quantity"] = quantity
print("Quantity updated successfully.")
break
else:
print("Book not found in the inventory.")
elif choice == "4":
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.")
break
else:
print("Book not found in the inventory.")
elif choice == "5":
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"]))
elif choice == "6":
print("Thank you for using the Book Inventory Management system.")
break
else:
print("Invalid choice. Please try again.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment