2018-10-16day13學生管理系統(tǒng)(文件、模塊、函數(shù)等操作)

簡單學生管理系統(tǒng)實現(xiàn)

重點:了解項目開發(fā)大概過程,以及項目如何逐步實現(xiàn)

  • 1、畫思維導圖,分功能模塊畫思維導圖
  • 2、數(shù)據(jù)結(jié)構(gòu)設計,根據(jù)實際情況設計相應的數(shù)據(jù)結(jié)構(gòu)
  • 3、分功能或模塊逐個實現(xiàn)相應功能

具體實現(xiàn)如下:

main_page.py
# 導入不在相同目錄下的導入模塊
import module.file_manager as file_manager
import module.student_manager as student_manager


# 注冊
def register():
    """
    注冊功能
    :return:
    """
    # 控制輸入賬號
    while True:
        user_name = input('請輸入賬號3~6位:')
        # 檢查賬號密碼位數(shù)是否符合要求
        if not 3 <= len(user_name) <= 6:
            print('賬號有誤,請重新輸入!')
            continue
        else:
            break

        # 控制輸入密碼
    while True:
    # 在賬號輸入成功的前提下輸入密碼
        password = input('請輸入密碼(6~12位):')
        if not 6 <= len(password) <= 12:
            print('密碼有誤,請重新輸入!')
            continue
        else:
            break

#   檢查賬號是否已經(jīng)注冊過
    """
    數(shù)據(jù)設計:用一個字典來保存所有賬號和密碼{賬號:密碼},
              將所有的賬號密碼存到文件中
              選用json文件
    """
#   獲取注冊過的所有賬號信息
    all_user = file_manager.json_read('all_user.json')

#   判斷輸入賬號是否存在
    if user_name in all_user:
        print('該用戶名已經(jīng)被注冊!')
        return
#   賬號可用,將賬號信息添加到所有用戶里面

    all_user[user_name] = password

#   更新本地文件
    re = file_manager.json_write('all_user.json', all_user)

    if re:
        print('注冊成功!')
    else:
        print('注冊失敗!')


# 登錄
def login():
    user_name = input('請輸入賬號:')
    password = input('請輸入密碼:')

#   檢查賬號是否注冊
#   獲取所有賬號信息
    all_user = file_manager.json_read('all_user.json')

    if user_name not in all_user:
        print('登錄失敗,賬號不存在!')
        return
    # 賬號存在先通過賬號去獲取正確的密碼
    password_old = all_user[user_name]

    if password == password_old:
        print('登錄成功!')
        student_manager.show_manager_page(user_name)
#       登錄成功以后的操作寫在這里
    else:
        print('密碼錯誤,登錄失敗!')
        return


def show_main_page():
    while True:
        print(file_manager.text_read_file('main_page.txt'))
        value = input('請輸入你的選擇(1-3):')
    #   根據(jù)不同的選擇做出不同的操作
        if value == '1':
            login()
        elif value == '2':
            register()
        elif value == '3':
            return
        else:
            print('輸入有誤')


if __name__ == '__main__':
    # 顯示登錄頁面
    show_main_page()
student_page.py
import module.file_manager as file_manager

# 這個全局變量用來保存登錄成功的用戶名
current_user = ''

# 添加學生
"""
1、賬號和賬號管理的學生要一一對應
2、一個賬號要管理多個學生
3、一個學生要存儲多個信息,每個學生存儲的信息數(shù)量一樣
方案一:整個系統(tǒng)管理的所有的賬號管理的所有學生放在一起
{
    賬號1:[{'name': name, 'age':age, 'tel':tell, 'id':id}, ....]
    賬號2:[學生1,學生2]
    賬號3:[]....
    ......
}
將一個大字典寫到一個json文件中
方案二:
1、一個賬號對應一個json文件
2、json文件名和賬號名一一對應
{
    count: num,
    all_student: [{'name': name, 'age':age, 'tel':tell, 'id':id},......]
}
"""

# 通過全局變量保存常用key值(開發(fā)常用)
key_count = 'count'
key_all_students = 'all_student'
key_name = 'name'
key_age = 'age'
key_tel = 'tel'
key_id = 'id'



# 獲取獲取當前賬號對應的json文件中的內(nèi)容
def get_current_userinfo():
    """
    獲取當前賬號對應的json文件中的內(nèi)容
    :return:
    """
    content = file_manager.json_read(current_user + '.json')
    if not content:
        content = {}
    return content


# 格式化打印學生信息
def show_student_info(stu):
    print('     %s        %s          %s            %s   ' % (stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))
    # print('學號:%s 姓名:%s 年齡:%s 電話:%s'\
    #       % (stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))


def show_head():
    print('  |    學號    |    姓名    |     年齡    |     電話   |')


