返回顶部
扶摇直上九万里,展翅高飞岂可待。

python菜鸟学习: 7. 购物车升级版,用户、商品信息存储,修改,新增

# -*- coding: utf-8 -*-
import os

'''
用户入口:
1. 商品信息存在文件里
2. 已购商品,余额记录

商家入口
1. 可以添加商品,修改商品价格
商品信息:
commdList.txt
华为手机,8888
iphon, 2222
bike, 8888

用户信息:
userinfo.txt
LiYuZhoupan,100000
ZhangSan,8888
liSi,10000
'''


# 读取商品信息,用户信息
def readCommod(filename):
commadList = {}
with open(filename, "r", encoding="utf-8") as f:
for i in f.readlines():
# 商品名称
name = i.split(",")[0]
info = i.split(",")[1].replace("\n", " ")
commadList[name] = info
f.close()
return commadList


# 追加商品信息,用户信息
def writeCommand(name, info, filename):
name = name
info = info
if str(info).isdigit():
with open(filename, 'a', encoding="utf-8") as f:
f.write(name + "," + str(info) + "\n")
else:
print("输入错误请重新输入!")
f.close()


# 修改商品信息
def changeCommand(name, info, filename):
name = name
info = info
if str(info).isdigit():
# 遍历原来的文件夹
with open(filename, 'r', encoding="utf-8") as f:
# 创建新的文件夹
with open(filename + "tmp", "a", encoding="utf-8") as f1:
# 将原来的文件内容添加到新的文件
for i in f.readlines():
# 如果传入的name存在原来文件中,则修改传入的info信息
if name == i.split(",")[0]:
f1.write(name + "," + str(info) + "\n")
else:
f1.write(i)
f1.close()
f.close()
os.remove(filename)
os.rename(filename + "tmp", filename)


# 购物车得到用户信息
def shoppingGetusreInfo():
# 输入用户名
username = input("请输入用户名:\n")
useSalary = 0
# 查找用户信息表中是否存在该用户,如果不存在则添加
userList = readCommod("userinfo.txt")
if username in userList:
print("您当前的余额为:{salary}".format(salary=userList[username]))
useSalary = userList[username]
else:
salary = input("your salary:\n")
if salary.isdigit():
writeCommand(username, salary, "userinfo.txt")
useSalary = salary
print("用户-->{_username}<--添加成功,余额为-->{_salary}<--".format(_username=username, _salary=useSalary))
else:
print("请输入整数")
changeCommand(username, useSalary, "userinfo.txt")
print("用户-->{_username}<--,余额为-->{_salary}<--".format(_username=username, _salary=useSalary))
return username, int(useSalary)


# 购物车挑选商品:
def shoppingGetCommand():
# 设置标签
flag = True
# 定义所选购物品
yourCommandList = []
username, money = shoppingGetusreInfo()
print("商品信息如下:".center(50, "#"))
# 将商品信息写入到字典中
commandDict = readCommod("commdList.txt")
# 将字段中的商品名称以数组存储
commandList = list(commandDict.keys())
for i in range(len(commandList)):
print(str(i) + "," + commandList[i] + "," + commandDict.get(commandList[i]))
print("请选择你需要的商品:".center(50, "*"))
while flag:
commandNum = input("商品编号,退出请按Q:\n")
if commandNum.isdigit() and int(commandNum) < len(commandList):
# 如果余额足够则添加商品,并扣除余额
if money > int(commandDict.get(commandList[int(commandNum)])):
yourCommandList.append(commandList[int(commandNum)])
money = money - int(commandDict.get(commandList[int(commandNum)]))
print("您选择的物品有{yourCommandList},余额为:{money}".format(yourCommandList=yourCommandList, money=money))
else:
print("您余额{money},不足够支付:{money1}".format(money=money,
money1=int(commandDict.get(commandList[int(commandNum)]))))
elif commandNum == "Q":
flag = False
else:
print("输入错误,请重新输入!")
# 打印最终购买的物品
print("您选择的物品有{yourCommandList},余额为:{money}".format(yourCommandList=yourCommandList, money=money))
# 回填余额信息
changeCommand(username, money, "userinfo.txt")


if __name__ == '__main__':
flag = input("请输入1/2,其他退出:【1,商家;2,顾客】:\n")
if flag.isdigit():
# 商家操作
if int(flag) == 1:
# 修改商品信息
# 将商品信息写入到字典中
commandDict = readCommod("commdList.txt")
# 将字段中的商品名称以数组存储
commandList = list(commandDict.keys())
for i in range(len(commandList)):
print(str(i) + "," + commandList[i] + "," + commandDict.get(commandList[i]))
name = input("请输入需要修改的商品名称:\n")
price = input("请输入需要修改的商品价格:\n")
# 如果存在商品字典中则修改商品价格
if name in commandDict:
changeCommand(name, price, "commdList.txt")
# 如果不存在商品字段中则增加商品信息
else:
writeCommand(name, price, "commdList.txt")
# 顾客操作
elif int(flag) == 2:
shoppingGetCommand()
else:
print("输入错误或者退出{flag}".format(flag=flag))
exit()

# writeCommand("liyuzhoupan123", "100000", "userinfo.txt")
# print(readCommod("userinfo.txt"))
# changeCommand("liyuzhoupan", "7777", "userinfo.txt")
# shoppingGetCommand()
posted @ 2022-08-16 15:37  鲤鱼洲畔  阅读(58)  评论(0编辑  收藏  举报