2018-09-01-day10作業(yè)-學(xué)生信息管理系統(tǒng)

1.主界面模塊

"""__author__=wuliang"""
from time import sleep
import logout
import login
import student
import file_operation
'''
主界面模塊,也是被程序的入口
'''
def start_inter():
    '''
    系統(tǒng)初始化的主界面
    :return:
    '''
    print('='*30)
    print('+',end='')
    print(' '*7,end='')
    print('學(xué)生信息管理系統(tǒng)',end='')
    print(' '*5,end='')
    print('+')

    print('+', end='')
    print(' '*10,end='')
    print('1.登錄',end='')
    print(' '*10,end='')
    print('+')

    print('+', end='')
    print(' ' * 10, end='')
    print('2.注冊', end='')
    print(' ' * 10, end='')
    print('+')

    print('+', end='')
    print(' ' * 10, end='')
    print('3.退出', end='')
    print(' ' * 10, end='')
    print('+')
    print("="*30)

    operation=input('請輸入你的選擇:')
    fun_operation(operation)

def main_operation(username):
    '''
    系統(tǒng)操作主界面
    :return:
    '''
    operation=0 #設(shè)定operation初始值0,什么也不做
    while operation!=6:
        print('=' * 30)
        print('+', end='')
        print(' ' * 7, end='')
        print('學(xué)生信息管理系統(tǒng)', end='')
        print(' ' * 8, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('1.查看學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('2.添加學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('3.修改學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('4.刪除學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('5.查找學(xué)生', end='')
        print(' ' * 10, end='')
        print('+')

        print('+', end='')
        print(' ' * 10, end='')
        print('6.退出', end='')
        print(' ' * 13, end='')
        print('+')
        print("=" * 30)

        operation = input('請輸入你的操作選擇:')
        try:
            if isinstance(int(operation),int):
                operation = int(operation)
                while operation not in range(1,7):
                    operation = input('你輸入的操作不存在,請重新輸入:')
                    if isinstance(operation, int):
                        operation = int(operation)

                operation_select(operation,username)

                sleep(2)
            else:
                print('你輸入的操作必須是(1~6)!!!')
                sleep(2)
        except:
            print('程序異常!!!')
            sleep(2)
        print('返回上一級目錄...')

def operation_select(operation:int,username:str):
    '''

    進(jìn)入操作界面后具體的操作選擇
    :param operation: 操作選擇
    :return:
    '''
    if operation==1:
        student.show_appoint_stus(username)
        sleep(2)
    elif operation==2:
        one_student=student.add_student(username)
        flag=file_operation.append_student(file_operation.get_all_students(),username,one_student)
        print(flag)
        sleep(2)
    elif operation == 3:
        stuname=input('請輸入你要修改的學(xué)生的姓名:')
        print(student.modify_student(stuname,username))
        sleep(2)

    elif operation == 4:
        stuname=input('請輸入你要刪除的學(xué)生的姓名:')
        print(student.del_student(stuname,username))
        sleep(2)
    elif operation == 5:
        stuname=input('請輸入你要查找的學(xué)生的姓名:')
        student.find_student(stuname,username)
        sleep(2)


def fun_operation(operation):
    '''
    操作方式選擇
    :param operation:
    :return:
    '''
    operation = int(operation)
    if operation == 1:
        #進(jìn)入登錄界面
        username=login.fun_login()
        if username:
            main_operation(username)
    elif operation == 2:
        #進(jìn)入注冊界面
        logout.fun_regis()
    elif operation == 3:
        #退出登錄、結(jié)束本程序
        print('謝謝使用!!!')
        exit(0)


if __name__=='__main__':
    while True:
       start_inter()


2.注冊模塊

"""__author__=wuliang"""
import file_operation
teacher={}
username=''
def fun_regis():
    '''
    注冊功能實現(xiàn)
    :return:
    '''

    while True:
        username = input('請輸入你的名字:')
        if not file_operation.check_teacher(file_operation.get_all_teacher(),username):
            while len(username)<3 or len(username)>16:
                username = input('請重新輸入你的名字(3~16位):')
            print('該用戶名可以使用!!!')
            password = input('請輸入你的密碼:')
            pw = input('確認(rèn)密碼:')
            while pw != password:
                print('密碼錯誤,請重新輸入??!')
                password = input('請輸入你的密碼:')
                pw = input('確認(rèn)密碼:')
            teacher['teaname'] = username
            teacher['password'] = pw
            #將數(shù)據(jù)存入文件中
            user_list = file_operation.get_all_teacher()
            print(file_operation.append_teacher(user_list,teacher))
            return
        print('用戶名已注冊!!!')
# def pass_input():
#     '''
#     進(jìn)行密碼確認(rèn)
#     :return:
#     '''
#     pw = ' '
#     password='  '
#
#     while pw==password:
#         password = input('請輸入你的密碼:')
#         pw = input('確認(rèn)密碼:')
#     teacher['teaname'] = username
#     teacher['password'] = pw

3.登錄模塊

"""__author__=wuliang"""
import file_operation
from time import sleep
def fun_login():
    '''

    教師登錄功能的實現(xiàn)
    :return: username
    '''
    username = input('請輸入你的姓名:')
    password = input('請輸入你的密碼:')
    user_list = file_operation.get_all_teacher()
    while True:
        try:
            if user_list.index({'teaname':username,'password':password})>=0:
                print('登錄成功,現(xiàn)在進(jìn)入操作界面...')
                sleep(1)
                return username
        except:
            print('用戶名或密碼錯誤,請重新輸入')
            username = input('請輸入你的姓名:')
            password = input('請輸入你的密碼:')

4.文件操作模塊

"""__author__=wuliang"""
import os
'''
文件操作模塊,進(jìn)行文件內(nèi)容的查詢,以及文件內(nèi)容的更新
'''
try:
    import cPickle as pickle
except ImportError:
    import pickle

filename = 'user.txt'

student_file='student.txt'


def mkuserfile(filename):
    '''
    如果不存在建存儲教師信息的文件并返回教師文件對象,否則返回教師文件對象
    :return:文件對象
    '''
    fb = open(filename,'wb')
    return fb

def get_onlyread(filename):
    if not os.path.exists(filename):
        mkuserfile(filename).close()
    fb2 = open(filename, 'rb')
    return fb2

def check_teacher(user_list,username):
    '''
    檢查該姓名是否存在
    :param username:
    :return:
    '''

    file = get_onlyread(filename)
    if os.path.getsize(filename):
        for teacher in user_list:
            if teacher['teaname']==username:
                file.close()
                return True

    else:
        file.close()
        return False
    file.close()
    return False


def get_all_teacher():
    '''
    獲取所有的教師
    :return:
    '''
    file = get_onlyread(filename)
    if os.path.getsize(filename):
        user_list = pickle.load(file)
        file.close()
        return user_list
    else:
        file.close()
        return []



def append_teacher(teacher_list:list,teacher:dict):
    '''
    添加教師
    :param teacher_list:教師列表
    :param teacher:新教師
    :return:是否成功
    '''
    try:
        file = mkuserfile(filename)
        teacher_list.append(teacher)
        pickle.dump(list(teacher_list),file)
        file.close()
        return '保存成功'
    except:
        return '保存失敗'


def get_all_students():
    '''
    獲取所有學(xué)生
    :param username:
    :return:
    '''
    #1.根據(jù)參數(shù)獲取指定文件的內(nèi)容
    file = get_onlyread(student_file)
    #2.建立學(xué)生總列表
    stu_list = []
    #3.判斷文件是否為空
    if os.path.getsize(student_file):

        stu_list=pickle.load(file)

    #4.關(guān)閉文件
    file.close()
    return stu_list

def append_student(student_list:list,username:str,student:dict):
    '''
    添加指定教師的學(xué)生
    :param student_list:學(xué)生的總列表
    :param username:教師姓名
    :param student:新學(xué)生
    :return:添加成功 or 失敗
    '''
    if student_list==[]:
        file = mkuserfile(student_file)
        pickle.dump([[username,student]],file)
        file.close()
        return '添加成功'
    for one_stu_list in student_list:
        if one_stu_list[0]==username:
            one_stu_list.append(student)
            student_list[student_list.index(one_stu_list)]=one_stu_list
            file = mkuserfile(student_file)
            pickle.dump(student_list,file)
            file.close()
            return "添加成功"

    file = mkuserfile(student_file)
    student_list.append([username, student])
    pickle.dump((student_list), file)
    file.close()
    return '添加成功'

def get_appoint_stu(username):
    '''
    獲取指定教師的學(xué)生
    :param username: 教師姓名
    :return: stu_appoint_list

    '''
    stu_appoint_list=[]
    all_list = get_all_students()
    for s_l in all_list:
        if s_l[0]==username:
            stu_appoint_list=s_l
            break
    return stu_appoint_list

def check_student(username:str,stuname:str):
    '''
    檢查是否該教師下是否已經(jīng)有了那個學(xué)生
    :param stuname: 學(xué)生姓名
    :param username: 教師姓名
    :return:False:名字已存在,True:可以注冊
    '''
    stu_list=get_appoint_stu(username)
    if stu_list:
        for stu in stu_list[1:]:
            if stu['stuname']==stuname:
                return False
    return True

def modify_student(student_list:list):
    '''
    刪除學(xué)生并更新表
    :param student_list: 已經(jīng)刪除了學(xué)生的的表
    :return:True or False
    '''
    try:
        file = mkuserfile(student_file)
        pickle.dump(student_list, file)
        file.close()
        return True
    except:
        return False

def get_onestudnet(stuname:str,username:str):
    '''
    獲取指定學(xué)生姓名的學(xué)生
    :param stuname:
    :param username:
    :return:
    '''
    appoint_stu_list=get_appoint_stu(username)
    for stu in appoint_stu_list[1:]:
        if stu['stuname']==stuname:
            return stu
    return None

5.學(xué)生模塊

"""__author__=wuliang"""
import file_operation

def add_student(username):
    '''
    添加學(xué)生,獲取學(xué)生的輸入
    :return:
    '''
    stuname = input('請輸入你要添加的學(xué)生的姓名:')
    while not file_operation.check_student(username,stuname):
        print('對不起輸入的學(xué)生的姓名存在!!!')
        stuname = input('請重新輸入學(xué)生的姓名:')
    print('該姓名可以注冊!!!')
    stuid = input('請輸入你要添加的學(xué)號:')
    phone_num = input('請輸入你要添加的電話:')
    age = input('請輸入你要添加的年齡')
    student={'stuname':stuname,'stuid':stuid,'phone_num':phone_num,'age':age}
    return student


def show_appoint_stus(username:str):
    '''
    查看當(dāng)前教師所有學(xué)生信息
    :param username:
    :return:
    '''
    stu_list=file_operation.get_appoint_stu(username)
    for student_ in stu_list[1:]:
        print('stuname:'+student_['stuname']+' stuid:'+student_['stuid'],end=' ')
        print('phone_num:' + student_['phone_num'] + ' age:' + student_['age'])

def modify_student(stuname:str,username:str):
    '''

    :param stuname:
    :param username:
    :return:
    '''
    stu_appoint_list = file_operation.get_appoint_stu(username)
    for stu in stu_appoint_list[1:]:
        if stu['stuname']==stuname:
            print('你要修改的學(xué)生存在!!')
            new_stuname=input('請輸入新的姓名:')
            while not file_operation.check_student(username,new_stuname):
                print('你輸入的名字已存在!!!')
                new_stuname=input('請重新輸入姓名:')
            new_stuid = input('請輸入新的學(xué)號:')
            new_phone_num = input('請輸入新的電話號碼:')
            new_age = input('請輸入新的年齡:')
            student_={'stuname':new_stuname,'stuid':new_stuid,'phone_num':new_phone_num,'age':new_age}
            all_stu_list=file_operation.get_all_students()
            all_stu_list.remove(stu_appoint_list)

            stu_appoint_list.remove(stu)
            stu_appoint_list.append(student_)

            all_stu_list.append(stu_appoint_list)

            file_operation.modify_student(all_stu_list)
            return '修改成功'

def del_student(stuname:str,username:str):
    '''

    :param stuname:
    :param username:
    :return:
    '''
    stu_all_list=file_operation.get_all_students()

    stu_appoint_list=file_operation.get_appoint_stu(username)
    stu_all_list.remove(stu_appoint_list)
    for stu in stu_appoint_list[1:]:
        if stu['stuname']==stuname:
            stu_appoint_list.remove(stu)
            stu_all_list.append(stu_appoint_list)
            file_operation.dmodify_student(stu_all_list)
            return '刪除成功'
            break
    print('你要刪除的學(xué)生不存在')
    return '刪除失敗!!'

def find_student(stuname:str,username:str):
    flag=file_operation.get_onestudnet(stuname,username)
    if flag:
        print('stuname:'+flag['stuname']+' stuid:'+flag['stuid'],end=' ')
        print('stuname:'+flag['phone_num']+' age:'+flag['age'])
    else:
        print('對不起,你輸入的學(xué)生不存在')

6.文件設(shè)計

user.txt:存儲教師信息文件
存儲格式:
  [{'teaname':教師名,'password':登錄密碼}]

student.txt:存儲學(xué)生信息文件
存儲格式:
  [[教師名,{'stuname':學(xué)生名,'stuid':學(xué)號,'phone_num':學(xué)生電話號碼,'age':年齡},...],...]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,039評論 25 709
  • 非暴力溝通的第二要素是感受。體會和表達(dá)感受是件很重要的事情。在生活中,感受往往與看法相近,體會和表達(dá)感受并...
    紅芝_d3fa閱讀 8,244評論 0 0
  • 一個語言表達(dá)不是很好的人,怎么才能把想表達(dá)的說出來呢,每次想說出口的時候,停住了,猶豫了,也不知道為什么,也許自己...
    那一夏舊時光閱讀 193評論 0 0
  • 1、出名要趁早,還是大器晚成 遙想公瑾當(dāng)年,小喬初嫁了,雄姿英發(fā),羽扇綸巾。自古以來,國人都偏愛少年英雄。這也難怪...
    江左不歸人閱讀 382評論 0 1

友情鏈接更多精彩內(nèi)容