Question: # Define Product Object class Product: def _ _ init _ _ ( self , name, price, quantity ) : self.name = name self.price =
# Define Product Object
class Product:
def initself name, price, quantity:
self.name name
self.price price
self.quantity quantity
def strself:
return fProductnameselfname priceselfprice quantityselfquantity
# Create productList
productList
# Define Functions Needed to Maintain productList
def addproductname price, quantity:
productList.appendProductname price, quantity
def deleteproductname:
global productList
productList product for product in productList if product.name name
def changepricename newprice:
for product in productList:
if product.name name:
product.price newprice
break
def purchaseproductunitsname units:
for product in productList:
if product.name name:
product.quantity units
break
def sellproductunitsname units:
for product in productList:
if product.name name:
if product.quantity units:
product.quantity units
else:
printNot enough units in stock"
break
def displayproductname:
for product in productList:
if product.name name:
printproduct
break
def displayallproducts:
for product in productList:
printproduct
# Main Part of Program
# Read Data About Products from text file into productList
def readproductsfromfilefilename:
with openfilenamer as file:
for line in file:
name, price, quantity line.stripsplit
addproductname floatprice intquantity
def writeproductstofilefilename:
with openfilenamew as file:
for product in productList:
file.writefproductnameproductpriceproductquantity
# Enter Transaction Loop
choice
while choice :
print Add Product"
print Delete Product"
print Change Price"
print Purchase Product Units"
print Sell Product Units"
print Display One Product"
print Display All Products"
print Exit"
choice intinputEnter Choice:
# do if elif choice
if choice :
name inputEnter product name:
price floatinputEnter product price:
quantity intinputEnter product quantity:
addproductname price, quantity
elif choice :
name inputEnter product name to delete:
deleteproductname
elif choice :
name inputEnter product name to change price:
newprice floatinputEnter new price:
changepricename newprice
elif choice :
name inputEnter product name to purchase units:
units intinputEnter number of units to purchase:
purchaseproductunitsname units
elif choice :
name inputEnter product name to sell units:
units intinputEnter number of units to sell:
sellproductunitsname units
elif choice :
name inputEnter product name to display:
displayoneproductname
elif choice :
displayallproducts
elif choice :
writeproductstofileproductstxt
printleaving the program..."
else:
printInvalid choice, please try again
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
