需求:
一、商家界面
商家可根据自身需求添加和修改商品。并保存到文件中
二、顾客端
- 顾客登录系统,如果是初次登录,需输入金额;再次登录则不用,直接显示商品清单界面
- 可保存顾客购买商品信息与价格,下次登录可直接显示
商家代码:
# -*- conding:utf-8 -*-
# Author:OnlyZZQ菜鸟
#商家添加商品界面
#欢迎界面
print("--------欢迎进入店铺商品信息系统----------")
#选择要咨询的操作
while 1:
print("====请输入商品信息管理编号====")
c_input = str(input("(1、添加;2、修改价格;q、退出):"))
if c_input == "1":
while 1:
# 判断是否退出
# 输入商品名称
goods_add = input("请输入要添加的商品:")
if (goods_add == "q" or goods_add == "Q"):
print("欢迎再次使用")
exit()
else:
isnum = True
while isnum:
# 输入价格
goods_pre = input("请输入价格:")
if goods_pre.isdigit():
goods_pre = str(goods_pre)
isnum = False
else:
print("输入有误,重新输入")
# 追加列表
sl = (" ".join([goods_add, goods_pre]))
goods_list = open("gl.txt", "a")
goods_list2 = goods_list.write(sl)
goods_list2 = goods_list.write("\n")
# goods_list2.close()
# 显示当期菜单
print("--------当前有如下商品------")
goods_list = open("gl.txt", "r")
goods_list2 = goods_list.read()
print(goods_list2)
print("----------------------------")
goods_list.close()
elif c_input == "2":
print("--------当前有如下商品------")
goods_list = open("gl.txt", "r")
goods_list2 = goods_list.read()
print(goods_list2)
print("----------------------------")
goods_list.close()
#将文本里面的信息读取出来,并将列表转为字典
g_list2 = goods_list2.splitlines()
#再将列表解出来(使用空格切割出列表)
gg_list2 = []
for i in g_list2:
gg_list2.append(i.split(" "))
#print(gg_list2)
#将列表转化为字典
g_dict = dict(gg_list2)
#print(g_dict)
#提示输入要修改的商品价格
while True:
c_goods = input("请输入需要修改的商品名称:")
if (c_goods in g_dict):
c_price = input("请输入商品价格")
g_dict[c_goods] = c_price
print("修改成功!!!")
#print(g_dict)
#更新文件
#清空
c = open("gl.txt","w")
c_file = c.write("")
c.close()
d = open("gl.txt","a")
for i in g_dict:
c_file = d.write(i)
k = str(g_dict[i])
c_file =d.write(" ")
c_file = d.write(k)
c_file =d.write("\n")
d.close()
elif (c_goods == "q" or c_goods == "Q"):
print("欢迎再次使用")
exit()
else:
print("输入有误,请重新输入")
客户端代码:
# -*- conding:utf-8 -*-
# Author:OnlyZZQ
#公众号:OnlyZZQ菜鸟
#列表列出商品信息
'''
shoping_list = [["iphone","5000"],
[ "ipad", "8000"],
[ "mac", "12000"],
[ "xiaomi", "1000"],
[ "meizu", "2500"],
[ "huawei", "3000"],
[ "anycall", "4000"],
["oppo", "2600"],]
#将商品列表写如文件中
#将列表转为字符串
for i in shoping_list:
sl =(" ".join(["8、", "oppo", "2600"]))
goods_list = open("gl.txt","w")
goods_list.write(sl)
goods_list.close()
'''
#储存用户选择列表
goods = []
#储存商品总金额列表
money = []
t_money = 0
#退出函数
'''
def getout():
continue_shopping = input("是否继续购物(按Q退出购物)")
if (continue_shopping == "q" or continue_shopping == "Q"):
for i in money:
t_money = t_money + int(i)
print("您选择的商品为{_goods},金额为{_money}.余额为{_salay}".format(goods, str(t_money), salay))
quit()
'''
#从文件中读取商品列表
goods_str = open("gl.txt","r")
shoping_str = goods_str.read()
#print(shoping_str)
goods_str.close()
#将字符串转化为列表
#先按行转成列表
shoping_goods = shoping_str.splitlines()
#print(shoping_goods)
#按空格转换成列表
shoping_list = []
for i in shoping_goods:
shoping_good = i.split(" ")
shoping_list.append(shoping_good)
#print(shoping_list)
#存储用户购买记录信息
customer_b = open("cb.txt","r")
customer_buy = customer_b.read()
customer_b.close()
#显示用户购买记录
print(customer_buy)
#判断顾客金额
#先获取用户金额文件
customer_m = open("cm.txt","r")
customer_money = customer_m.read()
customer_m.close()
#判断里面是否有钱,没有的话,提示输入金额
if customer_money == "":
print("这是空用户,请输入金额")
is_money = True
while is_money:
salay = input("请输入你的余额:")
if (salay.isdigit()):
salay = int(salay)
is_money = False
else:
print("您的输入不是余额,请从新输入")
elif int(customer_money) == 0:
print("这是空用户,请输入金额")
is_money = True
while is_money:
salay = input("请输入你的余额:")
if (salay.isdigit()):
salay = int(salay)
is_money = False
else:
print("您的输入不是余额,请从新输入")
else:
salay = int(customer_money)
print("您目前账户余额为:", salay)
while 1:
#通过枚举获取下标
for index,i in enumerate(shoping_list):
print(index+1,i)
#提示用户选择商品
choose_in = input("请输入你要选择的商品")
#判断输入是否为数字
if choose_in.isdigit():
choose = int(choose_in)
# 判断商品是否存在
if ((choose > shoping_list.__len__()) or (choose < 1)):
print("你选择的商品不存在")
continue
# 出现金额不足
elif (int(shoping_list[choose - 1][1]) > salay):
print("余额不足,请重新选择或者退出")
# getout()
continue
else:
print("你已经将%s加入购物车" % shoping_list[choose - 1][0])
# 剩下余额
salay = salay - int(shoping_list[choose - 1][1])
# 显示加入购物车列表
goods.append(shoping_list[choose - 1][0])
# 金额
money.append(shoping_list[choose - 1][1])
print("你剩下的余额为", salay)
continue
#输入Q退出
elif (choose_in == "q" or choose_in == "Q"):
for l in money:
t_money = int(t_money) + int(l)
print("您选择的商品为", goods, "金额为{_money}.余额为{_salay}".format(_money=t_money, _salay=salay))
#将用户余额写入文件中
s_salay = str(salay)
customer_m = open("cm.txt", "w")
customer_money = customer_m.write(s_salay)
customer_m.close()
#将list转为字符串
s_goods = " ".join(goods)
#将购买金额转换为字符串
tt_money = str(t_money)
#将商品购买记录追加到文件中
customer_b = open("cb.txt", "a")
customer_buy = customer_b.write(s_goods)
customer_buy = customer_b.write(" 总共花费:"+tt_money)
customer_buy = customer_b.write("\n")
customer_b.close()
break
else:
print("输入有误,请重新输入:")
continue