o1-preview-2024-09-12· OpenAI
WildChat-4.8M (AI2)
create a python script for bakery management system without any library
- User
create a python script for bakery management system without any library
- Assistant
# Bakery Management System class Product: def __init__(self, product_id, name, price, quantity): self.id = product_id # unique identifier for product self.name = name self.price = price self.quantity = quantity # stock level class Inventory: def __init__(self): self.products = {} # dictionary to hold products, key is product_id def add_product(self, product): if product.id in self.products: print(f'Product ID {product.id} already exists. Use restock to add quantity.') else: self.products[product.id] = product print(f'Product {product.name} added to inventory.') def restock_product(self, product_id, quantity): if product_id in self.products: self.products[product_id].quantity += quantity print(f'Restocked {quantity} units of {self.products[product_id].name}. New quantity: {self.products[product_id].quantity}') else: print(f'Product ID {product_id} not found in inventory.') def sell_product(self, product_id, quantity): if product_id in self.products: product = self.products[product_id] if product.quantity >= quantity: product.quantity -= quantity total_price = quantity * product.price print(f'Sold {quantity} units of {product.name}. Total price: ${total_price:.2f}') else: print(f'Not enough stock to sell {quantity} units of {product.name}. Current stock: {product.quantity}') else: print(f'Product ID {product_id} not found in inventory.') def view_inventory(self): print('\nCurrent Inventory:') print('ID\tName\t\tPrice\tQuantity') print('---------------------------------------------') for product in self.products.values(): print(f'{product.id}\t{product.name}\t\t${product.price:.2f}\t{product.quantity}') print('---------------------------------------------\n') def main(): inventory = Inventory() while True: print('Bakery Management System') print('1. Add new product') print('2. Restock product') print('3. Sell product') print('4. View inventory') print('5. Exit') choice = input('Enter your choice (1-5): ') if choice == '1': product_id = input('Enter product ID: ') name = input('Enter product name: ') price = float(input('Enter product price: ')) quantity = int(input('Enter product quantity: ')) product = Product(product_id, name, price, quantity) inventory.add_product(product) elif choice == '2': product_id = input('Enter product ID to restock: ') quantity = int(input('Enter quantity to add: ')) inventory.restock_product(product_id, quantity) elif choice == '3': product_id = input('Enter product ID to sell: ') quantity = int(input('Enter quantity to sell: ')) inventory.sell_product(product_id, quantity) elif choice == '4': inventory.view_inventory() elif choice == '5': print('Exiting Bakery Management System. Goodbye!') break else: print('Invalid choice. Please enter a number from 1 to 5.') if __name__ == '__main__': main()This script provides a simple Bakery Management System implemented entirely in Python without using any external libraries. It allows you to:
- Add new products to the inventory.
- Restock existing products by increasing their quantity.
- Sell products to customers, which decreases their quantity in the inventory.
- View the current inventory with details of all products.
The system uses classes to represent products and manage inventory, and a command-line interface to interact with the user. You can run this script in your Python environment to manage your bakery's inventory efficiently.
How to Use the Script:
- Run the script in a Python environment (e.g., using
python bakery_management.pyif you save it asbakery_management.py). - Select an option from the menu by entering the corresponding number.
- Follow the prompts to add products, restock, sell, or view inventory.
- Exit the program by selecting option 5.
Example Interaction:
Bakery Management System 1. Add new product 2. Restock product 3. Sell product 4. View inventory 5. Exit Enter your choice (1-5): 1 Enter product ID: 101 Enter product name: Sourdough Bread Enter product price: 3.50 Enter product quantity: 20 Product Sourdough Bread added to inventory. Bakery Management System 1. Add new product 2. Restock product 3. Sell product 4. View inventory 5. Exit Enter your choice (1-5): 4 Current Inventory: ID Name Price Quantity --------------------------------------------- 101 Sourdough Bread $3.50 20 --------------------------------------------- Bakery Management System 1. Add new product 2. Restock product 3. Sell product 4. View inventory 5. Exit Enter your choice (1-5): 3 Enter product ID to sell: 101 Enter quantity to sell: 5 Sold 5 units of Sourdough Bread. Total price: $17.50 Bakery Management System 1. Add new product 2. Restock product 3. Sell product 4. View inventory 5. Exit Enter your choice (1-5): 4 Current Inventory: ID Name Price Quantity --------------------------------------------- 101 Sourdough Bread $3.50 15 --------------------------------------------- Bakery Management System 1. Add new product 2. Restock product 3. Sell product 4. View inventory 5. Exit Enter your choice (1-5): 5 Exiting Bakery Management System. Goodbye!Note: This script does not persist data between runs. If you close the program, all inventory data will be lost. To add data persistence, you would need to implement file reading and writing, which is beyond the scope of this script as per the given constraints.