#!/usr/bin/env python#-*- coding:utf-8 -*-#-Author-Lian
importjson,os,time
BASE_DIR= os.path.dirname(os.path.dirname(os.path.abspath(__file__)))'''数据库文件相对路径'''
__db_product = BASE_DIR + r"\database\product_list"
__db_shoping_car = BASE_DIR + r"\database\shopping_car"
__db_users_dict = BASE_DIR + r"\database\users_dict"
__db_creditcard_dict = BASE_DIR + r"\database\creditcard_dict"
__db_shopping_record = BASE_DIR + r"\database\shopping_record"
__db_creditcard_record = BASE_DIR + r"\database\creditcard_record"
'''购物商城'''
defShopping_mall():
shopping_list,pro_list=[],[]
with open(__db_product, "r", encoding="utf-8") as f_product:for item inf_product:
pro_list.append(item.strip("\n").split())defpro_inf():print("编号\t商品\t\t价格")for index, item inenumerate(pro_list):print("%s\t\t%s\t\t%s" % (index, item[0], item[1]))whileTrue:print(("\33[32;0m目前商城在售的商品信息\33[0m").center(40, "-"))
pro_inf()
choice_id= input("\n\33[34;0m选择要购买的商品编号 【购买 ID】/【返回 b】\33[0m:")ifchoice_id.isdigit():
choice_id=int(choice_id)if choice_id < len(pro_list) and choice_id >=0:
pro_item=pro_list[choice_id]print("\33[31;0m商品%s加入购物车 价格%s\33[0m"%(pro_item[0],pro_item[1]))
shopping_list.append(pro_item)else:print("\33[31;0m错误:没有相应的编号 请重新输入:\33[0m\n")elif choice_id == "b":
with open(__db_shoping_car, "r+") as f_shopping_car:
list=json.loads(f_shopping_car.read())
list.extend(shopping_list)
f_shopping_car.seek(0)
f_shopping_car.truncate(0)
list=json.dumps(list)
f_shopping_car.write(list)break
else:print("\33[31;0m错误:没有相应的编号 请重新输入:\33[0m\n")'''清空购物车'''
defEmpty_shopping_car():
with open(__db_shoping_car, &