2019-01-12学生管理系统(函数方法)

2019-01-12  本文已影响0人  Little茂茂

main_page.py

import file_operation   # 导入对文件相关操作的模块
import student_system    # 导入学生管理系统


# 写登录函数
def sign_on():
    # 读出所有账号信息保存为all_user
    all_user = file_operation.read_json_file('accounts.json')
    while True:
        account = input('请输入您的账号:')
        password = input('请输入密码:')
        # 如果all_user不存在说明还没有账号,赋空字典
        if not all_user:
            all_user = {}
        # 当前账号在all_user
        if account in all_user:
            # 输入密码
            if all_user[account] == password:
                print('登录成功!')
                # 给学生管理系统的使用者赋值
                student_system.user_name = account
                # 执行学生管理系统
                student_system.student_system()
                return
            else:
                print('账号或密码不正确请重新输入!')
                # 结束循环
                break
        else:
            print('账号或密码有误!')
            # 结束函数
            return


# 注册函数
def sign_in():
    # 取使用账号信息
    all_user = file_operation.read_json_file('accounts.json')
    while True:
        account = input('请输入您的账号(4-6位字符):')
        if 4 > len(account) or len(account) > 6:
            print('账号不符合要求,请重新输入!')
            # 结束循环
            break
        else:
            # 如果满足条件
            if all_user == {}:
                password = input('请输入您的密码(8-12位字符):')
                if 8 > len(password) or len(password) > 12:
                    print('密码不符合要求,请重新输入')
                    break
                else:
                    # 添加字典
                    all_user[account] = password
                    # 存储账号信息
                    result = file_operation.write_json_file(all_user, 'accounts.json')
                    if result:
                        print('恭喜您注册成功!')
                        break
                    else:
                        print('注册失败,请重新注册!')
        if account in all_user:
            print('账号已存在请重新输入!')
            break
        else:
            password = input('请输入您的密码(8-12位字符):')
            if 8 > len(password) or len(password) > 12:
                print('密码不符合要求,请重新输入')
                break
            else:
                all_user[account] = password
                result = file_operation.write_json_file(all_user, 'accounts.json')
                if result:
                    print('恭喜您注册成功!')
                    break
                else:
                    print('注册失败,请重新注册!')


def main_page():
    while True:
        print(file_operation.read_text_file('main_page.txt'))
        input_value = input('请输入(1-3):')
        if input_value == '1':
            sign_on()
        elif input_value == '2':
            sign_in()
        elif input_value == '3':
            break
        else:
            print('输入有误')


def main():
    main_page()


if __name__ == '__main__':
    main()

student_system.py

import file_operation


key_stu_id = 'id'
key_stu_name = 'name'
key_stu_age = 'age'
key_stu_tel = 'tel'
key_all_students = 'all_students'
key_max_number = 'max_number'

user_name = 'admin'


def show_stu_information(item: dict):
    # 显示学生信息
    return '学号:%s 姓名:%s 年龄:%s 电话:%s' % (item[key_stu_id], item[key_stu_name], item[key_stu_age], item[key_stu_tel])


# 添加学生信息
def add_student():
    input_value = input('1.添加学生信息\n2.返回:')
    while True:
        if input_value == '2':
            return
        if input_value == '1':
            # 每个账号对应一个json文件来存储学生数据,读取这个账号的学生数据并保存为student_information
            student_information = file_operation.read_json_file(user_name + '.json')
            # 如果不存在该账号的json文件
            if not student_information:
                student_information = {}
            # 最大学生数,用来编学号
            max_number = student_information.get(key_max_number, 0)
            name = input('请输入学生姓名:')
            age = input('请输入学生年龄:')
            tel = input('请输入学生电话:')
            max_number += 1
            # 编写学生学号
            id1 = 'stu' + str(max_number).rjust(5, '0')
            # 学生信息字典
            stu = {key_stu_name: name, key_stu_id: id1, key_stu_age: age, key_stu_tel: tel}
            # 添加学生到学生列表中
            all_students = student_information.get(key_all_students, [])
            all_students.append(stu)
            student_information[key_all_students] = all_students
            student_information[key_max_number] = max_number
            # 添加学生信息到该账号的学生信息中
            result = file_operation.write_json_file(student_information, user_name+'.json')
            if result:
                print('添加成功')
                input_value1 = input('1.继续添加\n2.返回:')
                if input_value1 != '1':
                    return
            else:
                print('添加失败')
                break


def find_student():
    student_information = file_operation.read_json_file(user_name + '.json')
    if not student_information:
        print('暂无学生请先添加学生!')
        return
    all_students = student_information.get(key_all_students, [])
    while True:
        input_value = input('1.查看所有学生信息\n2.按姓名查找\n3.按学号查找\n4.返回\n请选择(1-4):')
        if input_value == '1':
            # 查看所有学生信息
            for stu in all_students:
                print(stu)
        elif input_value == '2':
            # 按姓名查找
            input_name = input('请输入学生姓名:')
            count = 0
            for item in all_students:
                if input_name == item['name']:
                    print(item)
                    count += 1
                if count == 0:
                    print('无该学生信息!')
                    break

        elif input_value == '3':
            # 按学号查找
            input_num = input('请输入学生学号:')
            for item in all_students:
                if input_num == item['id']:
                    print(item)
                    break
                else:
                    print('无该学生信息!')
                    break
        elif input_value == '4':
            # 返回
            break
        else:
            print('输入有误!')


