• 购物车--用户和商户入口


    '''
    此模块需要使用用户登陆模块代码

    用户入口:
    1、输入用户名和密码
    2、商品信息存在文件里
    3、已购商品和余额记录在文件里

    注:用户购买商品后退出,再次登陆会自动读取上次用户的余额

    商户入口:
    1、添加商品
    2、修改商品名称和价格
    3、删除商品

    备注:需要新建shop文件和购物车文件,其中shop文件保存商品信息:


    '''
    import os
    import userLogin
    import types

    def read_info(_name):
    if os.path.getsize("购物车"):
    remaining = 0
    with open("购物车","r",encoding="utf-8") as f:
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    if line["name"] == _name:
    remaining = line["shopping"][2]
    if remaining > line["shopping"][2]:
    remaining = line["shopping"][2]
    if remaining == 0:
    return None
    else:
    return remaining
    else:
    return None


    def memu(num):
    if os.path.getsize("shop"):
    with open("shop","r",encoding="utf-8") as f:
    print("------------商品------------")
    for index,line in enumerate(f):
    if line.strip() == "":
    continue
    index -= 1
    line = eval(line)
    info2 = '''
    {_index}、{_goods},价格:{_price}
    '''.format(_index = index + 1,
    _goods = line["goods"],
    _price = line["price"])
    print(info2)
    else:
    print("当前无商品")
    if num == 1:
    exit()

    def shopping_list(name):
    sum = 0
    flag = 0
    with open("购物车", "r+", encoding="utf-8") as f:
    for temp in f:
    if temp.strip() == "":
    continue
    temp = eval(temp)
    if temp["name"] == name:
    sum += temp["shopping"][1]
    info2 = '''
    商品:{_goods},价格:{_price}
    '''.format(_goods=temp["shopping"][0],
    _price=temp["shopping"][1])
    if flag == 0:
    print("---------购物清单---------")
    print(info2)
    flag = 1
    print("总计金额: %d" % sum)


    def user(_momey,name):
    choose = input("输入你要购买商品的编号 >>>:")
    if choose.isdigit():
    choose = int(choose)
    elif choose == 'q':
    shopping_list(name)
    exit()
    else:
    print("输入错误")
    res = read_info(name)
    if res != None:
    _momey = res
    f = open("shop", "r", encoding="utf-8")
    content = f.readlines()
    for i in range(len(content)):
    if choose - 1 == i:
    content[i] = eval(content[i])
    surplus = _momey - content[i]["price"]
    if surplus >= 0:
    _momey = surplus
    print("33[33;1m %s 33[0m 已加入购物车,你的当前余额为:33[32;1m %d 33[0m "%(content[i]["goods"],_momey))
    save(content[i],name,_momey)
    return _momey
    else:
    print("33[31;1m 你的余额不足,当前余额为:%d 33[0m"%_momey)
    return _momey
    f.close()
    def save(d,name,momey):
    dict = {}
    dict["name"] = name
    dict["shopping"] = [d["goods"],d["price"],momey]
    with open("购物车","a+",encoding="utf-8") as f:
    f.write(str(dict) + ' ')

    def deposit(name):
    while True:
    res = read_info(name)
    if res == None:
    momey = input("输入你的存款金额 >>>:")
    else:
    momey = res
    print("33[34;1m %s你好,你的当前余额是:%d 33[0m"%(name,momey))
    return momey
    if type(momey) != type(1):
    if momey.isdigit():
    momey = int(momey)
    return momey
    elif momey == 'q':
    exit()
    else:
    print("存款输入错误")
    continue

    def add_shop():
    with open("shop","a+",encoding="utf-8") as f:
    dict = {}
    goods = input("输入你要添加的商品名称 >>>")
    price = input("输入商品的价格(正整数) >>>")
    if price.isdigit():
    price = int(price)
    dict["goods"] = goods
    dict["price"] = price
    f.write(str(dict) +' ')
    print("33[31;1m 添加商品成功 33[0m")

    def modify_shop():
    list = []
    f = open("shop", "r+", encoding="utf-8")
    content = f.readlines()
    num = input("请输入你要修改的商品编号 >>>")
    if num.isdigit():
    num = int(num)
    else:
    print("输入错误")
    for i in range(len(content)):
    if num - 1 == i:
    content[i] = eval(content[i])
    choose = input("请输入你要修改商品的信息1、名称,2、价格 >>>")
    if choose.isdigit():
    choose = int(choose)
    else:
    print("输入错误")
    if choose == 1:
    goods = input("请输入修改后的商品名称 >>>")
    content[i]["goods"] = goods

    elif choose == 2:
    price = input("请输入修改后的商品价格 >>>")
    if price.isdigit():
    price = int(price)
    content[i]["price"] = price

    else:
    print("输入错误")
    list.append(content[i])
    f.truncate(0)
    f.close()
    with open("shop", "a+", encoding="utf-8") as f1:
    for temp in list:
    f1.write(str(temp))
    print("33[31;1m 修改商品成功 33[0m")

    def del_shop():
    list = []
    f = open("shop", "r+", encoding="utf-8")
    content = f.readlines()
    num = input("请输入你要删除的商品编号 >>>")
    if num.isdigit():
    num = int(num)
    else:
    print("输入错误")

    for i in range(len(content)):
    if num - 1 == i:
    continue
    list.append(content[i])
    f.truncate(0)
    f.close()
    with open("shop", "a+", encoding="utf-8") as f1:
    for temp in list:
    f1.write(temp)
    print("33[31;1m 删除商品成功 33[0m")

    def main1(name):
    res = input("请输入你的选择:1、用户,2、商户 >>>")
    if res.isdigit():
    res = int(res)
    if res == 1:
    momey = deposit(name)
    while True:
    memu(res)
    momey = user(momey,name)
    elif res == 2:
    while True:
    memu(res)
    res1 = input("请输入你的选择:1、添加,2、修改,3、删除 >>>")
    if res1.isdigit():
    res1 = int(res1)
    if res1 == 1:
    add_shop()
    elif res1 == 2:
    modify_shop()
    elif res1 == 3:
    del_shop()
    else:
    print("输入错误")
    elif res1 == 'q':
    exit()
    else:
    print("输入错误")
    elif res == 'q':
    exit()
    else:
    print("输入错误")

    if __name__ == "__main__":
    while True:
    userLogin.main()




    '''
    -------用户登陆模块代码------
    '''
    '''
    先创建user、lock文件,user存储账号密码,用于登录时判断,lock文件为账号错误3次后,冻结
    user内容格式为:
    {'name':'zhangsan','passwd':'123'}
    {'name':'lisi','passwd':'123'}
    {'name':'wangwu','passwd':'123'}
    '''

    import os
    import shopping

    def lock(_name):
    '''判断一个用户是否冻结'''
    if os.path.getsize("lock"):
    with open("lock","r") as f:
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    if line["name"] == _name and line["num"] == 3:
    return 0
    else:
    return -1
    else:
    return -1

    def clear_lock():
    '''当程序退出时,删除非冻结的账号信息'''
    list = []
    with open("lock","r+") as f:
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    if line["num"] == 3:
    list.append(line)
    f.truncate(0)
    with open("lock","w") as f1:
    for i in list:
    f1.write(str(i) + ' ')

    def login(_name,_passwd):
    '''用户登陆'''
    with open("user","r") as f:
    flag = 0
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    if line["name"] == _name:
    if line["name"] == _name and line["passwd"] == _passwd:
    return 0
    else:
    return 1
    return -1

    def write_lock(_name):
    '''将输入错误的账号写入lock文件'''
    #文件不为空
    if os.path.getsize("lock"):
    list = []
    with open("lock","r+") as f:
    flag = 0
    for line in f:
    if line.strip() == "":
    continue
    line = eval(line)
    #判断账号是否存在
    if line["name"] == _name:
    line["num"] += 1
    list.append(line)
    else:
    dict2 = {}
    dict2["name"] = _name
    dict2["num"] = 1
    list.append(dict2)
    with open("lock","w") as f1:
    for i in list:
    f1.write(str(i) + ' ')


    #空文件直接写入
    else:
    list1 = []
    dict1 = {}
    dict1["name"] = _name
    dict1["num"] = 1
    list1.append(str(dict1))
    with open("lock","w") as f2:
    for j in list1:
    f2.write(str(j) + ' ')

    def main():
    name = input("用户名:")
    res = lock(name)
    if res == 0:
    print("%s已被冻结,请联系管理员"%name)
    clear_lock()
    exit()
    passwd = input("密码:")
    res1 = login(name,passwd)
    if res1 == 0:
    print("欢迎%s登陆CBD购物中心"%name)
    while True:
    shopping.main1(name)
    elif res1 == 1:
    print("密码错误")
    write_lock(name)
    else:
    print("账号不存在")

    res2 = lock(name)
    if res2 == 0:
    print("%s已被冻结,请联系管理员" %name)
    clear_lock()
    exit()

    if __name__ == "__main__":
    while True:
    main()


  • 相关阅读:
    Java_File类
    Java_Math类和Random类
    Java_包装类
    Java_Stringbuilder和StringBuffer
    Java_String
    Java_数组, 懒得整理了 ---------------------> 未完, 待续
    Java_内部类
    Java_三大特征相关
    Java_垃圾回收机制(未掌握)
    重载(overload)和重写(override)的区别?
  • 原文地址:https://www.cnblogs.com/hqd2008/p/7639971.html
Copyright © 2020-2023  润新知