• 代码1


    题目: 购物车程序
    业需求:
    数据结构:
    goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 10},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
    ......
    ]

    功能要求:
    基础要求:
    1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
    2、允许用户根据商品编号购买商品
    3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
    4、可随时退出,退出时,打印已购买商品和余额
    5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示

    扩展需求:
    1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
    2、允许查询之前的消费记录

    流程图

    话不多,上代码:

    #!/usr/bin/env python
    # _*_ coding: utf-8 _*_
    """
    @author: caimengzhi
    @license: (C) Copyright 2013-2017.
    @contact: 610658552@qq.com
    @software: pycharm 2017.02
    @project: luffy
    @file: shopping_car.py
    @time: 2018/1/17 13:50
    @desc:
    数据结构:
        goods = [
        {"name": "电脑", "price": 1999},
        {"name": "鼠标", "price": 10},
        {"name": "游艇", "price": 20},
        {"name": "美女", "price": 998},
        ......
        ]
    功能要求:
        基础要求:
            1、启动程序后,输入用户名密码后,让用户输入工资,然后打印商品列表
            2、允许用户根据商品编号购买商品
            3、用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒
            4、可随时退出,退出时,打印已购买商品和余额
            5、在用户使用过程中, 关键输出,如余额,商品已加入购物车等消息,需高亮显示
        扩展需求:
            1、用户下一次登录后,输入用户名密码,直接回到上次的状态,即上次消费的余额什么的还是那些,再次登录可继续购买
            2、允许查询之前的消费记录
    """
    goods = [
    {"name": "电脑", "price": 1999},
    {"name": "鼠标", "price": 100},
    {"name": "游艇", "price": 20},
    {"name": "美女", "price": 998},
    ]
    # 高亮显示字体颜色
    red = '33[1;31;40m'     # 红色
    green = '33[1;32;40m'   # 绿色
    blue = '33[1;34;40m'    # 蓝色
    end = '33[0m'            # 无色
    
    # 高亮闪烁显示字体颜色
    red_twinkle = '33[5;31;40m'     # 红色,闪烁
    green_twinkle = '33[1;32;40m'   # 绿色,闪烁
    blue_twinkle = '33[1;34;40m'    # 蓝色,闪烁
    
    # 预设账号和密码
    USER_INFO = {"cmz": "cmz"}
    
    # 标题头
    SYSTEM_AUTH_TITLE_MSG = " 购物车用户认证 ".center(50, "-")
    SYSTEM_GOODS_TITLE_MSG = "商品列表".center(40, '-')
    SYSTEM_SHOPPING_TITLE_MSG = "编号".ljust(4, " ") + "商品名称".ljust(8, " ") + "单价"
    
    # 用户,密码类错误
    ERROR_INPUT_NULL = red + "抱歉,输入不能为空" + end
    ERROR_INPUT_ILLEGAL = red + '抱歉,输入必须是q/c或者商品编号' + end
    ERROR_GOODS_ID = "抱歉,该商品" + red + "编号不存在" + end
    ERROR_USER = red + "该账号不存在" + end
    ERROR_USER_NULL = red + "密码不能为空" + end
    ERROR_PWD_NULL = red + "密码不能为空" + end
    ERROR_PWD = red + "密码错误" + end
    
    # 工资类错误
    ERROR_SALARY_NULL = red + "工资不能为空" + end
    ERROR_SALARY = "抱歉你输入的" + red + "工资" + end + "错误。请输入正确工资。"
    ERROR_SALARY_ZERO = red + "工资不能为零" + end
    
    # 通知类消息
    INFO_INPUT_NAME = "请输入姓名:"
    INFO_INPUT_PWD = "请输入密码:"
    INFO_INPUT_SALARY = "请输入工资: "
    INFO_SHOPPING_CAR_NULL = "购物车空空的,抓紧去购物吧"
    INFO_SHOPPING_LIST = " 购物车列表 ".center(40, '-')
    INFO_INPUT_Q_C = "q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: "
    
    # 退出标志位
    exit_flag = False
    exit_salary_flag = False
    exit_auth_flag = False
    buy_product_flag = False
    
    # 上次剩余的钱,上次购物车清单,本次购物车清单,初始化默认为0
    shop_car = []
    last_balance = 0
    last_product = []
    this_product = []
    
    # 读取工资输入状态信息
    with open("./data/system_salary_status.txt") as fd:
        for line in fd:
            exit_salary_flag = line
    
    # 读取上次购物情况
    if exit_salary_flag:
        # 读取上次购物后的结余
        with open("./data/account_balance.txt") as fd:
            for line in fd:
                last_balance = line
        salary = int(last_balance)
    
        # 读取上次购物车的清单
        with open("./data/purchased_product.txt") as fd:
            for item in fd:
                last_product.append(eval(item))
        for index in range(len(last_product)):
            last_product_dict = last_product[index]
    
    # 上次购物车商品导入到本次购物车
    shop_car = last_product
    
    while not exit_flag:
        # 1. 身份认证环境(用户名和密码)环节
        if not exit_auth_flag:
            print(SYSTEM_AUTH_TITLE_MSG)
            user_name = input(INFO_INPUT_NAME).strip()
            if user_name in USER_INFO:
                user_pwd = input(INFO_INPUT_PWD).strip()
                if not user_pwd:
                    print(ERROR_PWD_NULL)
                elif user_pwd == USER_INFO[user_name]:  # 用户名,密码验证成功
                    exit_auth_flag = True
                else:
                    print(ERROR_PWD)
            elif not user_name:
                print(ERROR_USER_NULL)
            else:
                print(ERROR_USER)
        # 2.  身份认证成功后进入验证输入工资环境
        if exit_auth_flag and not exit_salary_flag:
            salary = input(INFO_INPUT_SALARY).strip()
            if not salary:
                print(ERROR_SALARY_NULL)
                continue
            elif salary.isdigit():
                salary = int(salary)
                if salary == 0:
                    print(ERROR_SALARY_ZERO)
                else:
                    exit_salary_flag = True
            else:
                print(ERROR_SALARY)
        # 3. 身份认证环节和工资输入环节都认证成功后进入购物环节
        if exit_auth_flag and exit_salary_flag:
            if len(this_product) > 0 or len(last_product) > 0 or exit_salary_flag:
                print(SYSTEM_GOODS_TITLE_MSG)
                print(SYSTEM_SHOPPING_TITLE_MSG)
                for item, p in enumerate(goods, 1):
                    print("  %s.    %s      %s" % (item, p["name"], p["price"]))
            user_choice = input(INFO_INPUT_Q_C).strip()
            # 3.1 购物详情计算
            if user_choice.isdigit():
                user_choice = int(user_choice) - 1
                if user_choice < len(goods):        # 输入的商品序号要在范围之内
                    p_item = goods[user_choice]     # 选择的商品
                    if p_item["price"] <= salary:  # 买得起环节
                        this_product.append(p_item) # 本次选择的商品放在本次购物车中
                        salary -= p_item["price"]  # 减钱
                        buy_product_flag = True     # 记录购买行为
                        print("商品" + blue + "%s" % p_item["name"] + end + "已经被添加到购物车了")
                        print("当前你的余额: " + blue + "%s" % salary + end)
                    else:                          # 买不起环节
                        print("当前你的余额: " + red + "%s" % salary + ",余额不足,无法购买该商品!" + end)
                else:
                    print(ERROR_GOODS_ID)
            # 3.2 结束购物计算,(显示余额,购物车清单,存数据)
            elif user_choice == 'q' or user_choice == 'quit':
                print(INFO_SHOPPING_LIST)
                if len(this_product) == 0 and len(last_product) == 0:  # 只要上次和本次都没有商品,就显示购物车为空
                    print(INFO_SHOPPING_CAR_NULL)
                else:
                    shop_car = this_product + last_product               # 购物车为上次和本次之和
                    print(SYSTEM_SHOPPING_TITLE_MSG)
                    for item in range(len(shop_car)):
                        print("  %s.    %s      %s" % (item + 1, shop_car[item]["name"], shop_car[item]["price"]))
                    print("当前你的余额: " + blue + "%s" % salary + end)
                if buy_product_flag:                                             # 要是本次购买了物品就存入
                    with open("./data/purchased_product.txt", "a+") as fd:  # 购买的商品写入文件
                        for purchased_product in this_product:
                            fd.write(str(purchased_product))
                            fd.write("
    ")
                    with open("./data/account_balance.txt", "w") as fd:      # 余额写入文件
                        fd.write(str(salary))
                    with open("./data/system_salary_status.txt", "w") as fd:  # 输入工资状态写入文件
                        fd.write(str(exit_salary_flag))
                    buy_product_flag = False
                exit_flag = True
            # 3.3 检查购物车清单(显示购物车清单,余额)
            elif user_choice == 'c' or user_choice == 'check':    # 检查购物车情况
                if len(this_product) == 0 and len(last_product) == 0:
                    print(INFO_SHOPPING_CAR_NULL)
                else:
                    shop_car = this_product + last_product          # 购物车列表等于上次+本次
                    print(SYSTEM_GOODS_TITLE_MSG)
                    print(SYSTEM_SHOPPING_TITLE_MSG)
                    for item in range(len(shop_car)):
                        print("  %s.    %s      %s" % (item + 1, shop_car[item]["name"], shop_car[item]["price"]))
                print("当前你的余额: " + blue + "%s" % salary + end)
            elif not user_choice:
                print(ERROR_INPUT_NULL)
            else:
                print(ERROR_INPUT_ILLEGAL)
    View Code

    项目文件目录介绍
    本程序使用python3.5,使用pycharm IDE,亿图(流程图)
    leco@leco:~/luffy/module_1/shopping_car$ tree .
    .
    ├── data
    │   ├── account_balance.txt # 存放余额
    │   ├── purchased_product.txt # 存放上次购物车商品清单
    │   └── system_salary_status.txt # 存放工资是否输入过的状态
    ├── README.txt # 重要文件(读我)
    ├── shopping_car.py # 主程序
    └── 购物车流程图.bmp # 购物车流程图


    以下是执行过程
    # 第一次登录
    leco@leco:~/luffy/module_1/shopping_car$ python3 shopping_car.py
    ---------------------- 系统认证 ----------------------
    请输入姓名:cmz
    请输入密码:cmz
    请输入工资: 3000
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: c
    购物车空空的,抓紧去购物吧
    当前你的余额: 3000
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: 1
    商品电脑已经被添加到购物车了
    当前你的余额: 1001
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: c
    -----------------购物车清单------------------
    编号 商品名称 单价
    1. 电脑 1999
    当前你的余额: 1001
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: 2
    商品鼠标已经被添加到购物车了
    当前你的余额: 901
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: c
    -----------------购物车清单------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    当前你的余额: 901
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: q
    ---------------- 购物车列表 -----------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    当前你的余额: 901

    # 第二次登录
    leco@leco:~/luffy/module_1/shopping_car$ python3 shopping_car.py
    ---------------------- 系统认证 ----------------------
    请输入姓名:cmz
    请输入密码:cm
    密码错误
    ---------------------- 系统认证 ----------------------
    请输入姓名:cm
    该账号不存在
    ---------------------- 系统认证 ----------------------
    请输入姓名:cmz
    请输入密码:
    密码不能为空
    ---------------------- 系统认证 ----------------------
    请输入姓名:cmz
    请输入密码:cmz
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: c
    -----------------购物车清单------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    当前你的余额: 901
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: 3
    商品游艇已经被添加到购物车了
    当前你的余额: 881
    ------------------商品列表------------------
    编号 商品名称 单价
    1. 电脑 1999
    2. 鼠标 100
    3. 游艇 20
    4. 美女 998
    q[退出]/c[检查购物车],是否想购物?,是请输入商品编号: q
    ---------------- 购物车列表 -----------------
    编号 商品名称 单价
    1. 游艇 20
    2. 电脑 1999
    3. 鼠标 100
    当前你的余额: 881

  • 相关阅读:
    android-6
    android-5
    android-购物商城
    安卓简易计算器
    安卓第四周作业
    安卓第一周作业
    第十五周作业
    第十三周作业
    第十三周上机练习
    第三次安卓作业
  • 原文地址:https://www.cnblogs.com/caimengzhi/p/8303906.html
Copyright © 2020-2023  润新知