Day10—作業(yè)

學生管理系統(tǒng)

"""__author__=Zeng"""
import os
import json


# 登錄界面
def sys_menu():
    print('='*7, '*', '歡迎來到學生管理系統(tǒng)', '*', '='*7)
    print(' 1. 登錄\n',
          '2. 注冊\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        sign_in()
    elif select_num == '2':
        user_register_name()
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        sys_menu()


def sign_in():
    user_name = input('請輸入用戶名(3-10位):')
    user_password = input('請輸入密碼(6-16位):')
    f = open('./Student-management/UsersInformation.json', 'r', encoding='utf8')
    info = json.load(f)
    if not info:
        print('登錄失??!沒有找到該用戶名!請注冊后再登錄\n------')
        sys_menu()
    for i in range(len(info)):
        if info[i]['name'] == user_name:
            if info[i]['password'] == user_password:
                f.close()
                print('登錄成功!\n------')
                interface_menu(user_name)
            else:
                print('登錄失?。∶艽a錯誤!\n------')
                sys_menu()
    else:
        f.close()
        print('登錄失敗!沒有找到該用戶名!請注冊后再登錄\n------')
        sys_menu()


def user_register_name():
    user_name = input('請輸入用戶名(3-10位):')
    f = open('./Student-management/UsersInformation.json', 'r', encoding='utf8')
    info = json.load(f)
    if not info:
        user_register_password(user_name)
    for i in range(len(info)):
        if info[i]['name'] == user_name:
            f.close()
            print('該用戶名已存在!請從新輸入!\n------')
            user_register_name()
    else:
        f.close()
        print('用戶名可用!')
        user_register_password(user_name)


def user_register_password(user_name: str):
    user_password1 = input('請輸入密碼(6-16位):')
    user_password2 = input('請再次輸入密碼:')
    if user_password1 != user_password2:
        print('兩次密碼輸入不一致!請從新輸入!\n------')
        user_register_password(user_name)
    else:
        # 注冊成功,并在用戶信息文檔中,添加用戶信息
        list1 = []
        dict1 = {'name':user_name, 'password':user_password1}
        f = open('./Student-management/UsersInformation.json', 'r', encoding='utf8')
        info = json.load(f)
        f.close()
        if not info:
            list1.append(dict1)
            f1 = open('./Student-management/UsersInformation.json', 'w', encoding='utf8')
            json.dump(list1,f1)
            f1.close()
        else:
            info.append(dict1)
            f1 = open('./Student-management/UsersInformation.json', 'w', encoding='utf8')
            json.dump(info, f1)
            f1.close()
        # 添加用戶信息后,為該用戶創(chuàng)建一個單獨的文件夾用來存放該用戶的操作信息
        user_path = file_path + '\\Student-management\\User_space\\' + user_name + '//'
        if not os.path.exists(user_path):
            os.makedirs(user_path)
            f_path = user_path + '\\StudentInformation.json'
            if not os.path.exists(f_path):
                list1 = []
                f = open(f_path, 'w', encoding='utf-8')
                json.dump(list1, f)
                f.close()
        print('注冊成功!\n------')
        sys_menu()


# 系統(tǒng)界面菜單
def interface_menu(user_name: str):
    print('='*7,'*','歡迎進入學生管理系統(tǒng)','*','='*7)
    print(' 1. 添加學生\n',
          '2. 刪除學生\n',
          '3. 修改信息\n',
          '4. 查找信息\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        add_menu(user_name)
    elif select_num == '2':
        del_menu(user_name)
    elif select_num == '3':
        re_menu(user_name)
    elif select_num == '4':
        find_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        interface_menu(user_name)


# 添加學生功能部分
def add_menu(user_name: str):     # 添加學生界面菜單
    print('=' * 37)
    print(' 1. 開始添加\n',
          '2. 返回上一級\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        add_stud(user_name)
    elif select_num == '2':
        interface_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        add_menu(user_name)


def add_stud(user_name):
    stu_name = input('請輸入學生姓名:')
    stu_age = input('請輸入學生年齡:')
    stu_num = input('請輸入學生學號:')
    stu_tel = input('請輸入學生電話:')
    stu_info = {'姓名': stu_name,
                '年齡': stu_age,
                '學號': stu_num,
                '電話': stu_tel}
    info = r_json(user_name)
    info.append(stu_info)
    w_json(user_name, info)
    print('添加成功?。。?)
    cont_add(user_name)


def cont_add(user_name: str):
    print('=' * 37)
    print(' 1. 繼續(xù)添加\n',
          '2. 返回首頁\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        add_stud(user_name)
    elif select_num == '2':
        interface_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        cont_add(user_name)


# 刪除學生功能部分
def del_menu(user_name: str):     # 刪除學生界面菜單
    print('=' * 37)
    print(' 1. 根據(jù)姓名刪除\n',
          '2. 根據(jù)學號刪除\n',
          '3. 返回上一級\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        del_stud(1, user_name)
    elif select_num == '2':
        del_stud(2, user_name)
    elif select_num == '3':
        interface_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        del_menu(user_name)


def del_stud(id, user_name: str):
    if id == 1:
        s_name = input('請輸入學生姓名:')
        name_list = []    # 創(chuàng)建一個新的列表,用來存放在json文件中獲取的與輸入姓名相同的學生信息
        id_list = []      # 創(chuàng)建一個新的列表,用來存放在在json文件中獲取的與輸入姓名相同的索引值
        i = 0            # 計算索引值
        count = 0         # 計算json文件中獲取的與輸入姓名相同的元素個數(shù)
        info = r_json(user_name)
        for item in info[:]:
            if item['姓名'] == s_name:
                name_list.append(item)   # 遍歷json文件中與輸入姓名相同的同學,并將信息存入name_list列表中
                id_list.append(i)        # 將存入name_list列表中的同學信息在json文件中的索引值存入id_list列表中
                count += 1               # 統(tǒng)計名字相同的個數(shù)
            i += 1                       # 每循環(huán)一次,索引值+1
        if not count:
            print('未找到該名字學生,請重新輸入')
            del_menu(user_name)
        else:
            for j in range(count):
                print(j+1,'. ',name_list[j])
        del_id = int(input('請刪除學生對應的編號(1 - %d):' % count)) - 1
        for x in range(count):
            if x == del_id:
                del info[id_list[x]]
                w_json(user_name, info)
                print('刪除成功?。?!')
                cont_del(0, user_name)
            else:
                print('刪除錯誤!?。?)
                del_menu(user_name)
    else:
        s_num = input('請輸入學生學號:')
        y = 0  # 計算索引值
        info = r_json(user_name)
        for item in info[:]:
            if item['學號'] == s_num:
                del info[y]   # 遍歷json文件中與輸入學號相同的同學,并刪除
                w_json(user_name, info)
                print('刪除成功?。?!')
                break
            y += 1
        else:
            print('未找到該名字學生,請重新選擇')
            del_menu(user_name)
        cont_del(1, user_name)


def cont_del(did, user_name: str):
    did1 = did
    print('=' * 37)
    print(' 1. 繼續(xù)刪除\n',
          '2. 返回上一級\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        if did1 == 0:
            del_stud(1, user_name)
        else:
            del_stud(2, user_name)
    elif select_num == '2':
        del_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        cont_del(did1, user_name)


# 修改學生功能部分
def re_menu(user_name: str):     # 修改學生界面菜單
    print('=' * 37)
    print(' 1. 根據(jù)姓名修改\n',
          '2. 根據(jù)學號修改\n',
          '3. 返回上一級\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        re_stud(1, user_name)
    elif select_num == '2':
        re_stud(2, user_name)
    elif select_num == '3':
        interface_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        del_menu(user_name)


def re_stud(id, user_name: str):
    if id == 1:
        s_name = input('請輸入學生姓名:')
        name_list = []    # 創(chuàng)建一個新的列表,用來存放在json文件中獲取的與輸入姓名相同的學生信息
        id_list = []      # 創(chuàng)建一個新的列表,用來存放在在json文件中獲取的與輸入姓名相同的索引值
        i = 0            # 計算索引值
        count = 0         # 計算json文件中獲取的與輸入姓名相同的元素個數(shù)
        info = r_json(user_name)
        for item in info[:]:
            if item['姓名'] == s_name:
                name_list.append(item)   # 遍歷json文件中與輸入姓名相同的同學,并將信息存入name_list列表中
                id_list.append(i)        # 將存入name_list列表中的同學信息在json文件中的索引值存入id_list列表中
                count += 1               # 統(tǒng)計名字相同的個數(shù)
            i += 1                       # 每循環(huán)一次,索引值+1
        if not count:
            print('未找到該名字學生,請重新選擇')
            re_menu(user_name)
        else:
            for j in range(count):
                print(j+1,'. ',name_list[j])
        del_id = int(input('請輸入需修改學生對應的編號(1 - %d):' % count)) - 1
        for x in range(count):
            if x == del_id:
                re_info(x, user_name)
    else:
        s_num = input('請輸入學生學號:')
        y = 0  # 計算索引值
        info = r_json(user_name)
        for item in info[:]:
            if item['學號'] == s_num:
                print('該同學信息為:\n', info[y])
                re_info(y, user_name)   # 遍歷json文件中中與輸入學號相同的同學,并修改
                print('修改成功?。。?)
                break
            y += 1
        else:
            print('未找到該名字學生,請重新選擇')
            re_menu(user_name)


def re_info(fid, user_name: str):
    fid2 = fid
    print('=' * 37)
    print(' 1. 修改學生姓名\n',
          '2. 修改學生年齡\n',
          '3. 修改學生學號\n',
          '4. 修改學生電話\n',
          '5. 返回上一級\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    info = r_json(user_name)
    select_num = input('請選擇:')
    if select_num == '1':
        re_name = input('請輸入修改后的姓名:')
        info[fid]['姓名'] = re_name
        w_json(user_name, info)
        print('修改成功?。?!\n修改結(jié)果為:\n', info[fid])
    elif select_num == '2':
        re_age = input('請輸入修改后的年齡:')
        info[fid]['年齡'] = re_age
        w_json(user_name, info)
        print('修改成功?。。n修改結(jié)果為:\n', info[fid])
    elif select_num == '3':
        re_num = input('請輸入修改后的學號:')
        info[fid]['年齡'] = re_num
        w_json(user_name, info)
        print('修改成功?。?!\n修改結(jié)果為:\n', info[fid])
    elif select_num == '4':
        re_tel = input('請輸入修改后的電話:')
        info[fid]['年齡'] = re_tel
        w_json(user_name, info)
        print('修改成功?。?!\n修改結(jié)果為:\n', info[fid])
    elif select_num == '5':
        re_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        interface_menu(user_name)
    re_info(fid2, user_name)


# 查找學生功能部分
def find_menu(user_name: str):     # 查找學生界面菜單
    print('=' * 37)
    print(' 1. 查找所有學生\n',
          '2. 根據(jù)姓名查找\n',
          '3. 根據(jù)學號查找\n',
          '4. 返回上一級\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        find_stud(1, user_name)
    elif select_num == '2':
        find_stud(2, user_name)
    elif select_num == '3':
        find_stud(3, user_name)
    elif select_num == '4':
        interface_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        del_menu(user_name)


def find_stud(id,user_name: str):
    info = r_json(user_name)
    if id == 1:
        print('所有同學信息如下所示:')
        for item in info[:]:
            print(item)
        print('查找成功?。?!')
        find_menu(user_name)
    elif id == 2:
        s_name = input('請輸入學生姓名:')
        name_list = []    # 創(chuàng)建一個新的列表,用來存放在json文件中獲取的與輸入姓名相同的學生信息
        id_list = []      # 創(chuàng)建一個新的列表,用來存放在在json文件中獲取的與輸入姓名相同的索引值
        i = 0            # 計算索引值
        count = 0         # 計算json文件中獲取的與輸入姓名相同的元素個數(shù)
        for item in info[:]:
            if item['姓名'] == s_name:
                name_list.append(item)   # 遍歷json文件中與輸入姓名相同的同學,并將信息存入name_list列表中
                id_list.append(i)        # 將存入name_list列表中的同學信息在json文件中的索引值存入id_list列表中
                count += 1               # 統(tǒng)計名字相同的個數(shù)
            i += 1                       # 每循環(huán)一次,索引值+1
        if not count:
            print('未找到該名字學生,請重新查找')
            cont_find(0, user_name)
        else:
            for j in range(count):
                print(j+1,'. ',name_list[j])
        print('查找成功!??!')
        cont_find(0, user_name)
    else:
        s_num = input('請輸入學生學號:')
        y = 0  # 計算索引值
        for item in info[:]:
            if item['學號'] == s_num:
                print(info[y])   # 遍歷json文件中與輸入學號相同的同學,并輸出
                print('查找成功!??!')
                break
            y += 1
        else:
            print('未找到該學號學生,請重新輸入')
        cont_find(1, user_name)


def cont_find(id2,user_name: str):
    id3 = id2
    print('=' * 37)
    print(' 1. 繼續(xù)查找\n',
          '2. 返回上一級\n',
          'q. 退出系統(tǒng)')
    print('=' * 37)
    # 根據(jù)輸入數(shù)值進入相應操作界面
    select_num = input('請選擇:')
    if select_num == '1':
        if id2 == 0:
            find_stud(2, user_name)
        else:
            find_stud(3, user_name)
    elif select_num == '2':
        find_menu(user_name)
    elif select_num == 'q':
        exit(0)
    else:
        print('選擇錯誤,請選擇重新')
        cont_find(id3, user_name)


# 讀取json文件,并返回學生信息
def r_json(user_name: str):
    f = open('./Student-management/User_space/' + user_name + '/' + 'StudentInformation.json', 'r', encoding='utf8')
    info = json.load(f)
    f.close()
    return info


# 將更新后的信息重新寫入json文件并保存
def w_json(user_name: str, list1: list):
    f = open('./Student-management/User_space/' + user_name + '/' + 'StudentInformation.json', 'w', encoding='utf8')
    json.dump(list1, f)
    f.close()


# 在程序運行時,在當前路徑下為管理系統(tǒng)創(chuàng)建數(shù)據(jù)存放文件夾
file_path = os.getcwd()  # 返回當前工作目錄
sys_path = file_path + '\\Student-management//'
if not os.path.exists(sys_path):
    os.makedirs(sys_path)
    f_path = file_path + '\\Student-management\\UsersInformation.json'
    if not os.path.exists(f_path):
        list1 = []
        f = open(f_path, 'w', encoding='utf-8')
        json.dump(list1,f)
        f.close()

sys_menu()

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

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

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