学生管理系统

"""
__author__ = 叶兵
"""
import time
import json


def student_system(account_name):

    def number():
        all_student_list = read_file()
        numb = len(all_student_list['student']) + 1
        while True:
            yield "python1902" + str(numb).rjust(3, '0')
            numb += 1

    gen = number()

    def read_file():
        with open('student.json', encoding='utf-8') as f:
            all_student_temp = json.loads(f.read(), encoding='utf-8')
            return all_student_temp

    def write_file(all_student_list):
        with open('student.json', 'w', encoding='utf-8') as f:
            temp = json.dumps(all_student_list)
            f.write(temp)

    first_temp = True
    while first_temp:
        def welcome():
            global num
            print('================================')
            print('欢迎%s:' % account_name)
            print('     1.  添加学生')
            print('     2.  查看学生')
            print('     3.  修改学生信息')
            print('     4.  删除学生')
            print('     5.  返回(退出登录)')
            print('================================')
            num = input('请选择(1-5):')
            return manage_system(num)

        def manage_system(num):
            if num == '1':
                add_temp = True
                while add_temp:
                    all_student = []
                    name1 = input('请输入学生姓名:')
                    age1 = int(input('请输入学生年龄:'))
                    tel1 = input('请输入学生电话:')

                    def add_student(name: str, age: int, tel: str):
                        all_student.append({'id': next(gen), 'name': name, 'age': age, 'tel': tel})
                        print('添加成功!')

                        # return all_student

                    add_student(name1, age1, tel1)
                    # 先读文件提出以前的数据,再添加新数据后,保存所有数据
                    all_student_temp = read_file()
                    all_student_temp["student"].append(all_student[0])
                    write_file(all_student_temp)

                    print('1. 继续')
                    print('2. 返回')
                    add_num = input('请选择(1-2):')
                    if add_num == '1':
                        continue
                    else:
                        add_temp = False
                        welcome()
            elif num == '2':
                find_temp = True
                while find_temp:
                    print('1. 查看所有学生')
                    print('2. 按姓名查找')
                    print('3. 按学号查找')
                    print('4. 返回')
                    find_num = input('请选择(1-4):')
                    if find_num == '1':
                        all_student_temp = read_file()
                        if len(all_student_temp["student"]) == 0:
                            print('数据库里面还没有存储学生')
                        else:
                            for stu in all_student_temp["student"]:
                                print(stu)
                            else:
                                print('1. 返回主页面:')
                                print('按任意键返回上一级页面:')
                                find_temp_num = input('请选择:')
                            if find_temp_num == '1':
                                find_temp =False
                                welcome()
                    elif find_num == '2':
                        find_num2_temp = True
                        while find_num2_temp:
                            student_name = input('请输入学生姓名:')
                            find_name_student = []
                            all_student_temp = read_file()
                            for index in range(len(all_student_temp["student"])):
                                if all_student_temp["student"][index]['name'] == student_name:
                                    find_name_student.append(all_student_temp["student"][index])
                            if len(find_name_student) == 0:
                                print('没有找到该学生信息')
                                print('1. 返回主页面:')
                                print('按任意键返回上一级页面:')
                                find_num3 = input('请选择:')
                                if find_num3 == '1':
                                    find_num2_temp = find_temp = False
                                    welcome()
                                else:
                                    continue
                            else:
                                for stu in find_name_student:
                                    print(stu)
                                find_num2_temp = False
                                print('1. 返回主页面:')
                                print('按任意键返回上一级页面:')
                                # find_temp_num = input('请选择:')
                            if input('请选择:') == '1':
                                find_temp = False
                                welcome()
                    elif find_num == '3':
                        id_temp = True
                        while id_temp:
                            find_id = input('请输入学生学号:')
                            all_student_temp = read_file()
                            for index in range(len(all_student_temp["student"])):
                                if all_student_temp["student"][index]['id'] == find_id:
                                    print(all_student_temp["student"][index])
                                    id_temp = False
                                    break
                            else:
                                print('没有找到该学生信息')
                    else:
                        welcome()
            elif num == '3':
                print('1. 修改名字:')
                print('2. 修改年龄:')
                print('3. 修改电话:')
                print('4. 返回')
                modify_num = input('请选择(1-3):')
                modify_temp = True
                while modify_temp:
                    your_id = input('请输入你的学号:')
                    all_student_temp = read_file()
                    if modify_num == '1':
                        for index in range(len(all_student_temp["student"])):
                            if all_student_temp["student"][index]['id'] == your_id:
                                print('您要修改的学生信息为', all_student_temp["student"][index])
                                modify_name = input('请输入你要修改成的名字:')
                                all_student_temp["student"][index]['name'] = modify_name
                                print('修改之后为:', all_student_temp["student"][index])
                                write_file(all_student_temp)
                                modify_temp = False
                                break
                        else:
                            print('抱歉!没有找到和您对应学号的学生,请检查您输入的学号是否正确。')
                            print('1. 回到主页面查询学号信息')
                            print('2. 继续操作,重新输入学号')
                            modify_num2 = input('请选择(1-2):')
                            if modify_num2 == '1':
                                welcome()
                    elif modify_num == '2':
                        for index in range(len(all_student_temp["student"])):
                            if all_student_temp["student"][index]['id'] == your_id:
                                print('您要修改的学生信息为', all_student_temp["student"][index])
                                modify_age = input('请输入你要修改成的年龄:')
                                all_student_temp["student"][index]['age'] = modify_age
                                print('修改之后为:', all_student_temp["student"][index])
                                write_file(all_student_temp)
                                modify_temp = False
                                break
                        else:
                            print('抱歉!没有找到和您对应学号的学生,请检查您输入的学号是否正确。')
                            print('1. 回到主页面查询学号信息')
                            print('2. 继续操作,重新输入学号')
                            modify_num2 = input('请选择(1-2):')
                            if modify_num2 == '1':
                                welcome()
                    elif modify_num == '3':
                        for index in range(len(all_student_temp["student"])):
                            if all_student_temp["student"][index]['id'] == your_id:
                                print('您要修改的学生信息为', all_student_temp["student"][index])
                                modify_tel = input('请输入你要修改成的电话:')
                                all_student_temp["student"][index]['tel'] = modify_tel
                                print('修改之后为:', all_student_temp["student"][index])
                                write_file(all_student_temp)
                                modify_temp = False
                                break
                        else:
                            print('抱歉!没有找到和您对应学号的学生,请检查您输入的学号是否正确。')
                            print('1. 回到主页面查询学号信息')
                            print('2. 继续操作,重新输入学号')
                            modify_num2 = input('请选择(1-2):')
                            if modify_num2 == '1':
                                welcome()
                    else:
                        welcome()
                        break
            elif num == '4':
                remove_temp = True
                all_student_temp = read_file()
                while remove_temp:
                    print('请慎重考虑要删除的学生:')
                    remove_id = input('请输入你要删除学生的学号:')
                    for index in range(len(all_student_temp["student"])):
                        if all_student_temp["student"][index]['id'] == remove_id:
                            print('你要删除的学生信息如下,请仔细核对后再删除!')
                            print(all_student_temp["student"][index])
                            print('1. 确认删除')
                            print('2. 返回(或任意键返回)')
                            confirm_num = input('请选择(1-2):')
                            if confirm_num == '1':
                                del all_student_temp["student"][index]
                                print('删除成功')
                                write_file(all_student_temp)
                                time.sleep(1)
                                remove_temp = False
                                break
                            else:
                                remove_temp = False
                                break
                    else:
                        print('没有找到该学生信息!请重新输入,或者返回主页面进行学号查询。')
                        print('1. 重新输入学号')
                        print('2. 返回主页面进行查询')
                        remove_num = input('请选择(1-2):')
                        if remove_num == '1':
                            continue
                        else:
                            remove_temp = False
                            welcome()
            else:
                regist()

        welcome()


