项目的设计、购物车系统

一、项目设计

1、项目的生命周期:

1.需求分析

2.项目设计

3.分任务开发程序

4.项目测试

5.上线运行

6.维护更新

2、经典的三层结构

1.用户界面(用户功能)层 : 接收用户数据 ,或者展示数据给用户
2.业务逻辑(接口)层:   处理用户界面层得到的数据,进行判断 验证
3.数据访问(数据处理)层(持久层) : 帮助业务逻辑层 保存数据 或是读取数据

与程序处理数据的三步刚好是对应

一个应用程序的本质,就是在帮用户处理数据,一定分为三步
1.用户通过某种方式将数据交给程序 (手机APP 浏览器,PC段程序)
2.程序在拿到数据之后进行分析 判断数据是否是正确的 (python c,java)
3.将正确数据进行存储 (数据库 文件)

 界面层与数据层不应该直接通讯

二、购物车系统

1、文件

2、系统入口

I.所需信息

#系统用户信息
sys_msg = '''
欢迎使用购物车简单系统,请选择:
1.注册 | 2.登录 | 3.账户 | 4.充值 | 5.购物 | 6.支付 | 7.购物车 | 10.注销 | 0.退出
>>>'''

# 功能字典
method_dic = {
    '1': regist,
    '2': login,
    '3': account,
    '4': top_up,
    '5': shopping,
    '6': pay_maney,
    '7': shop_cart_info,
    '10': logout
}

II.系统入口

def system():
    while True:
        choice = input(sys_msg).strip()
        if choice == '0':
            print('系统退出!')
            return
        if not choice in method_dic:
            print('输入有误,请出现输入')
            continue
        method_dic[choice]()

system()

3、账户操作

I.所需信息

# 将文件信息读取到内存中,以变量存储,后期需要大量与这些信息交互
# 如何设计存储用户信息的集合变量
# 以用户名为key的dict,value可以为[]|{}
users_dic = {} # 从文件中读取来
'''
{
    'abc': {'ps': '123', 'money': 0},
    'qwe': {'ps': '000', 'money': 1000}
}
'''
# 存储当前登录成功的用户的信息
user = {}
# {'usr': 'abc', 'ps': '123', 'money': 0}

II.所需方法

# 获取所有已注册的用户,存放到user_dic
# 获取文件中用户信息
def get_users():
    # 如果用户集合已经有值,代表用户信息已经读取过了,不需要重复读取
    if users_dic:
        return users_dic
    # 读文件,存用户们
    with open('usr.info', 'rt', encoding='utf-8') as f:
        # "abc|123|0,qwe|123|1000" or "abc|123|0" or none
        data = f.read()
        if not data:
            # 文件中并没有用户信息,那么用户集合也不需要存储,可以直接返回
            return users_dic
        # ['abc|123|0', 'qwe|123|1000'] or ['abc|123|0']
        data_list = data.split(',')
        for d in data_list:
            # ['abc', '123', '0']
            user_info = d.split('|')
            usr = user_info[0]
            ps = user_info[1]
            money = user_info[2]
            # 按照 'abc': {'ps': '123', 'money': 0} 存储到 users_dic
            users_dic[usr] = {'ps': ps, 'money': money}
        return users_dic

III.注册

def regist():
    print("注册界面...")
    # 获取所有用户信息
    users = get_users()
    # 账号输入操作
    temp_info = ""
    while True:
        usr = input(temp_info + "输入账号:").strip()
        # 输入的用户名有格式问题
        if not usr:  # 用户名为空
            print("账号不能为空!")
            temp_info = "请重新"
            continue
        # 用户已存在
        if usr in users:
            print("用户已存在,请更换用户名!")
            temp_info = "请重新"
            continue
        # 用户不存在,可以进入输入密码阶段
        break
    # 密码输入操作
    temp_info = ""
    while True:
        ps = input(temp_info + "输入密码:").strip()
        # 输入的密码有格式问题
        if len(ps) < 3:
            print("密码过短!")
            temp_info = "请重新"
            continue
        # ... 添加其他密码安全判断
        break
    # 账号密码均满足条件,可以注册(写入文件)
    with open('usr.info', 'at', encoding='utf-8') as f:
        # 文件是否为空
        # 为空abc|123|0 不为空,qwe|123|0
        # users是否为空可以直接反映文件是否为空
        if users:
            user_info = ',%s|%s|%d' % (usr, ps, 0)
        else:
            user_info = '%s|%s|%d' % (usr, ps, 0)
        f.write(user_info)
    # 文件操作完代表信息更新到文件中了,
    # 还需要将信息更新到内存字典中
    users[usr] = {'ps': ps, 'money': 0}
    print('注册成功!')

IV.登录

