#购物车的功能
#客户模块
#1.添加商品
#2.去除商品
#3.展示商品
#4.结算
#商家模块
#1.会员注册
#2.会员登陆
#2.后台添加商品
#商家模块
import time
import pickle
import json
import os
mypath=os.getcwd()
os_name=os.name
print(os_name)
print(type(os_name))
print(mypath)
if os_name is not “nt”:
os.chdir("/mnt/hgfs/jiadiannao/oldboy/code/")
def register_member():
print(“欢迎进入注册页面”)
while 1:
username=input(“请输入用户名:”).strip()
#创建一个非法字符集
if username “”:
print(“注册名不能为空”)
print(“请重新输入”)
illegal_character_list=[ " “,”*","?",":","!","@","#","$","%","^","&","(",")","{","}","[","]","/",]
for i in illegal_character_list:
if i in username:
print(“你的姓名含有非法字符,请重新输入”)
continue
#名字查验去重 #{“name”:username,“passwd”:passwd}
#打开注册列表
with open(“regist.txt”,mode=“a+”)as fp:
sign=0
fp.seek(0)
for i in fp:
usr_info=json.loads(i)
if usr_info[“username”]username:
sign=1
if sign1:
print(“用户名重复,请重新输入”)
continue
while 1:
passwd = input(“请输入密码”)
if passwd"":
print(“密码不能为空”)
continue
passwd2=input(“请重新输入密码”)
if passwd==passwd2:
print(“恭喜你注册成功!”)
break
else:
print(“两次密码不一致”)
dic={}
dic[“username”]=username
dic[“passwd”]=passwd
with open("regist.txt",mode="a",encoding="utf-8")as fp:
json.dump(dic,fp)
fp.write("\n")
quit = input("按任意键继续注册,退出请按q")
if quit.lower() == "q":
return
register_member()
def landing():
while 1:
print("欢迎进入登陆界面")
username=input("请输入用户名:").strip()
#验证用户是否再黑名单中
if is_hack_name(username):
print("你的账户已被冻结,请联系管理员!")
continue
# 验证用户名是否存在
if is_logo(username) ==1:
return
quit = input("按任意键继续登陆,退出请按q")
if quit.lower() == "q":
return
#验证黑名单函数
def is_logo(username):
with open(“regist.txt”, mode=“r”, encoding=“utf-8”)as fp:
for i in fp:
res = json.loads(i)
if res[“username”] == username:
n = 3
while n > 0:
password = input(“请输入密码:”)
if res[“passwd”] == password:
print(“恭喜你,登陆成功”)
return 1
n -= 1
if n == 0:
print(“你的账户密码输错3次,将被冻结”)
with open(“hack_name.txt”, mode=“a+”, encoding=“utf-8”)as fp:
json.dump(res, fp)
fp.write("\n")
def is_hack_name(username):
sign=0
with open(“hack_name.txt”, mode=“r”)as fp:
for i in fp:
res = json.loads(i)
if res[“username”] == username:
sign = 1
return sign
landing()
#后台添加商品模块
path=“shoping_list.txt”
def add_commodity(name,price:float,amount:float,path):
path=“shoping_list.txt”
dic={}
dic[name]={“price”:price,“amount”:amount}
with open(path, mode=“r+”,encoding=“utf-8”)as fp:
if len(list(fp))==0:
json.dump(dic,fp,ensure_ascii=False)
return
with open(path, mode="r",encoding="utf-8")as fp:
res=json.load(fp)
res.update(dic)
with open(path, mode="w",encoding="utf-8")as fp:
json.dump(res,fp,ensure_ascii=False)
add_commodity(“黄瓜”,2.3,800,path)
#展示商品模块
def diplay_commodity(path):
path = “shoping_list.txt”
with open (path,mode=“r”,encoding=“utf-8”)as fp:
res=json.load(fp)
print("{:<10s}{:<10s}{:<10s}".format(“商品名称”,“价格”,“数量”))
for k,v in res.items():
print("{:<10s}{:<10.2f}{:<10.2f}".format(k,v[“price”],v[“amount”]))
add_commodity(“桔子”,5,120,path)
add_commodity(“南瓜”,1.3,1800,path)
diplay_commodity(path)
#shoping_list=[(“桔子”,5,120,path),(“南瓜”,1.3,1800,path)]
#for i in shoping_list:
#add_commodity(*i)
diplay_commodity(path)
landing()
register_member()
#充值模块
def recharge():
while 1:
usrname = input(“请输入要充值的账户”).strip()
print(“请输入充值金额:”)
money = input().strip()
print(“账户%s的充值金额:%s” % (usrname, money), “确认请按y”)
sure = input()
if sure.lower() == “y”:
with open(“money.txt”, mode=“a”, encoding=“utf-8”)as fp:
json.dump({usrname: float(money),“time”:time.localtime(),“type”:“充值”}, fp, ensure_ascii=False)
fp.write("\n")
quit = input("按任意键继续充值,退出请按q")
if quit.lower() == "q":
return
recharge()
total_money=recharge(“llj”)
def total_mymoney(usrname):
total_money=0
with open(“money.txt”, mode=“r”, encoding=“utf-8”)as fp:
for i in fp:
res = json.loads(i)
if usrname in res.keys():
total_money += res[usrname]
return total_money
def select_commodity():
# diplay_commodity(path)
print(“请选择商品”)
while 1:
commodity_name = input(“商品名称:”).strip()
commodity_amount = input(“商品数量:”).strip()
if commodity_amount.isdecimal():
with open(“shoping_list.txt”, mode=“r”, encoding=“utf-8”)as fp:
res = json.load(fp)
price = res[commodity_name]["price"]
if commodity_name in res.keys():
#print(res[commodity_name]["amount"], price, float(commodity_amount))
if res[commodity_name]["amount"] >= float(commodity_amount) and float(commodity_amount) > 0:
print("请选择下一商品")
with open("select_list.txt", mode="a", encoding="utf-8")as fp:
res = {commodity_name: [price, float(commodity_amount)]}
# print(res)
json.dump(res, fp, ensure_ascii=False)
fp.write("\n")
with open("shoping_list.txt",mode="r",encoding="utf-8")as fp:
dic1=json.load(fp)
dic1[commodity_name]["amount"]=dic1[commodity_name]["amount"]-float(commodity_amount)
print(dic1)
with open("shoping_list.txt",mode="w",encoding="utf-8")as fp:
json.dump(dic1,fp,ensure_ascii=False)
else:
print("你购买的商品信息不存在或者购买商品已经售罄,请选择其他商品")
quit = input("按任意键继续添加,退出请按q")
if quit.lower() == "q":
break
select_commodity()
#删除商品模块
def delete_commodity():
while 1:
name=input("请输入要删除的商品名称").strip()
lst=[]
with open("select_list.txt", mode="r", encoding="utf-8")as fp:
for i in fp:
res=json.loads(i)
if name not in res.keys():
lst.append(res)
with open("select_list.txt", mode="w", encoding="utf-8")as fp:
for i in lst:
json.dump(i,fp,ensure_ascii=False)
fp.write("\n")
quit = input("按任意键继续删除,退出请按q")
if quit.lower() == "q":
break
delete_commodity()
#结算
def settle_aounts(name):
name=“wuzhao”
mymoney=total_mymoney(“wuzhao”)
print(mymoney)
while 1:
settle_money = 0
with open(“select_list.txt”, mode=“r”,encoding=“utf-8”)as fp:
for i in fp:
res = json.loads(i)
for k in res.keys():
print(res)
settle_money += res[k][0]*res[k][1]
print(“商品总金额”,settle_money)
if settle_money > mymoney:
print(“你购买的商品如下”)
with open(“select_list.txt”,mode=“r”,encoding=“utf-8”)as fp:
for i in fp:
res=json.loads(i)
print(res)
money=total_mymoney(name)
print(“账户金额%s不足,请删除部分商品”%(money))
delete_commodity()
continue
break
print("结账中")
with open("money.txt", mode="a", encoding="utf-8")as fp:
json.dump({name: -settle_money,"time":time.localtime(),"type":"消费"}, fp,ensure_ascii=False)
fp.write("\n")
time.sleep(3)
print("请付%d,账户余额%d"%(settle_money,total_mymoney("wuzhao")))
print("欢迎下次光临!")
add_commodity(“桔子”,5.0,120.0,path)
add_commodity(“南瓜”,1.3,1800.0,path)
# recharge()
select_commodity()
settle_aounts(“wuzhao”)