# 按姓名查找學生信息
def find_student_with_name(name, all_students):
    """
    按姓名查找
    :param name:
    :param all_students:
    :return:
    """
    find_students = []
    for stu in all_students:
        if stu[key_name] == name:
            find_students.append(stu)
    return find_students


# 按學生學號查找學生
def find_student_with_id(stu_id, all_students):
    """
    按學號查找
    :param stu_id:
    :param all_students:
    :return:
    """
    for stu in all_students:
        if stu[key_id] == stu_id:
            return stu
    return None


# 添加學生
def add_student():
    # 獲取當前賬號對應的數(shù)據(jù)
    user_info = get_current_userinfo()
    # 輸入信息
    while True:
        name = input('請輸入姓名:')
        age = input('請輸入學生年齡:')
        tel = input('請輸入學生電話:')

        # 產(chǎn)生學號
        number = user_info.get(key_count, 0)
        number += 1
        stu_id = 'stu' + str(number).rjust(3, '0')

        stu = {key_name:name, key_age:age, key_tel:tel, key_id:stu_id}

        all_students = user_info.get(key_all_students, [])
        all_students.append(stu)

        # 將數(shù)據(jù)保存在本地
        user_info[key_count] = number
        user_info[key_all_students] = all_students

        re = file_manager.json_write(current_user + '.json', user_info)

        if re:
            print('添加成功!')
        else:
            print('添加失敗!')

        print('1. 繼續(xù)')
        print('2. 返回上層')
        value = input('請選擇:')
        if value == '1':
            continue
        else:
            return