def change_student():
    student_information = file_operation.read_json_file(user_name + '.json')
    if not student_information:
        print('暂无学生请先添加学生!')
        return
    all_students = student_information.get(key_all_students)
    while True:
        input_value = input('1.按姓名修改\n2.按学号修改\n3.退出\n请输入(1-3):')
        if input_value == '1':
            input_name = input('请输入您要修改的学生姓名:')
            for item in all_students:
                if item[key_stu_name] == input_name:
                    new_age = input('请修改学生年龄:')
                    new_tel = input('请修改学生电话:')
                    item[key_stu_age] = new_age
                    item[key_stu_tel] = new_tel
                    student_information[key_all_students] = all_students
                    result = file_operation.write_json_file(student_information, user_name+'.json')
                    if result:
                        print('修改成功!')
                        return
                    else:
                        print('修改失败!')
                        break
                else:
                    print('无此学生信息!')
        elif input_value == '2':
            input_id = input('请输入您要修改的学生学号:')
            for item in all_students:
                if item[key_stu_id] == input_id:
                    new_age = input('请修改学生年龄:')
                    new_tel = input('请修改学生电话:')
                    item[key_stu_age] = new_age
                    item[key_stu_tel] = new_tel
                    student_information[key_all_students] = all_students
                    result = file_operation.write_json_file(student_information, user_name + '.json')
                    if result:
                        print('修改成功!')
                        return
                    else:
                        print('修改失败!')
                        break
                else:
                    print('无此学生信息!')
        elif input_value == '3':
            # 退出
            break
        else:
            print('输入有误,请重新输入!')


def del_student():
    student_information = file_operation.read_json_file(user_name + '.json')
    if not student_information:
        print('暂无学生请先添加学生!')
        return
    all_students = student_information.get(key_all_students)
    while True:
        input_value = input('1.按姓名删除\n2.按学号删除\n3.退出\n请输入(1-3):')
        if input_value == '1':
            input_name = input('请输入您要修改的学生姓名:')
            del_student1 = []
            for item in all_students:
                if item[key_stu_name] == input_name:
                    del_student1.append(item)
            count = 0
            for _ in del_student1:
                count += 1
                print(str(count) + ' ' + str(del_student1[count-1]))
            input_confirm = input('是否要删除学生 Y/N')
            if input_confirm == 'Y':
                input_num = input('请输入您要删除的学生的标号:')
                all_students.remove(del_student1[int(input_num)-1])
                student_information[key_all_students] = all_students
                result = file_operation.write_json_file(student_information, user_name + '.json')
                if result:
                    print('删除成功!')
                    return
                else:
                    print('删除失败')
                    break
        elif input_value == '2':
            input_id = input('请输入您要删除的学生的学号:')
            for item in all_students:
                if item[key_stu_id] == input_id:
                    all_students.remove(item)
                    student_information[key_all_students] = all_students
                    result = file_operation.write_json_file(student_information, user_name + '.json')
                    if result:
                        print('删除成功!')
                        return
                    else:
                        print('删除失败')
                        break
        elif input_value == '3':
            return
        else:
            print('输入有误!')


def student_system():
    while True:
        print(file_operation.read_text_file('student_system_page.txt') % user_name)
        input_value = input('请选择(1-5):')
        if input_value == '1':
            add_student()
        elif input_value == '2':
            find_student()
        elif input_value == '3':
            change_student()
        elif input_value == '4':
            del_student()
        elif input_value == '5':
            break
        else:
            print('输入有误!')


def main():
    student_system()


if __name__ == '__main__':
    main()

file_operation.py

import json


def read_text_file(file_name: str):
    """
    读取该文本文件内容
    :param file_name:文件名
    :return: 文件内容
    """
    try:
        with open('file/'+file_name, 'r', encoding='utf-8') as f:
            return f.read()
    except(FileNotFoundError, IsADirectoryError):
        print('警告:文件%s不存在!' % file_name)


def write_text_file(content, file_name: str):
    """
    向该文本文件中写入内容
    :param content: 要写入的内容
    :param file_name: 文件名
    :return: 将内容写入文件
    """
    try:
        with open('file/'+file_name, 'w', encoding='utf-8') as f:
            return f.write(content)
    except(FileNotFoundError, IsADirectoryError):
        print('写入失败')


def read_json_file(file_name):
    """
    读json文件内容
    :param file_name: json文件名
    :return:json对应的python数据,文件不存在返回None
    """
    try:
        with open('file/'+file_name, 'r', encoding='utf-8') as f:
            return json.load(f)
    except(FileNotFoundError, IsADirectoryError):
        print('警告,文件%s不存在' % file_name)


def write_json_file(content, file_name):
    """
    将数据写入json文件中
    :param content: 内容
    :param file_name: json文件名
    :return:
    """
    try:
        with open('file/'+file_name, 'w', encoding='utf-8') as f:
            json.dump(content, f)
            return True  # 写入成功
    except(FileNotFoundError, IsADirectoryError):
        print('写入失败')
        return False

上一篇 下一篇

猜你喜欢

热点阅读