Simple Inventory Management with a Shelve Database
This challenge focuses on utilizing Python's shelve module to create a persistent database for managing a simple inventory. You'll be building a program that allows users to add, retrieve, update, and delete items from the inventory, storing the data in a shelve file. This is a practical exercise in data persistence and demonstrates a lightweight database solution for smaller applications.
Problem Description
You are tasked with creating a Python program that manages a small inventory of items. The inventory will be stored in a shelve database file named "inventory.shelve". Each item in the inventory will be represented by a unique key (e.g., an item ID) and a dictionary containing the item's details, including:
name: The name of the item (string).quantity: The quantity of the item in stock (integer).price: The price of a single item (float).
The program should provide the following functionalities:
- Add Item: Allow the user to add a new item to the inventory. The user should be prompted for the item ID, name, quantity, and price.
- Retrieve Item: Allow the user to retrieve the details of an item given its ID.
- Update Item: Allow the user to update the quantity or price of an existing item. The user should be prompted for the item ID and the field to update (quantity or price).
- Delete Item: Allow the user to delete an item from the inventory given its ID.
- List Inventory: Display all items currently in the inventory, showing their ID, name, quantity, and price.
The program should handle the following edge cases:
- Item ID Already Exists: If the user tries to add an item with an ID that already exists, display an error message and prevent the addition.
- Item ID Not Found: If the user tries to retrieve, update, or delete an item with an ID that doesn't exist, display an appropriate error message.
- Invalid Input: Handle cases where the user enters invalid input (e.g., non-numeric values for quantity or price). Provide informative error messages.
Examples
Example 1:
Input: User adds item with ID "A123", name "Widget", quantity 10, price 5.99.
Output: "Item A123 added to inventory."
Explanation: The item details are stored in the "inventory.shelve" file under the key "A123".
Example 2:
Input: User requests to retrieve item with ID "A123".
Output: "Item A123: Name: Widget, Quantity: 10, Price: 5.99"
Explanation: The program retrieves the item details from the "inventory.shelve" file using the key "A123" and displays them.
Example 3:
Input: User attempts to add item with ID "A123" (already exists).
Output: "Error: Item with ID A123 already exists."
Explanation: The program checks if the ID already exists before adding the new item.
Constraints
- The
shelvefile name must be "inventory.shelve". - Item IDs should be strings.
- Quantity must be a non-negative integer.
- Price must be a non-negative float.
- The program should provide a user-friendly menu-driven interface.
- Error handling should be robust and informative.
Notes
- The
shelvemodule provides a simple way to store Python objects in a persistent database. - Consider using a
whileloop to keep the program running until the user chooses to exit. - Use appropriate data types for storing item details.
- Think about how to structure your code to make it modular and easy to understand. Functions are your friend!
- Remember to close the
shelveobject when you're finished to ensure data is properly saved. Use atry...finallyblock to guarantee closure even if errors occur. - The user interface can be simple text-based input and output. No graphical user interface is required.