2018-09-05-学生管理系统

1,主页面函数(01-mainpage.py)

import json
import file_manager
import student_system
''' '''
# 全局变量
file_name='user_info.json'

key_user_name='user_name'
key_password='password'

#=====================注册=======================
'''
为了下次打开系统的时候能够正常登录,注册成功的信息需要保存。保存用户名和密码
一个系统可以注册多个用户,可以用列表保存多个用户,通过字典来保存每个用户的用户名和密码
[
    {'user_name':用户名,'password':密码}
]
保存到user_info.json中
'''
#
def is_register(username):
    all_user = file_manager.read_json_file(file_name)
    if not all_user:
        return False
    for user in all_user:
        if user[key_user_name]==username:
            return True
        return False




def get_all_user():

    if not file_manager.read_json_file(file_name):
        return []
    return file_manager.read_json_file(file_name)





def register():
    while True:
        user_name=input('请输入用户名(3~10位)')
        # 判断是否符合格式要求
        if not 3<=len(user_name)<=10:
            print('重新输入')
            continue
        # 判断是否被注册过
        if is_register(user_name):
            print('用户名%s已被注册,请重新输入' % (user_name))
            continue
        print('用户名可用')
        break

    # 2密码

    while True:
        password = input('请输入密码:')
        if not 6 <= len(password) <= 16:
            print('密码输入有误')
            continue
        re_password = input('确认密码')
        if password != re_password:
            print('密码不同,请重新输入')
            continue
        break

    all_user=get_all_user()
    all_user.append({key_user_name:user_name,key_password:password})
    file_manager.write_json_file(all_user,file_name)

    print('zhucechenggong')

#============================登录=========================
def loging():
    user_name=input('请输入用户名')
    password=input('请输入密码')
#     用户名是否注册
    all_user=get_all_user()
    for user in all_user:
        if user[key_user_name]==user_name:
            if user[key_password]==password:
                print('登陆成功')
                return user_name
            print('密码错误')
            return None
        print('用户名未注册')
        return None



#=====================主页=======================
def show_main_page():
    while True:
        print(file_manager.read_text_file('loging.txt'))
        value=int(input('请选择(1~3):'))


        #1,退出
        if value==3:
            break
        #2,注册
        elif value==2:
            register()

        #3,登录
        elif value==1:
            user_name=loging()
            if user_name:
                student_system.user_name=user_name
                student_system.main_page()






        else:
            print('重新选择')




if __name__ == '__main__':
    show_main_page()

2,学生系统函数(student_system.py)

import file_manager

user_name=''





#====================ADD===========
'''
一个账号对应管理不同的学生-----不同用户对应不同json文件
json格式
{'name':'','number':个数,
'all_students':[
                {'name':  ,'age':  ,'tel'  ,}
                ]
}

'''
key_number='number'
key_all_students='all_students'
key_name='name'
key_age='age'
key_tel='tel'
key_id='id'


def get_system_info():
    system_info=file_manager.read_json_file(user_name+'.json')
    if system_info:
        return system_info
    return {}


def creat_id():
    system_info=get_system_info()
    number=system_info.get(key_number,0)
    number+=1
    id='stu'+str(number).rjust(4,'0')
    return id,number
    #更新系统中的数据


def add_student():
    while True:
        name=input('姓名')
        age=input('年龄')
        tel=input('电话')

        #id
        id,number=creat_id()

        #3创建学生
        student={key_name:name,key_age:age,key_tel:tel,key_id:id}

        #4保存学生信息
        system_info=get_system_info()
        all_student=system_info.get(key_all_students,[])
        all_student.append(student)

        #5保存
        system_info[key_all_students]=all_student
        system_info[key_number]=number
        re=file_manager.write_json_file(system_info,user_name+'.json')

        if re:
            print('添加成功')
        else:
            print('添加失败')
        print('1,继续添加','2,返回')
        x=input('选择')
        if x=='1':
            continue
        elif x=='2':
            break

# ====================查找学生===================
def find_student():
    while True:
        print('1,查看所有学生')
        print('2,根据姓名查找学生')
        print('3,根据学号查找学生')


        system_info=get_system_info()
        all_students=system_info[key_all_students]
        if not all_students:
            print('no stu')
            return

        value = input('请选择:')

        if value=='1':
            for student in all_students:
                print('学号:%s,姓名:%s,年龄:%s,电话:%s'\
                      %(student[key_id],student[key_name],student[key_age],student[key_tel]))

        if value=='2':
            name = input('姓名')
            for student in all_students:
                if name==student[key_name]:
                    print('学号:%s,姓名:%s,年龄:%s,电话:%s' \
                          % (student[key_id], student[key_name], student[key_age], student[key_tel]))


        if value=='3':
            id = input('学号')
            for student in all_students:
                if id==student[key_id]:
                    print('学号:%s,姓名:%s,年龄:%s,电话:%s' \
                          % (student[key_id], student[key_name], student[key_age], student[key_tel]))

        print('1,继续', '2,返回')
        x = input('选择')
        if x == '1':
            continue
        elif x == '2':
            break

