Bootstrap

python实现账户管理系统(全网功能最全!!!+ 保姆级注释)

# python账户管理系统 作者:WWJJ!
import json  # 导入jason模块,用于读写jason文件

# 存储用户信息的文件名
user_file = "users.json"

# 读取用户数据
try:
    with open(user_file, 'r', encoding='utf-8') as f:  # 打开文件,以只读模式读取
        users = json.load(f)  # 将文件中的json数据解析为python对象
except FileNotFoundError:  # 若出现异常,建立空字典
    users = {}

# 保存用户数据


def save_data():
    with open(user_file, "w") as f:  # 打开文件以写入的额模式写入
        json.dump(users, f)  # 将python对象转为json格式并写入文件


class Account:
    def __init__(self, username, password, ident, balance, is_blocked):
        self.username = username
        self.password = password
        self.ident = ident
        self.balance = balance
        self.is_blocked = is_blocked

    def to_dict(self):  # 用于将Account对象的username和password和ident属性打包为一个字典并返回
        return {"username": self.username,  # 便于将用户的信息写入文件或先从文件中读取出来
                "password": self.password,
                "ident": self.ident,
                "balance": self.balance,
                "is_blocked": self.is_blocked}

# 创建新用户


def creat_account():
    username = input("请输入姓名:")
    if username == "super":
        print("出错了!")
        return
    if username in users:
        print("该用户名已被使用,请重新输入:")
        return
    password = input("请设置密码:")
    ident = "0"
    balance = 0
    account = Account(username, password, ident="2",
                      balance="0", is_blocked=False)
    users[username] = account.to_dict()
    save_data()
    print("账户添加成功!")

# 用户登录


def login():
    while True:
        username = input("请输入用户名:")
        if username == "super":
            superpass = input("请输入密钥:")
            if superpass == "12345678":
                super_interface()
                return
        if username not in users:
            print("用户名不存在,请重新输入!")
            continue
        if users[username]["is_blocked"] == True:
            print("该账户已被封禁,无法登录!请联系管理员解封")
            return None
        password = input("请输入密码:")
        tries = 2
        while users[username]["password"] != password:
            if tries == 0:
                users[username]["is_blocked"] = True
                save_data()
                print("密码错误次数已达上线,该账户已被封禁!请联系管理员解封!")
                return None
            print("密码错误,请重新输入")
            print(f"还有{tries}次尝试机会")
            password = input("请输入密码:")
            tries -= 1

        print(f"登录成功!欢迎回来,{username}!")
        if users[username]["ident"] == "0":
            admin_interface()
            return
        elif users[username]["ident"] == "1":
            member_interface(username)
            return
        else:
            user_interface(username)
            return

# 修改用户名


def change_username(username):
    new_username = input("请输入新用户名:")
    if new_username in users:
        print("该用户名已被注册,请重新输入!")
        return
    users[new_username] = users.pop(username)  # 更新用户名
    users[new_username]["username"] = new_username
    save_data()
    print("用户名修改成功!")

# 修改密码


def change_password(username):
    new_password = input("请输入新的密码:")
    users[username]["password"] = new_password
    save_data()
    print("密码修改成功!")

# 升级为会员


def upgrade_member(username):
    if int(users[username]["balance"]) >= 100:
        users[username]["ident"] = "1"
        save_data()
        print("升级会员成功!")
    else:
        print("充值金额不足100,无法升级!")

# 充值


def recharge_balance(username):
    amount = int(input("请输入充值金额(整数):"))
    users[username]["balance"] = str(
        (int(users[username]["balance"]) + amount))
    save_data()
    print("充值成功!")
    if users[username]["ident"] == "2" and int(users[username]["balance"]) >= 100:
        upgrade_member(username)

# 删除用户


def delete_account():
    username = input("请输入要删除的账户名:")
    if username == "super":
        print("出错!请重试!")
        return
    if username not in users:
        print("用户名不存在,请重新输入!")
        return
    if users[username]["ident"] == "0":
        print("管理员账户不能被删除!")
        return
    del users[username]  # 从用户列表中删除用户
    save_data()
    print("该账户删除成功!")

