Question: # Define Product Object class Product: def _ _ init _ _ ( self , name, price, quantity ) : self.name = name self.price =

# Define Product Object
class Product:
def __init__(self, name, price, quantity):
self.name = name
self.price = price
self.quantity = quantity
def __str__(self):
return f"Product(name={self.name}, price={self.price}, quantity={self.quantity})"
# Create productList
productList =[]
# Define Functions Needed to Maintain productList
def add_product(name, price, quantity):
productList.append(Product(name, price, quantity))
def delete_product(name):
global productList
productList =[product for product in productList if product.name != name]
def change_price(name, new_price):
for product in productList:
if product.name == name:
product.price = new_price
break
def purchase_product_units(name, units):
for product in productList:
if product.name == name:
product.quantity += units
break
def sell_product_units(name, units):
for product in productList:
if product.name == name:
if product.quantity >= units:
product.quantity -= units
else:
print("Not enough units in stock")
break
def display_product(name):
for product in productList:
if product.name == name:
print(product)
break
def display_all_products():
for product in productList:
print(product)
# Main Part of Program
# Read Data About Products from text file into productList
def read_products_from_file(filename):
with open(filename,'r') as file:
for line in file:
name, price, quantity = line.strip().split(',')
add_product(name, float(price), int(quantity))
def write_products_to_file(filename):
with open(filename,'w') as file:
for product in productList:
file.write(f"{product.name},{product.price},{product.quantity}
")
# Enter Transaction Loop
choice =0
while choice !=8:
print("1. Add Product")
print("2. Delete Product")
print("3. Change Price")
print("4. Purchase Product Units")
print("5. Sell Product Units")
print("6. Display One Product")
print("7. Display All Products")
print("8. Exit")
choice = int(input("Enter Choice: "))
# do if elif choice
if choice ==1:
name = input("Enter product name: ")
price = float(input("Enter product price: "))
quantity = int(input("Enter product quantity: "))
add_product(name, price, quantity)
elif choice ==2:
name = input("Enter product name to delete: ")
delete_product(name)
elif choice ==3:
name = input("Enter product name to change price: ")
new_price = float(input("Enter new price: "))
change_price(name, new_price)
elif choice ==4:
name = input("Enter product name to purchase units: ")
units = int(input("Enter number of units to purchase: "))
purchase_product_units(name, units)
elif choice ==5:
name = input("Enter product name to sell units: ")
units = int(input("Enter number of units to sell: "))
sell_product_units(name, units)
elif choice ==6:
name = input("Enter product name to display: ")
display_one_product(name)
elif choice ==7:
display_all_products()
elif choice ==8:
write_products_to_file('products.txt')
print("leaving the program...")
else:
print("Invalid choice, please try again>.<")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!