python自动化-作业2——A+

作者:郭刚志
内容: 完美购物车
1.0版完成时间:2018/12/8
功能简介:
    1、有两种模式:采购模式、查询模式
    2、多用户,可充值,采购数量可选
    3、可分级查看商品
    4、可查选当前的购买性息,可查询之前的购买记录
    5、支持商品层级、种类扩展


import time
import json

# user = [                            # 所有用户
#     {
#         'username': 'nb',           # 用户名
#         'password': '123',          # 密码
#         'salary': 0,                # 工资
#         'remainder': 0,             # 余额
#         'wares': {                  # 本次购买的商品
#             # 'wares_name':['num','time']               # 名称:[数量,购买时间]
#         },
#         'start_login':False             # 第一次登录
#         'Records_of_consumption':{  # 消费记录
#             # 'time':['num','wares_name']               # 购买时间:[数量,名称]
#         }
#     },
# ]

goods = {                           #所有货物分级
    '家电类':{
        '海尔':{
            '海尔最新款电视':2200,
            '海尔最新款冰箱':5200,
            '海尔最新款洗衣机':3600
        },
        '美的':{
            '美的最新款电视': 2100,
            '美的最新款冰箱': 5100,
            '美的最新款洗衣机': 3200
        }
    },
    '衣服类':{
        '优衣库':{
            '优衣库最新款外衣':380,
            '优衣库最新款裤子':450,
            '优衣库最新款鞋子':500
        },
        '七匹狼':{
            '优衣库最新款外衣': 390,
            '优衣库最新款裤子': 420,
            '优衣库最新款鞋子': 400
        }
    },
    '手机类':{
        '魅族 ΜⅢ':3000,
        '小米 ΧⅤ':2999,
        '华为 ΗⅥ':4000,
        '中兴 ΖⅣ':2600
    },
    '车类':{
        '宝马 Χ叁':300000,
        '奔驰 Β陆':150000,
        '特斯拉 ВЕ':130000
    }
}

login_flog = False          # 登录标志
end_layer_flog = False      # 商品菜单最后一层标志
exit_flog = False           # 用户按q键退出购物车标志
stop_flog = False           # 用户按b键停止购买标志
tamp_item = None            # 存放单个用户性息
goods_list = [goods]        # 数组的最末尾元素存放当前层级商品类
goods_tamp = None           # 存放单个字典