# 修改信息
def change_student():
    """
    修改學生
    :return:
    """
    # 拿到所有學生信息
    user_info = get_current_userinfo()
    all_students = user_info.get(key_all_students, [])
    if not all_students:
        print('沒有可管理的學生!')
        return
    # 用于標記需要修改的學生下標
    index = 1
    show_head()
    for stu in all_students:
        print(index, end=' ')
        show_student_info(stu)
        index += 1

    while True:
        # 給出選擇
        print('1. 修改姓名\n2. 修改年齡\n3. 修改電話\n4. 修改所有信息\n5. 返回上層')
        # 記錄選擇值,做出不同的操作
        value = input('請輸入你的選擇:')

        if value in '1234':
            change_stu_id = input('請輸入需要修改的學生標號(如上所示):')
            # change_stu_index = find_student_return_index(change_stu_id, all_students)
            # if not str(change_stu_index).isalnum():
            #     print('該學號不存在,請核對!')
            #     continue
            if not int(change_stu_id) <= len(all_students):
                print('輸入有誤,請從新輸入!')
            if value == '1':
                new_name = input('請輸入新名字:')
                all_students[int(change_stu_id) - 1][key_name] = new_name
                # 更新本地文件
                re = file_manager.json_write(current_user + '.json', user_info)
                if re:
                    print('修改成功!')
                    continue
                else:
                    print('修改失敗!')
                    continue
            elif value == '2':
                # 獲取新的年齡值
                new_age = input('請輸入新年齡:')
                # 更改年齡值
                all_students[int(change_stu_id) - 1][key_age] = new_age
                # 更新本地文件數(shù)據(jù)
                re = file_manager.json_write(current_user + '.json', user_info)
                if re:
                    print('修改成功!')
                    continue
                else:
                    print('修改失??!')
                    continue

            elif value == '3':
                # 獲取新的電話號碼
                new_tel = input('請輸入新電話號碼:')
                # 更改數(shù)據(jù)
                all_students[int(change_stu_id) - 1][key_tel] = new_tel
                # 更新本地文件
                re = file_manager.json_write(current_user + '.json', user_info)
                if re:
                    print('修改成功!')
                    continue
                else:
                    print('修改失?。?)
                    continue
            elif value == '4':
                # 獲取所有信息
                new_name = input('請輸入新姓名:')
                new_age = input('請輸入新年齡:')
                new_tel = input('請輸入新電話:')
                # 更該數(shù)據(jù)
                all_students[int(change_stu_id) - 1][key_name] = new_name
                all_students[int(change_stu_id) - 1][key_age] = new_age
                all_students[int(change_stu_id) - 1][key_tel] = new_tel
                # 更新本地文件數(shù)據(jù)
                re = file_manager.json_write(current_user + '.json', user_info)
                if re:
                    print('修改成功!')
                    continue
                else:
                    print('修改失??!')
                    continue
        elif value == '5':
            break
        else:
            print('輸入錯誤,請重新輸入!')
            continue


# 刪除學生信息
def delete_student():
    """
    刪除學生
    :return:
    """
#   拿到所有的學生
    user_info = get_current_userinfo()
    all_sutdents = user_info.get(key_all_students, [])

    if not all_sutdents:
        print('當前賬號沒有可管理的學生')
        return
#   給出選擇
    while True:
        print('1. 按姓名刪除\n2. 按學號刪除\n3. 返回')
        value = input('請輸入選擇(1~3):')
        if value == '1':
            # print('按姓名刪除')
            name = input('請輸入姓名:')
            students = find_student_with_name(name, all_sutdents)
        #   判斷是否有同名的學生
            if not students:
                print('該學生不存在!')
                continue
#       如果找到了學生,先展示找到的學生
            index = 0
            show_head()
            for stu in students:
                print(index,end=' ')
                show_student_info(stu)
                index += 1
            # 選擇需要刪除的學生的下標
            print('q. 返回上層')
            del_index = input('請輸入需要刪除的學生對應的標號:')
            if del_index == 'q':
                continue
            else:
                # 找到要刪除的學生對應的字典
                del_stu = students[int(del_index)]
            #     從所有的學生對應的列表中刪除
                all_sutdents.remove(del_stu)

                re = file_manager.json_write(current_user + '.json', user_info)

                if re:
                    print('刪除成功!')
                else:
                    print('刪除失敗!')

        elif value == '2':
            del_id = input('請輸入學號:')
        #    根據(jù)學號查找學生
            del_stu = find_student_with_id(del_id, all_sutdents)

            if not del_id:
                print('沒有改學生!')
                continue
            show_head()
            show_student_info(del_stu)
            value = input('Y/N')
            if value == 'Y' or value == 'y':
                all_sutdents.remove(del_stu)
            else:
                continue
        #     刪除后更新信息
            re = file_manager.json_write(current_user + '.json', user_info)

            if re:
                print('刪除成功!')
            else:
                print('刪除失??!')

        else:
            return


# 查詢學生信息
def find_student():
    user_info = get_current_userinfo()
    all_sutdents = user_info.get(key_all_students, None)
    if not all_sutdents:
        print('當前賬號沒有可管理的學生')
        return
        # 給出選擇
    while True:
        print('1. 查看所有學生\n2. 按姓名查找\n3. 按學號查找\n4. 返回')
        value = input('請選擇(1~4):')

        if value == '1':
            # 打印表頭
            show_head()
            for stu in all_sutdents:
                show_student_info(stu)
        elif value == '2':
            name = input('請輸入姓名:')
            students = find_student_with_name(name, all_sutdents)
            if students:
                show_head()
                for stu in students:
                    show_student_info(stu)
            else:
                print('學生%s不存在!' % name)
        elif value == '3':
            stu_id = input('請輸入學號:')
            stu = find_student_with_id(stu_id, all_sutdents)
            if stu:
                show_head()
                show_student_info(stu)
            else:
                print('沒有找到%s對應的學生!' % stu_id)
        else:
            return


# 打印學生信息
def show_manager_page(user_name):
    # 保存登錄成功的用戶名
    global current_user
    current_user = user_name

    # 顯示管理界面
    while True:
        content = file_manager.text_read_file('manager_page.txt')
        print(content % user_name)

        # 做出選擇
        value = input('請選擇(1~5):')
        if value == '5':
            return
        elif value == '1':
            add_student()
        elif value == '2':
            find_student()
        elif value == '3':
            change_student()
        elif value == '4':
            delete_student()
        else:
            print('輸入有誤,請從新輸入!')
            continue


if __name__ == '__main__':
    pass
file_manager.py
import json


def text_read_file(file_name:str):
    """
    普通文本讀操作
    :param   file_name 文件路徑
    :return: 讀到的內(nèi)容
    """
    try:
        with open('./files/' + file_name, encoding='utf-8') as f:
            return f.read()

    except FileNotFoundError:
        print('%s文件不存在' % file_name)

        return


def text_write(file_name:str, content:str):
    """
       普通文本寫操作
       :param   file_name 文件路徑
       :param   content  需要寫入的內(nèi)容
       :return: 寫入的結(jié)果
       """
    try:
        with open('./files/' + file_name, 'w', encoding='utf-8') as f:
            f.write(content)
            return True
    except:
        print('寫入失敗!')


def json_read(file_name:str):
    """
    獲取json文件內(nèi)容
    :param file_name 文件名
    :return: 讀到的內(nèi)容
    """
    try:
        with open('./files/' + file_name, encoding='utf-8') as f:
            return json.load(f)
    except FileNotFoundError:
        print('%s文件不存在' % file_name)
        return


def json_write(file_name, content):
    """
    json文件寫操作
    :param file_name 文件名
    :param content:  寫入內(nèi)容
    :return:   True/False 成功/失敗
    """
    try:
        with open('./files/' + file_name, 'w', encoding='utf-8') as f:
            json.dump(content, f)
            return True
    except:
        # print('寫入失敗')
        return False


if __name__ == '__main__':
    pass
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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