Notes
Notes - notes.io |
import os
class Product:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
def total_value(self):
return self.price * self.quantity
class InventoryManager:
def __init__(self, filename="inventory.txt"):
self.filename = filename
self.products = []
self.load_products()
def load_products(self):
if os.path.exists(self.filename):
with open(self.filename, 'r') as f:
try:
self.products = json.load(f)
except json.JSONDecodeError:
self.products = []
def save_products(self):
with open(self.filename, 'w') as f:
json.dump([product.__dict__ for product in self.products], f, indent=2)
def add_product(self, name, price, quantity):
new_product = Product(name, price, quantity)
self.products.append(new_product)
self.save_products()
def remove_product(self, name):
self.products = [product for product in self.products if product.name != name]
self.save_products()
def update_stock(self, name, quantity):
for product in self.products:
if product.name == name:
product.quantity = quantity
self.save_products()
return
print(f"Product {name} not found in inventory.")
def list_products(self):
print("nCurrent Inventory:")
for product in self.products:
print(f"{product.name} (Price: ${product.price}, Quantity: {product.quantity})")
def get_total_value(self):
total = sum(product.total_value() for product in self.products)
return f"Total inventory value: ${total:.2f}"
def exit(self):
print("Exiting the program. Goodbye!")
exit()
def main():
inventory_manager = InventoryManager()
while True:
print("nWhat would you like to do?")
print("1. Add a new product")
print("2. Remove a product")
print("3. Update stock quantity")
print("4. List all products")
print("5. Calculate total value")
print("6. Exit program")
try:
user_choice = int(input("nEnter your choice (1-6): "))
if user_choice == 1:
name = input("Enter product name: ")
price = float(input("Enter product price: $"))
quantity = int(input("Enter product quantity: "))
inventory_manager.add_product(name, price, quantity)
elif user_choice == 2:
name = input("Enter product name to remove: ")
inventory_manager.remove_product(name)
elif user_choice == 3:
name = input("Enter product name to update: ")
quantity = int(input(f"Enter new stock quantity for {name}: "))
inventory_manager.update_stock(name, quantity)
elif user_choice == 4:
inventory_manager.list_products()
elif user_choice == 5:
print(inventory_manager.get_total_value())
elif user_choice == 6:
inventory_manager.exit()
break
except ValueError:
print("Invalid input. Please enter a number between 1 and 6.")
if __name__ == "__main__":
main()
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
