第二天习题

"""
优化:
1.购买商品信息存在文件里
2.可以添加商品,修改商品价格
已购商品,余额记录【用户金额只需要输入一次】
"""
思路:
  1.用户余额,只输入一次后,下次进来就是上次保存的值,所以不能保存在内存中,并且每次退出程序的时候保存
  2.购买的商品也需要保存下来
  3.商家可以添加和修改商品价格,说明有两个入口,并且 商家的修改要时时反映到用户这边
简单的流程图:
  

因为没有学习数据库,所以所有的存储数据都记录到文件中
#这里有个问题,就是用户在都买的时候,商家正在修改商品价格,所以商家正在修改这个商品价格的时候,需先下架,如果用户正在购买这个商品,则提示商品已下架不可购买:这里做不到这种实时同步,这里不考虑这种情况
# import collections
import os
def readFile(file,modle):
    #判断文件是否存在
    if os.path.exists(file) :
        bf = open(file,modle)
        res = bf.read()
        bf.close()
        return res
    else :
        return None
def saveFile(file,res):
    gf = open(file, "w")
    gf.write(res)
    gf.close()

def bussessFun():
    # eval 讲类型的字符串转为类型
    res = readFile("goods", "r")
    goods = {}
    if  res :
        #将字典类型的字符串转为字典
        goods = eval(res)
    print(goods)
    # print(res)
    # print(goods)
    #修改商品价格
    # goods['iphoneX'] = 7000
    # #添加个商品
    # goods['watch'] = 5600
    #这里做成商家输入
    while True:
        goodsname = input("input goodsname if you want to exit input q:")
        if 'q' == goodsname :
            break
        price = int(input("input %s price :"%goodsname))
    #商品存在,修改价格,商品不存在,保存商品
        goods[goodsname] = price
    #循环结束,保存商品
    saveFile("goods",str(goods))
def consumer():
    res = readFile("goods", "r")
    goods = {}
    if res:
        goods = eval(res)
    if len(goods) == 0 :
        print("has no goods")
        exit()
    #查询已买的商品
    hasBuy = readFile("hasBuy","r")
    if not hasBuy:
        hasBuy = []
    else:
        #将列表类型的字符串转为列表
        hasBuy = eval(hasBuy)
    moneys = {}
    moneyres = readFile("money", "r")
    if  moneyres:
        moneys = eval(moneyres)
    if "money" not in moneys:
    #展示所有的商品和价格,以及所在的编号,因为字典是无序的这里使用列表
    #百度的方法,使用collections模块,变成有序字典
    # z = collections.OrderedDict(goods)
    # print(z)
    #每次购买完后退出程序,需要将余额保存下来
        money = int(input("input your money:"))
    else:
        money = moneys.get("money")
    #这里使用字符串的方法转为列表
    goods = str(goods).strip("{").strip("}").split(",")
    # print(goods)
    # hasBuy = hasBuy.split(",")
    for i,v in enumerate(goods):
        print(i,v)
    while True:
        index = input("input you want to buy:,if 'q' exit:")
        if index.isdigit():
            index = int(index)
            if index < len(goods) and index >= 0:
                #将买的商品和价格
                goodsprice = goods[index]
                # print(goodsprice.split(":")[1])
                price = int(goodsprice.split(":")[1])
                if price < money :
                    money -= price
                    hasBuy.append(goodsprice)
                else:
                    print("you don't have enough mmoney")
        elif 'q' == index:
            break
        else:
            pass
    #购买结束,将余额和购买的商品保存下来
    moneys["money"] = money
    saveFile("money", str(moneys))
    saveFile("hasBuy", str(hasBuy))
consumer();
View Code

 

转载于:https://www.cnblogs.com/zylong1201/p/7994041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值