# -*- 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()

最新文章

  1. Win10 UI入门RelativePanel(2)
  2. for in 结构
  3. HTTP权威协议笔记-4.连接管理
  4. 【2016-10-12】【坚持学习】【Day3】【命令模式】
  5. HTML5系列四(WebWorker、地理定位)
  6. Factstone Benchmark
  7. PHP错误日志控制(display_errors和error_reporting)
  8. Linux基础※※※※访问Windows共享文件夹
  9. QWidget、QDialog、QMainWindow的异同点
  10. Java NIO缓冲
  11. Oracle JDBC通过占位符可以查询可变长字段,不可查询固定长度字段
  12. sprintf,多少钱你知道?
  13. TSP问题(旅行商问题)[分支限界法]
  14. webservice03#schema#元素属性定义
  15. Java中的读写锁
  16. Eclipse格式化整个项目
  17. MySQL的or/in/union与索引优化
  18. python小练--使用正则表达式将json解析成dict
  19. 优化 PHP 代码技巧
  20. JS在生成csv文件时,&quot;,&quot;逗号问题处理.

热门文章

  1. 4、Normal Equation 的向量投影解法与几何和直觉解释
  2. Python3开启自带http服务
  3. typora文件中不显示公式
  4. urllib编码问题踩坑
  5. python 的多线程
  6. etcd 入门
  7. python37
  8. torch&amp; tensorflow
  9. java位运算分析
  10. 面试之AQS