if __name__ == '__main__':
    print('欢迎来到购物车网上商城'.center(100,'-'))
    while True:
        user = json.load(open('userdata','r'))
        in_user = input('请输入您的账号:\n>>>')
        if in_user == 'q':                                                          # 退出
            exit_flog = True
            exit('您已经退出'.center(100,'-'))
        in_password = input('请输入您的密码:\n>>>')
        if in_password == 'q':                                                      # 退出
            exit_flog = True
            exit('您已经退出'.center(100,'-'))
        for i in user:
            if in_user == i.get('username') and in_password == i.get('password'):
                login_flog = True
                print('登录成功!'.center(40,'-'))
                tamp_item = i                                                       # tamp_item存放登录账户的所有信息字典
                tamp_item['wares'] = {}                                             # 拿购物车
                break
        if login_flog:                                                              # 已登录
            if tamp_item.get('start_login') is False:               # 判断是第一次登录要输入工资
                in_salary = input('请输入您的工资:\n>>>')
                tamp_item['salary'] = int(in_salary)                # 把工资存到账户信息字典
                tamp_item['remainder'] = tamp_item['salary']        # 把工资赋值给余额
                tamp_item['start_login'] = True                     # 标志位置位
                print('你现在的账户余额为:【{}】'.format(tamp_item.get('salary')))
            while True:
                goods_list = [goods]
                while True:
                    print('请选择您需要的商品序号:')
                    type_value = [*goods_list[-1].values()][0]
                    if type(type_value) == int:                                                 # 判断是商品列表中是最后一层   加¥价格
                        for index,i in enumerate(goods_list[-1]):
                            print('{}.{: <10}  ¥{}'.format(str(index+1),i,[*goods_list[-1].values()][index]))
                    else:                                                                       # 不是最后一层
                        for index,key in enumerate(goods_list[-1]):
                            print('{}.{}'.format(str(index+1),key))

                    in_goods_class = input('你选择的商品序号是(b.进入用户性息查询、c.充值、q.退出商城):>>>')
                    if in_goods_class =='q':                                                    # 退出
                        exit_flog = True
                        break
                    if in_goods_class == 'b':                                                   # 停止购买
                        stop_flog = True
                        break
                    if in_goods_class == 'c':                                                   # 充值
                        money = input('请输入您要充值的金额:>>>')
                        if money.isdigit():
                            tamp_item['remainder'] += int(money)
                            print('钱已到账,您现在的账户余额是:【{}】'.format(tamp_item['remainder']))
                            break
                        else:
                            print('输入错误,请重新选择购买商品或充值!'.center(40,'-'))
                            break
                    if in_goods_class.isdigit():
                        in_goods_class=int(in_goods_class)
                    else:
                        print('输入错误,请重新输入您要的商品序号!'.center(20,'-'))
                    if in_goods_class in list(range(1,len(goods_list[-1])+1)):                  # 是否有这个商品类
                        select_goods_class = [*goods_list[-1].values()][in_goods_class-1]       # select_goods_class是所选择的字典的值
                        select_goods_keys = [*goods_list[-1].keys()][in_goods_class-1]          # select_goods_class是所选择的字典的键
                        goods_list.append(select_goods_class)                                   # 压栈
                        if (type(select_goods_class) == int):                                   # 值是数字,说明是(最后层)
                            end_layer_flog = True
                            in_num = input('请输入购买数量(b.进入用户性息查询、q.退出商城):>>>')
                            if in_num == 'q':                                                   # 退出
                                exit_flog = True
                                break
                            if in_num == 'b':                                                   # 停止购买
                                stop_flog = True
                                break
                            if in_num == 'c':                                                   # 充值
                                money = input('请输入您要充值的金额:>>>')
                                if money.isdigit():
                                    tamp_item['remainder'] += int(money)
                                    print('钱已到账,您现在的账户余额是:【{}】'.format(tamp_item['remainder']))
                                    break
                                else:
                                    print('输入错误,请重新选择购买商品或充值!'.center(40,'-'))
                                    break
                            in_num = int(in_num)
                            if tamp_item['remainder'] >= select_goods_class*in_num:             #判断是否买得起
                                tamp_item['remainder'] -= select_goods_class*in_num                     #买了
                                print('购买成功'.center(50,'-'))
                            else:
                                print('您的余额已不足,请及时充值!'.center(40,'-'))
                                break
                            time_obj = time.localtime()                                         # 购买时间戳
                            time_str = '{}-{}-{} {}:{}:{}'.format(
                                time_obj.tm_year,
                                time_obj.tm_mon,
                                time_obj.tm_mday,
                                time_obj.tm_hour,
                                time_obj.tm_min,
                                time_obj.tm_sec)
                            if select_goods_keys in tamp_item['wares'].keys():
                                select_value_num = tamp_item['wares'][select_goods_keys][0]+in_num                      # 如果有同样的商品和之前的购买数量相加
                            else:
                                select_value_num = in_num
                            tamp_item['wares'][select_goods_keys] = [select_value_num,time_str]                         # 存入用户商品名称、数量、时间戳到用户账户
                            break
                while stop_flog:                                                                                        # 查询模式
                    print('请选择你要查询的内容:\n1.您当前的余额\n2.您的购物车(已付款)\n3.您的购买记录')
                    in_find_num = input('请输入你要查询的内容(b.采购商品、c.充值、q.退出):>>>')
                    if in_find_num == '1':                                                                                  # 查看当前用户余额
                        while True:
                            is_not_money = input('您当前的余额为:【{}】,是否充值(c.充值、任意键.退出):>>>'.format(tamp_item['remainder']))
                            if is_not_money == 'c':
                                money = input('请输入您要充值的金额:>>>')
                                if money.isdigit():
                                    tamp_item['remainder'] += int(money)
                                    print('钱已到账,您现在的账户余额是:【{}】'.format(tamp_item['remainder']))
                                    break
                                else:
                                    print('输入错误,请重新选择购买商品或充值:')
                                    break
                            else:
                                print('已退出充值'.center(50,'-'))
                                break
                    elif in_find_num == '2':                                                                                # 查看你现在购买的商品
                        print('您的购买清单'.center(100, '-'))
                        print('序号       名称                        数量     日期')
                        tamp_tamp = tamp_item.get('wares')
                        for num, i in enumerate(tamp_tamp.items()):
                            print('{: <3}      {: <15}{: <3}      {}'.format(num + 1, i[0], i[1][0], i[1][1]))
                        print('-' * 106)
                    elif in_find_num == '3':                                                                                # 查看登录账户所有的购买记录
                        print('您的购买记录'.center(100, '-'))
                        for w_name, w_value in tamp_item['wares'].items():  # 购买时间/购买商品名称 在字典中的位置互换
                            tamp_item['Records_of_consumption'].update({w_value[1]: [w_value[0], w_name]})  # 保存到 购买记录
                        print('序号        购买时间                  数量       商品名称')
                        for num, i in enumerate(tamp_item['Records_of_consumption'].items()):
                            print('{: <3}       {: <20}       {: <3}        {}'.format(num + 1, i[0], i[1][0], i[1][1]))
                        json.dump(user, open('userdata', 'w'))  # 将用户数据保存到userdata文件
                        print('-' * 106)
                    elif in_find_num == 'b':                        # 退出查询模式,进入购买模式
                        stop_flog = False
                        break
                    elif in_find_num == 'q':                        # 退出购物车
                        stop_flog = False
                        exit_flog = True
                        break
                    elif in_find_num == 'c':                        # 充值
                        money = input('请输入您要充值的金额:>>>')
                        if money.isdigit():
                            tamp_item['remainder'] += int(money)
                            print('钱已到账,您现在的账户余额是:【{}】'.format(tamp_item['remainder']))
                            break
                        else:
                            print('输入错误,请重新选择购买商品或充值:'.center(40, '-'))
                            break
                    else:
                        print('您的输入有误,请重新输入'.center(40,'-'))

                if exit_flog:
                    break
            if exit_flog:
                break
        else :
            print('帐号/密码错误,请重新输入!'.center(40, '-'))
    print('您已经退出了'.center(100, '-'))

    print('您的购买清单'.center(100, '-'))
    print('序号       名称                        数量     日期')
    tamp_tamp = tamp_item.get('wares')
    for num, i in enumerate(tamp_tamp.items()):
        print('{: <3}      {: <15}{: <3}      {}'.format(num + 1, i[0], i[1][0], i[1][1]))
    print('-' * 106)

 user_data文件 和 多用调试程序文件:

[{"username": "nb", "password": "123", "salary": 10000, "remainder": 2920, "wares": {}, "start_login": true, "Records_of_consumption": {"2018-12-1 19:52:23": [1, "\u4f18\u8863\u5e93\u6700\u65b0\u6b3e\u5916\u8863"], "2018-12-1 19:52:41": [1, "\u4f18\u8863\u5e93\u6700\u65b0\u6b3e\u978b\u5b50"], "2018-12-2 13:59:35": [1, "\u7f8e\u7684\u6700\u65b0\u6b3e\u51b0\u7bb1"], "2018-12-3 10:41:53": [1, "\u6d77\u5c14\u6700\u65b0\u6b3e\u7535\u89c6"], "2018-12-3 11:11:30": [1, "\u9b45\u65cf\u3000\u039c\u2162"]}}, {"username": "guogangzhi", "password": "ggz19891030", "salary": 12000, "remainder": 6800, "wares": {"\u6d77\u5c14\u6700\u65b0\u6b3e\u7535\u89c6": [1, "2018-12-1 19:53:38"], "\u9b45\u65cf\u3000\u039c\u2162": [1, "2018-12-1 19:53:46"]}, "start_login": true, "Records_of_consumption": {"2018-12-1 19:53:38": [1, "\u6d77\u5c14\u6700\u65b0\u6b3e\u7535\u89c6"], "2018-12-1 19:53:46": [1, "\u9b45\u65cf\u3000\u039c\u2162"]}}, {"username": "ggz", "password": "19891030", "salary": 15000, "remainder": 6300, "wares": {"\u6d77\u5c14\u6700\u65b0\u6b3e\u6d17\u8863\u673a": [1, "2018-12-1 19:54:53"], "\u7f8e\u7684\u6700\u65b0\u6b3e\u51b0\u7bb1": [1, "2018-12-1 19:55:1"]}, "start_login": true, "Records_of_consumption": {"2018-12-1 19:54:53": [1, "\u6d77\u5c14\u6700\u65b0\u6b3e\u6d17\u8863\u673a"], "2018-12-1 19:55:1": [1, "\u7f8e\u7684\u6700\u65b0\u6b3e\u51b0\u7bb1"]}}]
#author:    郭刚志
date:      2018/8/25
import json
user = [                            # 所有用户
    {
        'username': 'nb',           # 用户名
        'password': '123',          # 密码
        'salary': 0,                # 工资
        'remainder': 0,             # 余额
        'wares': {                  # 本次购买的商品
            # 'wares_name':['num','time']               # 名称:[数量,购买时间]
        },
        'start_login':False,             # 第一次登录
        'Records_of_consumption':{  # 消费记录
            # 'time':['num','wares_name']               # 购买时间:[数量,名称]
        }
    },
    {
        'username': 'guogangzhi',           # 用户名
        'password': 'ggz19891030',          # 密码
        'salary': 0,                # 工资
        'remainder': 0,             # 余额
        'wares': {                  # 本次购买的商品
            # 'wares_name':['num','time']               # 名称:[数量,购买时间]
        },
        'start_login':False,             # 第一次登录
        'Records_of_consumption':{  # 消费记录
            # 'time':['num','wares_name']               # 购买时间:[数量,名称]
        }
    },
    {
        'username': 'ggz',           # 用户名
        'password': '19891030',          # 密码
        'salary': 0,                # 工资
        'remainder': 0,             # 余额
        'wares': {                  # 本次购买的商品
            # 'wares_name':['num','time']               # 名称:[数量,购买时间]
        },
        'start_login':False,             # 第一次登录
        'Records_of_consumption':{  # 消费记录
            # 'time':['num','wares_name']               # 购买时间:[数量,名称]
        }
    },
]

json.dump(user, open('userdata', 'w'))  # 将用户数据保存到userdata文件

猜你喜欢

转载自www.cnblogs.com/guogangzhi/p/10087165.html