#     ===========================删除学生===========================
def delete():
    while True:
        print('1,根据姓名删除学生')
        print('2,根据学号删除学生')
        wait_dele=[]

        system_info=get_system_info()
        all_student=system_info.get(key_all_students,[])
        value=input('请选择:')
        if value=='1':
            name = input('姓名')
            count=0
            for stu in all_student[:]:

                if stu[key_name]==name:
                    count += 1
                    wait_dele.append(stu)
                    print('%d,学号:%s,姓名:%s,年龄:%s,电话:%s' \
                          % (count,stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))

            option=int(input('请选择删除项:'))
            all_student.remove(wait_dele[option-1])
            system_info[key_all_students]=all_student
            file_manager.write_json_file(system_info,user_name+'.json')

        if value == '2':
            id = input('学号')
            for stu in all_student[:]:

                if stu[key_id] == id:

                    print('学号:%s,姓名:%s,年龄:%s,电话:%s' \
                          % (stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))
                    dele_stu=stu
            option = input('是否删除:1,是;2,否')
            if option==1:
                all_student.remove(dele_stu)

            system_info[key_all_students] = all_student
            file_manager.write_json_file(system_info, user_name + '.json')

        print('1,继续', '2,返回')
        x = input('选择')
        if x == '1':
            continue
        elif x == '2':
            break

# ==========================修改=======================================
def revise():
    while True:
        print('1,根据姓名修改学生')
        print('2,根据学号修改学生')

        wait_revise=[]

        system_info = get_system_info()
        all_student = system_info.get(key_all_students, [])
        value = input('请选择:')
        if value == '1':
            name = input('姓名')
            count = 0
            index=-1
            for stu in all_student[:]:
                index+=1
                if stu[key_name] == name:
                    count += 1
                    wait_revise.append(index)
                    print('%d,学号:%s,姓名:%s,年龄:%s,电话:%s' \
                          % (count, stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))

            option = int(input('请选择修改项:'))
            index=wait_revise[option-1]
            name = input('姓名')
            age = input('年龄')
            tel = input('电话')
            all_student[index][key_name]=name
            all_student[index][key_age] = age
            all_student[index][key_tel] = tel


            system_info[key_all_students] = all_student
            file_manager.write_json_file(system_info, user_name + '.json')

        if value == '2':
            id = input('学号')
            index1=-1
            for stu in all_student[:]:
                index1+=1
                if stu[key_id] == id:
                    print('学号:%s,姓名:%s,年龄:%s,电话:%s' \
                          % (stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))
                    index = index1
            option = input('是否修改:1,是;2,否')
            if option == 1:
                name = input('姓名')
                age = input('年龄')
                tel = input('电话')
                all_student[index][key_name] = name
                all_student[index][key_age] = age
                all_student[index][key_tel] = tel

            system_info[key_all_students] = all_student
            file_manager.write_json_file(system_info, user_name + '.json')

        print('1,继续', '2,返回')
        x = input('选择')
        if x == '1':
            continue
        elif x == '2':
            break








#====================zhuye================
def main_page():
    while True:
        print(file_manager.read_text_file('system.txt'))
        value = int(input('请选择(1~5):'))

        if value ==5:
            break
        elif value ==1: #添加
            add_student()
        elif value ==2: #查找
            find_student()
        elif value == 3:  # 删除
            delete()
        elif value == 4:  # 修改
            revise()

3,自定义模块函数(file_manager.py)

import json


'''   '''

def read_text_file(file_name):
    '''
    获取文本文件的内容
    :param file_name:文件名
    :return:
    '''
    try:
        with open('./files/'+file_name,encoding='utf-8') as f:
            return f.read()
    except:
        print('文件不存在')
        return None

def write_json_file(content,filename):
    '''
    写入json文件
    :param filename: 文件名
    :param content:写入内容
    :return:
    '''
    try:
        with open('./files/'+filename,'w',encoding='utf-8') as f:
            json.dump(content,f)
            return True
    except:
        return None



def read_json_file(filename):
    '''
    读json文件
    :param filename:
    :return:
    '''
    try:
        with open('./files/'+filename,encoding='utf-8') as f:
            return json.load(f)
    except:
        return None

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