Realize shopping cart function --- file operation version

1. User Interface

>>>Determine whether the user's salary has a record

>>> Ability to read item list from file

>>> Ability to select desired products and deduct wages

>>>Print and save order information and salary balance

2. Merchant interface

>>>Able to read product list

>>> Ability to modify commodity prices and save

>>> Ability to add items and save

>>> Ability to delete items and save

3. Code implementation

shopping_cart.py

'''Implement shopping mall function---file operation version'''
# interface cart_user and cart_merchant

role = input('Please enter the role interface you want to enter (user/merchant)')
if role == 'user':
from day2 import cart_user
elif role == 'merchant':
from day2 import cart_merchant
else:
print("You The character interface entered is incorrect!")

 

cart_user.py

'''Implement shopping cart function---user interface'''

# 获得商品列表
def get_product():
product_list = []
with open('product.txt', 'r') as f:
for i in f.readlines():
i = i.replace('\n', '')
product_list.append(i.split(':'))
return product_list


# Save product list and balance
def save_product(goods_order_salary):
with open('shopping.txt', 'a') as f:
f.write(goods_order_salary + '\n')


# 读取工资
def read_salary():
with open('shopping.txt', 'r') as f:
data = f.read()
return data

# Determine whether the salary has a record
data = read_salary()
if 'balance' in data:
salary = data.split(':')[-1]
print('Your current balance is:'+salary)
else:
salary = input ("Please enter your salary:")

# 主程序
shopping_list = []
# if salary.isdigit():
salary = int(salary)
while True:
product_list = get_product()
for index, item in enumerate(product_list):
print(index, item)
user_choice = input('选择你想要的产品:')
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice >= 0:
p_item = product_list[user_choice]
p_item[1] = int(p_item[1])
if p_item[1] <= salary:
shopping_list.append(p_item)
salary -= p_item[1]
print('Added %s into shopping cart.your present balance is \033[31;1m%s\033[0m' % (p_item, salary))
save_product(str(p_item))
else:
print('\033[41;1m Your balance is only [%s], and you have to buy some wool\033[0m' % salary)
else:
print('product code [%s] is not exist!' % user_choice )
elif user_choice == 'q':
print('-------shopping list-------------')
for p in shopping_list:
print(p)
print('Your current balance :', salary)
save_product('\nyour present balance is :' + str(salary) + '\n')
exit()
else:
print('invalid option')

 

cart_merchant.py

'''Implement shopping cart function---merchant interface'''

# 获得商品列表
def get_product():
product_list = []
with open('product.txt', 'r') as f:
for i in f.readlines():
i = i.replace('\n', '')
product_list.append(i.split(':'))
return product_list

# 保存商品
def save_product(product_list):
with open('product.txt','w') as f:
for i in product_list:
i = str(i).replace('[', '')
i = str(i).replace(']', '')
i = str(i).replace(' ', '')
f.write(i+'\n')


product_list = get_product()

while True:
for index,item in enumerate(product_list):
print(index,item)
choice = input('Please enter the serial number of the product to be modified||Press a to add||Press q to exit:')
if choice.isdigit() :
choice = int(choice)
p_item = product_list[choice]
print(p_item)
price = input("Please enter the revised price:")
product_list[choice][1] = price
print(product_list)
save_product(product_list)
print( "Modified successfully!")
elif choice == 'a':
name = input('Please enter the name of the product you want to add:')
price = input('Please enter the price of the product you want to add:')
add_pro = [name ,price]
product_list.append(add_pro)
print(product_list)
save_product(product_list)
print('Added successfully!')
elif choice == 'q':
exit()
else:
print("Invalid input!")

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325354784&siteId=291194637