Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

import os# Function to convert a line from the inventory file to a dictionarydef line _ to _ dict ( line ) : parts =

import os# Function to convert a line from the inventory file to a dictionarydef line_to_dict(line): parts = line.strip().split(",") details = parts[6:] return {'ID': parts[0], 'Name': parts[1], 'Category': parts[2], 'Quantity': int(parts[3]), 'Unit Price': float(parts[4]), 'Supplier Email': parts[5], 'Details': details }# Function to add an item to the inventorydef add_item(): # Collect item information from the user id = input("Enter the ID of the item: ") name = input("Enter the name of the item: ") category = input("Enter the category of the item: ") quantity = int(input("Enter the quantity of the item: ")) unit_price = float(input("Enter the unit price of the item: ")) supplier_email = input("Enter the supplier email of the item: ") details =[] while True: detail = input("Enter a detail for the item (press Enter to finish): ") if not detail: break details.append(detail) # Convert item information to a line in the inventory file format item_line = f"{id},{name},{category},{quantity},{unit_price},{supplier_email},{','.join(details)}
" # Append item to the inventory file with open("inventory.txt","a") as file: file.write(item_line) print("Item added successfully.")# Function to remove an item from the inventorydef remove_item(): # Check if the inventory file exists if not os.path.exists("inventory.txt"): print("Inventory file not found.") return # Collect item ID from the user id = input("Enter the ID of the item to remove: ") # Check if item exists in inventory file with open("inventory.txt","r") as file: lines = file.readlines() found = False with open("inventory.txt","w") as file: for line in lines: if line.strip().split(",")[0]!= id: file.write(line) else: found = True if found: print("Item removed successfully.") else: print("Item not found.")# 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 with open("inventory.txt","r") as file: lines = file.readlines() found = False updated_lines =[] for line in lines: if line.strip().split(",")[0]== id: found = True new_name = input("Enter the new name (press Enter to skip): ") new_category = input("Enter the new category (press Enter to skip): ") new_quantity = input("Enter the new quantity (press Enter to skip): ") new_price = input("Enter the new unit price (press Enter to skip): ") new_email = input("Enter the new supplier email (press Enter to skip): ") parts = line.strip().split(",") if new_name: parts[1]= new_name if new_category: parts[2]= new_category if new_quantity: parts[3]= new_quantity if new_price: parts[4]= new_price if new_email: parts[5]= new_email line =",".join(parts)+"
" updated_lines.append(line) if not found: print("Item not found.") return # Write updated information back to the file with open("inventory.txt","w") as file: file.writelines(updated_lines) print("Item information updated successfully.")# Function to display all items' informationdef display_all_items(): if not os.path.exists("inventory.txt"): print("Inventory file not found.") return with open("inventory.txt","r") as file: for line in file: item_dict = line_to_dict(line) print(item_dict)# Main functiondef main(): while True: 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 all Items' Information") print("5. Exit") 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_all_items() 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(). make our output file as dictionry

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_2

Step: 3

blur-text-image_3

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

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions