ATM+购物车

ATM+购物车

src主程序

# 主程序
import os
import json
from conf import settings
from common import common

# 定义公用类 username登录名字  is_login登录状态
user_data = {
    'username': None,
    'is_login': False
}


# 退出功能
def loginout():
    print('\033[1;31m退出成功\033[0m')
    exit()


# 注册功能
def register():
    print('\033[1;34m注册功能\033[0m')
    while True:
        username = input('请输入姓名').strip()
        # path_file=os.path.join(settings.BASE_DIR,'db','%s.json' %username)
        if os.path.exists(common.PATH_FILE(username)):
            # 文件存在
            print('\033[1;31m用户已存在\033[0m')
            continue
        password = input('请输入密码').strip()
        # 创建保存的字典
        user_dic = {
            'username': username,
            'password': password,
            'acount': 15000,
            'locked': False
        }
        # 保存到db下 用json.dump
        with open(common.PATH_FILE(username), 'w', encoding='utf-8')as f:
            json.dump(user_dic, f)
            print('\033[1;31m注册成功\033[0m')
            break


# 登录功能
def login():
    print('\033[1;34m登录功能\033[0m')
    count = 0
    if user_data['is_login']:
        print('\033[1;31m')
        print('账户已经登录,', end='')
        print('登录账户为:%s' % user_data['username'])
        print('\033[0m')
        return
    while True:
        username = input('请输入姓名:').strip()
        # path_file=os.path.join(settings.BASE_DIR,'db','%s.json'%name)
        if not os.path.exists(common.PATH_FILE(username)):
            print('\033[1;31m账户不存在\033[0m')
            break
            # `查询用户信息
        with open(common.PATH_FILE(username), 'r', encoding='utf-8')as f:
            data = json.load(f)
        # 判断用户状态
        if data.get('locked'):
            print('\033[1;31m密码错误三次,账户已锁定,请联系12345678911\033[0m')
            break
        password = input('请输入密码:').strip()
        if data.get('password') == password:
            print('\033[1;31m登陆成功\033[0m')
            user_data['username'] = username
            user_data['is_login'] = True
            break
        else:
            print('\033[1;31m密码错误,登陆失败\033[0m')
            count += 1
            if count == 3:
                # 错三次锁定
                data['locked'] = True
                with open(common.PATH_FILE(username), 'w', encoding='utf-8')as f1:
                    json.dump(data, f1)
                    print('\033[1;31m密码错误三次,账户已锁定,请联系12345678911\033[0m')
                    break


# 提现功能
def withdraw():
    print('\033[1;34m提现功能\033[0m')


# 转账功能
def transfer():
    pass


# 充值功能
def pay():
    pass


# 查看余额功能
def check_money():
    pass


# 查看流水
def check_flow():
    pass


# 加入购物车
def add_shopping_car():
    pass


# 查看购物车
def check_shop_car():
    pass


func_dic = {

    '0': ['退出', loginout],
    '1': ['注册', register],
    '2': ['登录', login],
    '3': ['提现', withdraw],
    '4': ['转账', transfer],
    '5': ['充值', pay],
    '6': ['查看余额', check_money],
    '7': ['查看流水', check_flow],
    '8': ['加入购物车', add_shopping_car],
    '9': ['查看购物车', check_shop_car],

}


def main():
    while True:
        for i in func_dic:
            print(i, func_dic[i][0])
        choise = input('请输入你要选择功能对应的的编号:').strip()
        if choise not in func_dic:
            print('请从新输入你要选择的编号')
            continue
        func_dic[choise][1]()

conf-settings

#配置文件

import os

#根路径
BASE_DIR=os.path.dirname(os.path.dirname(__file__))


# #打开名字.json文件登录或者注册,写在了common里不知道对不
# def PATH_FILE(username):
#     return os.path.join(BASE_DIR, 'db', '%s.json' % username)

posted @ 2021-08-19 08:40  迪迦张  阅读(13)  评论(0编辑  收藏  举报