def regist():
    while True:
        def true_regist(account):
            with open('account.json', 'w') as f:
                f.write(json.dumps(account))
                print('注册成功')
                time.sleep(1)

        def enter(account_name):
            student_system(account_name)

        print('====================')
        print('欢迎使用学生管理系统')
        print('====================')
        print('1. 注册')
        print('2. 登录')
        print('====================')
        regist_num = input('请选择(1-2):')
        if regist_num == '1':
            account_number = input('请输入账号:')
            with open('account.json', encoding='utf-8') as f:
                account = json.loads(f.read(), encoding='utf-8')
                if account == {}:
                    password = input('请输入密码:')
                    account[account_number] = password
                    true_regist(account)

                for key in account:
                    if key == account_number:
                        print('该账号已经被注册,注册失败')
                        print('====================')
                        time.sleep(1)
                        break
                else:
                    password = input('请输入密码:')
                    account[account_number] = password
                    true_regist(account)

        else:
            with open('account.json', encoding='utf-8') as f:
                account_temp = json.loads(f.read(), encoding='utf-8')
                your_account = input('请输入账号:')
                if your_account not in account_temp.keys():
                    print('您输入的账号没有注册,1秒后回到主页面')
                    print('====================')
                    time.sleep(1)
                else:
                    temp_pass = True
                    while temp_pass:
                        your_password = input('请输入密码:')
                        if account_temp[your_account] == your_password:
                            print('登录成功,请稍后...')
                            time.sleep(1)
                            temp_pass = False
                            enter(your_account)
                        else:
                            print('您输入的密码错误!!!!!')


regist()

你可能感兴趣的:(学生管理系统)