Answered step by step
Verified Expert Solution
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 linetodictline: parts line.stripsplit details parts: return ID: parts 'Name': parts 'Category': parts 'Quantity': intparts 'Unit Price': floatparts 'Supplier Email': parts 'Details': details # Function to add an item to the inventorydef additem: # Collect item information from the user id inputEnter the ID of the item: name inputEnter the name of the item: category inputEnter the category of the item: quantity intinputEnter the quantity of the item: unitprice floatinputEnter the unit price of the item: supplieremail inputEnter the supplier email of the item: details while True: detail inputEnter a detail for the item press Enter to finish: if not detail: break details.appenddetail # Convert item information to a line in the inventory file format itemline fidnamecategoryquantityunitpricesupplieremailjoindetails
# Append item to the inventory file with openinventorytxta as file: file.writeitemline printItem added successfully."# Function to remove an item from the inventorydef removeitem: # Check if the inventory file exists if not ospath.existsinventorytxt: printInventory file not found." return # Collect item ID from the user id inputEnter the ID of the item to remove: # Check if item exists in inventory file with openinventorytxtr as file: lines file.readlines found False with openinventorytxtw as file: for line in lines: if line.stripsplit id: file.writeline else: found True if found: printItem removed successfully." else: printItem not found."# Function to update an item's informationdef updateitem: # Collect item ID from the user id inputEnter the ID of the item to update: # Check if item exists in inventory file with openinventorytxtr as file: lines file.readlines found False updatedlines for line in lines: if line.stripsplit id: found True newname inputEnter the new name press Enter to skip: newcategory inputEnter the new category press Enter to skip: newquantity inputEnter the new quantity press Enter to skip: newprice inputEnter the new unit price press Enter to skip: newemail inputEnter the new supplier email press Enter to skip: parts line.stripsplit if newname: parts newname if newcategory: parts newcategory if newquantity: parts newquantity if newprice: parts newprice if newemail: parts newemail line joinparts
updatedlines.appendline if not found: printItem not found." return # Write updated information back to the file with openinventorytxtw as file: file.writelinesupdatedlines printItem information updated successfully."# Function to display all items' informationdef displayallitems: if not ospath.existsinventorytxt: printInventory file not found." return with openinventorytxtr as file: for line in file: itemdict linetodictline printitemdict# Main functiondef main: while True: print
Store XYZ Inventory Management System" print Add an Item to the Inventory" print Remove an Item from the Inventory" print Update an Item's Information" print Display all Items' Information" print Exit" choice inputEnter your choice : if choice : additem elif choice : removeitem elif choice : updateitem elif choice : displayallitems elif choice : printExiting program..." break else: printInvalid choice. Please enter a number between and # Entry point of the programif namemain: main make our output file as dictionry
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started