# 登录
def login():
    global user
    print("登录界面...")
    # 获取所有用户信息
    users = get_users()
    # 当前是否为登录状态
    # 可以通过user(存储已登录账号)来反映是否为登录状态
    if user:
        print("系统已经处于登录状态!")
        return
    # 用户名输入
    temp_info = ""
    while True:
        usr = input(temp_info + '输入账号:').strip()
        # 账号不能为空
        if not usr:
            print("账号不能为空!")
            temp_info = "请重新"
            continue
        # 账号不存在
        if not usr in users:
            print("输入用户名不存在")
            # 文件为空,没有必要继续,不为空,可以让用户重新输入
            if users:
                temp_info = "请重新"
                continue
            return
        break
    # 输入密码操作
    temp_info = ""
    count = 0
    while count < 3:
        ps = input(temp_info + '输入密码:').strip()
        if users[usr]['ps'] == ps:
            print('登录成功!')
            # money在二次以后操作文件可能已经拥有金额
            money = users[usr]['money']
            # 直接赋值代表覆盖,方法内不能直接覆盖全局变量,需要做global处理
            user = {'usr': usr, 'ps': ps, 'money': money}
            break
        print('密码输入错误!')
        temp_info = "请重新"
        count += 1

V.账户

# 账户
def account():
    if not user:
        print('请先登录系统!')
        return
    user_info = '账户:%s | 密码:%s | 金额:%d ' % (user['usr'], user['ps'], user['money'])
    print(user_info)

VI.注销

# 注销
def logout():
    if not user:
        print('未有用户登录,不需注册!')
        return 
    user.clear()
    print('注销成功!')

4、充值购物支付

I.所需信息

# 登录成功后,对于商品的一系列操作
# 商品列表
goods_dic = {'1': 'iPhone', '2': 'Mac', '3': 'iPad'}
price_dic = {'iPhone': 100, 'Mac': 200, 'iPad': 50}
# 购物车
shop_car = {}
# {'iPhone': 3, 'iPad': 1}
goods_msg = '''
请添加商品到购物车:
1.iPhone | 2.Mac | 3.iPad | 0.退出购买'''

II.所需方法

# 对密码或金额进行修改
def update_info(k, v):
    # 需要更新的内容有:
    # 1.当前登录状态下的用户
    # 2.内存中的用户们
    # 3.文件中的用户信息
    # 更新1号位
    # 区分更新的类型
    if k == 'money':
        user[k] += v
    else:
        user[k] = v
    # 通过1号位更新2号位
    users = get_users()
    users[user['usr']][k] = user[k]
    # 通过2号位更新3号位
    #
    # {'abc': {'ps': '123', 'money': 0},'qwe': {'ps': '000', 'money': 1000}}
    # 转换为
    # "abc|123|0,qwe|123|1000"
    # 写入文件
    # dict 转换为 str
    users_info = ''
    for k, v in users.items():
        usr = k
        ps = v['ps']
        money = str(v['money'])
        if not users_info:
            users_info += '|'.join((usr, ps, money))
        else:
            users_info += ',' + '|'.join((usr, ps, money))
    # 转换完毕后便可以写入文件
    with open('usr.info', 'wt', encoding='utf-8') as f:
        f.write(users_info)

III.充值

# 充值
def top_up():
    if not user:
        print('系统未登录!')
        return
    temp_info = ""
    while True:
        money = input(temp_info + '输入充值金额:').strip()
        if not money.isdigit():
            print('输入金额有误!')
            temp_info = "请重新"
            continue
        money = int(money)
        break
    # 更新金额
    update_info('money', money)
    print("充值完毕!")

IV.购物

# 购物
def shopping():
    if not user:
        print('系统未登录!')
        return
    print(goods_msg)
    while True:
        # 商品编号
        goods_num = input("商品编号:").strip()
        if goods_num == '0':
            print('退出购买!')
            break
        if not goods_num in goods_dic:
            print('商品不存在!')
            continue

        while True:
            # 商品数
            count = input('商品个数:').strip()
            if not count.isdigit():
                print('个数有误!')
                continue
            count = int(count)
            # 编号与个数均正确
            # 加入购物车:{商品名: 个数}
            goods = goods_dic[goods_num]
            # 通过商品与购物车进行匹配,判断商品个数是累加还是赋值
            if not goods in shop_car:
                shop_car[goods] = count
            else:
                shop_car[goods] += count
            # 更新完购物车后代表一次购物车添加完毕
            # 查看一下当前购物车信息
            shop_cart_info()
            break
    # 进入支付:余额充足直接付款,不足充值
    pay_money()

V.支付

# 支付
def pay_money():
    if not user:
        print('系统未登录!')
        return
    # 由购物来到支付,也可能主动调用支付
    if not shop_car:
        print("购物车为空,请前往购物!")
        return
    # 计算购物车内商品总价
    total = 0
    for goods in shop_car:
        total += price_dic[goods] * shop_car[goods]
    # 判断余额与商品总价大小
    if user['money'] >= total:
        print('余额充足,购买成功!')
        # 更新信息
        reduce = 0 - total
        update_info('money', reduce)
        # 支付成功后,需要清空购物车
        shop_car.clear()
    else:
        print('余额不足,请充值!')
        top_up()

VI.购物车

# 购物车
def shop_cart_info():
    if not shop_cart:
        print("购物车未空,可前往购物!!!")
        return
    print("购物车: ", shop_cart)

 

 

原文地址:https://www.cnblogs.com/dongzhihaoya/p/10109242.html