Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import os# Function to display the main menudef display _ menu ( ) : print ( Store XYZ Inventory Management System ) print (

import os# Function to display the main menudef display_menu(): print("Store XYZ Inventory Management System") print("1. Add an Item to the Inventory.") print("2. Remove an Item from the Inventory.") print("3. Update an Item's Information.") print("4. Display a specific Items Information.") print("5. Exit")# Function to add an item to the inventorydef add_item(): # Function to validate ID def validate_id(id): if len(id)!=8 or id[3]!='-' or not id[:3].isalpha() or not id[4:].isdigit(): return False return True # Function to validate name def validate_name(name): # Add your validation code here if len(name)<1: return False return True # Function to validate category def validate_category(category): # Add your validation code here if len(category)<1: return False return True # Function to validate quantity def validate_quantity(quantity): # Add your validation code here try: quantity = int(quantity) if quantity <=0: return False except ValueError: return False return True # Function to validate unit price def validate_unit_price(price): # Add your validation code here try: price = float(price) if price <=0: return False except ValueError: return False return True # Function to validate supplier email def validate_supplier_email(email): # Add your validation code here if "@" not in email or "." not in email: return False return True # Function to validate item details def validate_details(details): # Add your validation code here if len(details)<1: return False return True # Collect item information from the user id = input("Enter the ID of the item (format: XYZ-1234):") if not validate_id(id): print("Invalid ID format.") return name = input("Enter the name of the item: ") if not validate_name(name): print("Invalid name.") return category = input("Enter the category of the item: ") if not validate_category(category): print("Invalid category.") return quantity = input("Enter the quantity of the item: ") if not validate_quantity(quantity): print("Invalid quantity.") return unit_price = input("Enter the unit price of the item: ") if not validate_unit_price(unit_price): print("Invalid unit price.") return supplier_email = input("Enter the supplier email of the item: ") if not validate_supplier_email(supplier_email): print("Invalid supplier email.") return details =[] while True: detail = input("Enter a detail for the item (press Enter to finish): ") if detail =="": break if not validate_details(detail): print("Invalid detail.") continue details.append(detail) # Add item to the inventory file with open("inventory.txt","a") as file: file.write(f"{id},{name},{category},{quantity},{unit_price},{supplier_email},{','.join(details)}
") print("Item added successfully.")# Function to remove an item from the inventorydef remove_item(): # Collect item ID from the user id = input("Enter the ID of the item to remove: ") # Check if item exists in inventory file items =[] with open("inventory.txt","r") as file: for line in file: if line.strip().split(",")[0]== id: items.append(line.strip()) if not items: print("Item not found.") return print("Item found with the following information:") for item in items: print(item) confirm = input("Are you sure you want to remove this item? (yes/no): ") if confirm.lower()== "yes": with open("inventory.txt","w") as file: for item in items: if item.strip().split(",")[0]!= id: file.write(f"{item}
") print("Item removed successfully.") else: print("Removal cancelled.")# Function to update an item's informationdef update_item(): # Collect item ID from the user id = input("Enter the ID of the item to update: ") # Check if item exists in inventory file items =[] with open("inventory.txt","r") as file: for line in file: if line.strip().split(",")[0]== id: items.append(line.strip()) if not items: print("Item not found.") return print("Item found with the following information:") for item in items: print(item) # Display update menu print("
Update Menu:") print("1. Update Name") print("2. Update Category") print("3. Update Quantity") print("4. Update Unit Price") print("5. Update Supplier Email") print("6. Update Item Details") # Collect user choice choice = input("Enter your choice: ") if choice =='1': new_name = input("Enter the new name: ") items[0]= items[0].split(",",1)[0]+","+ new_name +","+ items[0].split(",",2)[2] elif choice =='2': new_category = input("Enter the new category: ") items[0]= items[0].split(",",1)[0]+","+ items[0].split(",",1)[1].split(",",1)[0]+","+ new_category +","+ items[0].split(",",2)[2] elif choice =='3': new_quantity = input("Enter the new quantity: ") items[0]= items[0].split(",",3)[0]+","+ new_quantity +","+ items[0].split(",",4)[3] elif choice =='4': new_price = input("Enter the new unit price: ") items[0]= items[0].split(",",4)[0]+","+ new_price +","+ items[0].split(",",5)[4] elif choice =='5': new_email = input("Enter the new supplier email: ") items[0]= items[0].rsplit(",",1)[0]+","+ new_email elif choice =='6': new_details =[] while True: detail = input("Enter a new detail for the item (press Enter to finish): ") if detail =="": break new_details.append(detail) items[0]= items[0].split(",",6)[0]+","+ items[0].split(",",7)[0]+","+",".join(new_details) else: print("Invalid choice.") return with open("inventory.txt","w") as file: for item in items: file.write(f"{item}
") print("Item information updated successfully.")# Function to display item informationdef display_item_info(): # Display submenu print("Display Menu:") print("1. Display all items information.") print("2. Display all items information sorted by Quantity.") print("3. Display a specific items information based on the ID.") # Collect user choice choice = input("Enter your choice: ") if choice =='1': with open("inventory.txt","r") as file: for line in file: print(line.strip()) elif choice =='2': sort_order = input("Sort by Quantity (a - ascending, d - descending): ") with open("inventory.txt","r") as file: lines = file.readlines() lines.sort(key=lambda x: int(x.strip().split(",")[3]), reverse=(sort_order.lower()=='d')) for line in lines: print(line.strip()) elif choice =='3': id = input("Enter the ID of the item: ") with open("inventory.txt","r") as file: for line in file: if line.strip().split(",")[0]== id: print(line.strip()) return print("Item not found.") else: print("Invalid choice.")# Main functiondef main(): while True: display_menu() choice = input("Enter your choice (1-5): ") if choice =='1': add_item() elif choice =='2': remove_item() elif choice =='3': update_item() elif choice =='4': display_item_info() elif choice =='5': print("Exiting program...") break else: print("Invalid choice. Please enter a number between 1 and 5.")# Entry point of the programif __name__=="__main__": main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions