學(xué)生管理系統(tǒng)
"""__author__=Zeng"""
import os
import json
# 登錄界面
def sys_menu():
print('='*7, '*', '歡迎來到學(xué)生管理系統(tǒng)', '*', '='*7)
print(' 1. 登錄\n',
'2. 注冊\n',
'q. 退出系統(tǒng)')
print('=' * 37)
# 根據(jù)輸入數(shù)值進入相應(yīng)操作界面
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('登錄失斢肱Α!密碼錯誤塘装!\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,'*','歡迎進入學(xué)生管理系統(tǒng)','*','='*7)
print(' 1. 添加學(xué)生\n',
'2. 刪除學(xué)生\n',
'3. 修改信息\n',
'4. 查找信息\n',
'q. 退出系統(tǒng)')
print('=' * 37)
# 根據(jù)輸入數(shù)值進入相應(yīng)操作界面
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)
# 添加學(xué)生功能部分
def add_menu(user_name: str): # 添加學(xué)生界面菜單
print('=' * 37)
print(' 1. 開始添加\n',
'2. 返回上一級\n',
'q. 退出系統(tǒng)')
print('=' * 37)
# 根據(jù)輸入數(shù)值進入相應(yīng)操作界面
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('請輸入學(xué)生姓名:')
stu_age = input('請輸入學(xué)生年齡:')
stu_num = input('請輸入學(xué)生學(xué)號:')
stu_tel = input('請輸入學(xué)生電話:')
stu_info = {'姓名': stu_name,
'年齡': stu_age,
'學(xué)號': stu_num,
'電話': stu_tel}
info = r_json(user_name)
info.append(stu_info)
w_json(user_name, info)
print('添加成功!C汀嫉称!')
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ù)值進入相應(yīng)操作界面
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)
# 刪除學(xué)生功能部分
def del_menu(user_name: str): # 刪除學(xué)生界面菜單
print('=' * 37)
print(' 1. 根據(jù)姓名刪除\n',
'2. 根據(jù)學(xué)號刪除\n',
'3. 返回上一級\n',
'q. 退出系統(tǒng)')
print('=' * 37)
# 根據(jù)輸入數(shù)值進入相應(yīng)操作界面
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('請輸入學(xué)生姓名:')
name_list = [] # 創(chuàng)建一個新的列表织阅,用來存放在json文件中獲取的與輸入姓名相同的學(xué)生信息
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文件中與輸入姓名相同的同學(xué)震捣,并將信息存入name_list列表中
id_list.append(i) # 將存入name_list列表中的同學(xué)信息在json文件中的索引值存入id_list列表中
count += 1 # 統(tǒng)計名字相同的個數(shù)
i += 1 # 每循環(huán)一次荔棉,索引值+1
if not count:
print('未找到該名字學(xué)生,請重新輸入')
del_menu(user_name)
else:
for j in range(count):
print(j+1,'. ',name_list[j])
del_id = int(input('請刪除學(xué)生對應(yīng)的編號(1 - %d):' % count)) - 1
for x in range(count):
if x == del_id:
del info[id_list[x]]
w_json(user_name, info)
print('刪除成功]镉H笥!!')
cont_del(0, user_name)
else:
print('刪除錯誤O劭谩R既簟!')
del_menu(user_name)
else:
s_num = input('請輸入學(xué)生學(xué)號:')
y = 0 # 計算索引值
info = r_json(user_name)
for item in info[:]:
if item['學(xué)號'] == s_num:
del info[y] # 遍歷json文件中與輸入學(xué)號相同的同學(xué)皂冰,并刪除
w_json(user_name, info)
print('刪除成功5暾埂!秃流!')
break
y += 1
else:
print('未找到該名字學(xué)生赂蕴,請重新選擇')
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ù)值進入相應(yīng)操作界面
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)
# 修改學(xué)生功能部分
def re_menu(user_name: str): # 修改學(xué)生界面菜單
print('=' * 37)
print(' 1. 根據(jù)姓名修改\n',
'2. 根據(jù)學(xué)號修改\n',
'3. 返回上一級\n',
'q. 退出系統(tǒng)')
print('=' * 37)
# 根據(jù)輸入數(shù)值進入相應(yīng)操作界面
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('請輸入學(xué)生姓名:')
name_list = [] # 創(chuàng)建一個新的列表概说,用來存放在json文件中獲取的與輸入姓名相同的學(xué)生信息
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文件中與輸入姓名相同的同學(xué)嚣伐,并將信息存入name_list列表中
id_list.append(i) # 將存入name_list列表中的同學(xué)信息在json文件中的索引值存入id_list列表中
count += 1 # 統(tǒng)計名字相同的個數(shù)
i += 1 # 每循環(huán)一次糖赔,索引值+1
if not count:
print('未找到該名字學(xué)生,請重新選擇')
re_menu(user_name)
else:
for j in range(count):
print(j+1,'. ',name_list[j])
del_id = int(input('請輸入需修改學(xué)生對應(yīng)的編號(1 - %d):' % count)) - 1
for x in range(count):
if x == del_id:
re_info(x, user_name)
else:
s_num = input('請輸入學(xué)生學(xué)號:')
y = 0 # 計算索引值
info = r_json(user_name)
for item in info[:]:
if item['學(xué)號'] == s_num:
print('該同學(xué)信息為:\n', info[y])
re_info(y, user_name) # 遍歷json文件中中與輸入學(xué)號相同的同學(xué)轩端,并修改
print('修改成功9夷怼!!')
break
y += 1
else:
print('未找到該名字學(xué)生刻撒,請重新選擇')
re_menu(user_name)
def re_info(fid, user_name: str):
fid2 = fid
print('=' * 37)
print(' 1. 修改學(xué)生姓名\n',
'2. 修改學(xué)生年齡\n',
'3. 修改學(xué)生學(xué)號\n',
'4. 修改學(xué)生電話\n',
'5. 返回上一級\n',
'q. 退出系統(tǒng)')
print('=' * 37)
# 根據(jù)輸入數(shù)值進入相應(yīng)操作界面
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('修改成功9翘铩!声怔!\n修改結(jié)果為:\n', info[fid])
elif select_num == '2':
re_age = input('請輸入修改后的年齡:')
info[fid]['年齡'] = re_age
w_json(user_name, info)
print('修改成功L汀!醋火!\n修改結(jié)果為:\n', info[fid])
elif select_num == '3':
re_num = input('請輸入修改后的學(xué)號:')
info[fid]['年齡'] = re_num
w_json(user_name, info)
print('修改成功S破!芥驳!\n修改結(jié)果為:\n', info[fid])
elif select_num == '4':
re_tel = input('請輸入修改后的電話:')
info[fid]['年齡'] = re_tel
w_json(user_name, info)
print('修改成功J脸濉!兆旬!\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)
# 查找學(xué)生功能部分
def find_menu(user_name: str): # 查找學(xué)生界面菜單
print('=' * 37)
print(' 1. 查找所有學(xué)生\n',
'2. 根據(jù)姓名查找\n',
'3. 根據(jù)學(xué)號查找\n',
'4. 返回上一級\n',
'q. 退出系統(tǒng)')
print('=' * 37)
# 根據(jù)輸入數(shù)值進入相應(yīng)操作界面
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('所有同學(xué)信息如下所示:')
for item in info[:]:
print(item)
print('查找成功@鲡K薇ァ!')
find_menu(user_name)
elif id == 2:
s_name = input('請輸入學(xué)生姓名:')
name_list = [] # 創(chuàng)建一個新的列表脚祟,用來存放在json文件中獲取的與輸入姓名相同的學(xué)生信息
id_list = [] # 創(chuàng)建一個新的列表谬以,用來存放在在json文件中獲取的與輸入姓名相同的索引值
i = 0 # 計算索引值
count = 0 # 計算json文件中獲取的與輸入姓名相同的元素個數(shù)
for item in info[:]:
if item['姓名'] == s_name:
name_list.append(item) # 遍歷json文件中與輸入姓名相同的同學(xué),并將信息存入name_list列表中
id_list.append(i) # 將存入name_list列表中的同學(xué)信息在json文件中的索引值存入id_list列表中
count += 1 # 統(tǒng)計名字相同的個數(shù)
i += 1 # 每循環(huán)一次由桌,索引值+1
if not count:
print('未找到該名字學(xué)生为黎,請重新查找')
cont_find(0, user_name)
else:
for j in range(count):
print(j+1,'. ',name_list[j])
print('查找成功!P心碍舍!')
cont_find(0, user_name)
else:
s_num = input('請輸入學(xué)生學(xué)號:')
y = 0 # 計算索引值
for item in info[:]:
if item['學(xué)號'] == s_num:
print(info[y]) # 遍歷json文件中與輸入學(xué)號相同的同學(xué),并輸出
print('查找成功R匮拧!妈经!')
break
y += 1
else:
print('未找到該學(xué)號學(xué)生淮野,請重新輸入')
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ù)值進入相應(yīng)操作界面
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文件吹泡,并返回學(xué)生信息
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()