# 修改用户权限


def change_ident():
    username = input("请输入要修改权限的用户名:")
    if username not in users:
        print("该用户名不存在,请重新输入!")
        return
    if username == "super":
        print("出错了!请重新输入!")
        return
    ident = input("请输入该用户新的身份(0-管理员, 1-会员, 2-普通用户):")
    users[username]["ident"] = ident
    save_data()
    print("修改成功!")

# 封号


def block_account():
    username = input("请输入要封禁的用户名:")
    if username not in users:
        print("该用户名不存在,请重新输入!")
        return
    if username == "super":
        print("出错了!请重新输入!")
        return
    users[username]["is_blocked"] = True
    save_data()
    print("封禁账户成功!")


def view_all_users():
    print("{:<10} {:<10} {:<10} {:<10}".format(
        '用户名', '身份', '余额', '是否被封禁'))
    for user in users.values():
        print("{:<10} {:<10} {:<10} {:<10}".format(
            user["username"], user["ident"], user["balance"], user["is_blocked"]))

# 管理员界面


def admin_interface():
    while True:
        print("=" * 30)
        print("管理员界面")
        print("1.添加用户")
        print("2.删除账户")
        print("3.修改用户权限")
        print("4.封禁用户")
        print("5.产看所有用户信息")
        print("6.退出系统")
        choice = input("请选择操作:")
        if choice == "1":
            creat_account()
        elif choice == "2":
            delete_account()
        elif choice == "3":
            change_ident()
        elif choice == "4":
            block_account()
        elif choice == "5":
            view_all_users()
        elif choice == "6":
            print("谢谢使用!再见!")
            break
        else:
            print("请选择正确的操作!")

# 会员界面


def member_interface(username):
    while True:
        print("=" * 30)
        print("会员界面")
        print("1.修改用户名")
        print("2.修改密码")
        print("3.充值余额")
        print("4.退出系统")
        choice = input("请选择操作:")
        if choice == "1":
            change_username(username)
        elif choice == "2":
            change_password(username)
        elif choice == "3":
            recharge_balance(username)
        elif choice == "4":
            print("谢谢使用!再见!")
            break
        else:
            print("请选择正确的操作!")

# 普通用户界面


def user_interface(username):
    while True:
        print("=" * 30)
        print("普通用户界面")
        print("1.修改密码")
        print("2.充值余额")
        print("3.退出系统")
        choice = input("请选择操作:")
        if choice == "1":
            change_password(username)
        elif choice == "2":
            recharge_balance(username)
        elif choice == "3":
            print("谢谢使用!再见!")
            break
        else:
            print("请选择正确的操作!")

# super界面


def super_interface():
    while True:
        print("=" * 30)
        print("1.查看所有用户信息")
        print("2.解封单个用户")
        print("3.解封所有用户")
        print("4.退出系统")
        choice = input("请选择操作:")
        if choice == "1":
            view_all_users()
        elif choice == "2":
            username = input("请输入要解封的用户名:")
            if username not in users:
                print("该用户名不存在,请重新输入!")
                continue
            users[username]["is_blocked"] = False
            save_data()
            print("该账户已解封")
        elif choice == "3":
            for user in users.values():
                user["is_blocked"] = False
            save_data()
            print("所有用户已解封!")
        elif choice == "4":
            print("谢谢使用,再见!")
            break
        else:
            print("请输入正确的操作")


# 主程序
while True:
    print("-"*30)
    print("欢迎使用账户管理系统!")
    print("1.创建账户")
    print("2.登录账户")
    print("3.退出系统")
    choice = input("请选择操作:")
    if choice == '1':
        creat_account()
    elif choice == '2':
        login()  # 用户登录
    elif choice == "3":
        print("感谢使用!")
        break
    else:
        print("请输入正确的选择!")

;