Day13_作业_学生管理系统(加入文件和模块)

  • 主界面
import filesmanager
import studentmanager
user_file = 'files/users.json'
# 登录模块
def login():
    user_name = input('请输入账号:')
    password = input('请输入密码:')
    # 判断账号是否存在
    all_user = filesmanager.read_json_file(user_file)
    if user_name not in all_user:
        print('您输入的账号不存在')
        return
    if password == all_user[user_name]:
        print('登录成功')
        # 进入学生管理界面
        studentmanager.user_name = user_name
        studentmanager.show_student_page()
    else:
        print('密码错误')


# 注册模块
def register():
    # 输入账号,判断账号位数是否是3-6位,不对则重新输入
    while True:
        user_name = input('请输入用户名(3-6位):')
        if not 3 <= len(user_name) <= 6:
            print('账号输入有误,请重新输入')

        else:
            break
    # 输入密码,判断密码位数是否是6-12位,不对则重新输入
    while True:
        password = input('请输入密码(6-12位):')
        if not 6 <= len(password) <= 12:
            print('密码输入有误,请重新输入')
        else:
            break

    # 判断输入的账号是否存在(存储账号的文件必须有内容或为{},否则报错)
    all_users = filesmanager.read_json_file(user_file)
    if user_name in all_users:
        print('注册失败,该账号已经被注册!')
        return None
    # 用字典保存账号和密码
    all_users[user_name] = password
    filesmanager.write_json_file(user_file, all_users)
    print('注册成功!')
    # 注册成功后为这个账号创建对应的json文件,文件名就是账号名,用于保存学生信息
    filesmanager.write_json_file('files/'+user_name+'.json', {})

# 显示主界面
def show_main_page():
#     从mainPage.txt文件中读取登录界面
    with open('files/mainPage.txt', encoding='utf-8') as f:
        page = f.read()
    while True:
        print(page)
        choice = input('请选择(1-3):')
        if choice == '1':
            login()
        elif choice == '2':
            register()
        elif choice == '3':
            return # 退出
        else:
            print('选择有误,请重新选择')


show_main_page()
  • 学生管理界面
import filesmanager
# 全局的用户名(保存登录成功的用户名)
user_name = ''
# 获取当前账号的学生信息
def get_all_student():
    user_message = filesmanager.read_json_file('files/' + user_name + '.json')
    all_student = user_message.get('all_student', [])
    return all_student


# 显示对于学生的信息
def show_student_message(stu:dict):
    return '学号:%s 姓名:%s 年龄:%s 电话:%s' % (stu['stu_id'], stu['name'],
                                        stu['age'], stu['tele'])


# 查看学生
def find_student():
    all_student = get_all_student()
    if len(all_student) == 0:
        print('暂无学生信息')
        return
    print('1.查看所有学生\n2.按姓名查找\n3.按学号查找\n4.返回')
    value = input('请选择')
    if value == '1':
        for stu in all_student:
            print(show_student_message(stu))
    elif value == '2':
        stu_name = input('学生姓名:')
        flag = False   # 是否有该学生
        for stu in all_student:
            if stu['name'] == stu_name:
                print(show_student_message(stu))
                flag = True
        if not flag:
            print('没有该学生!')
    elif value == '3':
        stu_id = input('学生学号:')
        flag = False   # 是否有该学生
        for stu in all_student:
            if stu['stu_id'] == stu_id:
                print(show_student_message(stu))
                flag = True
        if not flag:
            print('没有该学生!')
    else:
        return None

# 修改学生信息
def revise_student():
    all_student = get_all_student()
    user_message = filesmanager.read_json_file('files/' + user_name + '.json')
    if len(all_student) == 0:
        print('暂无学生信息')
        return
    stu_name = input('请输入学生姓名:')
    flag = False   # 是否有该学生
    for stu in all_student:
        if stu['name'] == stu_name:
            pass
            flag = True
    if not flag:
        print('没有该学生!')
        return

    value = input('请选择要修改的信息:\n1.年龄\n2.电话\n3.返回')
    if value == '1':
        age = input('请输入年龄:')
        for stu in all_student:
            if stu['name'] == stu_name:
                stu['age'] = age
                break
    elif value == '2':
        tele = input('请输入电话:')
        for stu in all_student:
            if stu['name'] == stu_name:
                stu['tele'] = tele
                break
    else:
        return
    user_message['all_student'] = all_student
    filesmanager.write_json_file('files/'+user_name+'.json', user_message)
    print('修改成功')

# 删除学生
def delete_student():
    all_student = get_all_student()
    user_message = filesmanager.read_json_file('files/' + user_name + '.json')
    id_num = user_message['id_num']
    if len(all_student) == 0:
        print('暂无学生信息')
        return
    stu_name = input('请输入要删除的学生姓名:')
    flag = False   # 是否有该学生
    for stu in all_student:
        if stu['name'] == stu_name:
            all_student.remove(stu)
            print('删除成功')
            user_message['all_student'] = all_student
            user_message['id_num'] = id_num - 1
            filesmanager.write_json_file('files/'+user_name+'.json', user_message)
            flag = True
    if not flag:
        print('没有该学生!')
        return


# 添加学生
def add_student():
    while True:
        # 输入基本信息
        name = input('请输入学生姓名:')
        age = input('请输入学生年龄:')
        tele = input('请输入学生电话:')
        # 读取该账号下的学生数据
        user_message = filesmanager.read_json_file('files/'+user_name+'.json')
        all_student = user_message.get('all_student',[])
        id_num = user_message.get('id_num', 0)
        # 生成学号
        id_num += 1
        stu_id = 'stu'+str(id_num).zfill(3)
        # 创建字典用于保存学生信息
        stu = {'stu_id': stu_id, 'name': name, 'age': age, 'tele': tele}
        all_student.append(stu)
        # 将学生信息写入文件并保存
        user_message['id_num'] = id_num
        user_message['all_student'] =all_student
        filesmanager.write_json_file('files/'+user_name+'.json', user_message)
        print('添加成功')
        print('1.继续添加\n2.返回')
        value = input('请选择(1-2)')
        if value == '1':
            continue
        elif value == '2':
            return
        else:
            return
# 显示主界面
def show_student_page():
    with open('files/studentpage','r',encoding='utf-8') as f:
        page = f.read()
    while True:
        print(page % user_name)
        value = input('请选择(1-5):')
        if value == '1':
            add_student()
        elif value == '2':
            find_student()
        elif value == '3':
            revise_student()
        elif value == '4':
            delete_student()
        elif value == '5':
            return
        else:
            print('选择错误,请重新选择')
  • 文件管理模块
import json

def read_json_file(file:str):
    """
    读一个json文件
    :param file: 变量file就是文件的地址
    :return: 返回文件内容
    """
    with open(file, 'r', encoding='utf-8') as f:
        return json.loads(f.read())


def write_json_file(file:str, obj):
    """
    将制定内容写入文件中
    :param file: 文件的地址
    :param obj: 要写入的内容
    :return:
    """
    with open(file, 'w', encoding='utf-8') as f:
        f.write(json.dumps(obj))
···

你可能感兴趣的:(Day13_作业_学生管理系统(加入文件和模块))