簡(jiǎn)單學(xué)生管理系統(tǒng)實(shí)現(xiàn)
重點(diǎn):了解項(xiàng)目開(kāi)發(fā)大概過(guò)程,以及項(xiàng)目如何逐步實(shí)現(xiàn)
- 1、畫(huà)思維導(dǎo)圖,分功能模塊畫(huà)思維導(dǎo)圖
- 2、數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì),根據(jù)實(shí)際情況設(shè)計(jì)相應(yīng)的數(shù)據(jù)結(jié)構(gòu)
- 3、分功能或模塊逐個(gè)實(shí)現(xiàn)相應(yīng)功能
具體實(shí)現(xiàn)如下:
main_page.py
# 導(dǎo)入不在相同目錄下的導(dǎo)入模塊
import module.file_manager as file_manager
import module.student_manager as student_manager
# 注冊(cè)
def register():
"""
注冊(cè)功能
:return:
"""
# 控制輸入賬號(hào)
while True:
user_name = input('請(qǐng)輸入賬號(hào)3~6位:')
# 檢查賬號(hào)密碼位數(shù)是否符合要求
if not 3 <= len(user_name) <= 6:
print('賬號(hào)有誤,請(qǐng)重新輸入!')
continue
else:
break
# 控制輸入密碼
while True:
# 在賬號(hào)輸入成功的前提下輸入密碼
password = input('請(qǐng)輸入密碼(6~12位):')
if not 6 <= len(password) <= 12:
print('密碼有誤,請(qǐng)重新輸入!')
continue
else:
break
# 檢查賬號(hào)是否已經(jīng)注冊(cè)過(guò)
"""
數(shù)據(jù)設(shè)計(jì):用一個(gè)字典來(lái)保存所有賬號(hào)和密碼{賬號(hào):密碼},
將所有的賬號(hào)密碼存到文件中
選用json文件
"""
# 獲取注冊(cè)過(guò)的所有賬號(hào)信息
all_user = file_manager.json_read('all_user.json')
# 判斷輸入賬號(hào)是否存在
if user_name in all_user:
print('該用戶(hù)名已經(jīng)被注冊(cè)!')
return
# 賬號(hào)可用,將賬號(hào)信息添加到所有用戶(hù)里面
all_user[user_name] = password
# 更新本地文件
re = file_manager.json_write('all_user.json', all_user)
if re:
print('注冊(cè)成功!')
else:
print('注冊(cè)失敗!')
# 登錄
def login():
user_name = input('請(qǐng)輸入賬號(hào):')
password = input('請(qǐng)輸入密碼:')
# 檢查賬號(hào)是否注冊(cè)
# 獲取所有賬號(hào)信息
all_user = file_manager.json_read('all_user.json')
if user_name not in all_user:
print('登錄失敗,賬號(hào)不存在!')
return
# 賬號(hào)存在先通過(guò)賬號(hào)去獲取正確的密碼
password_old = all_user[user_name]
if password == password_old:
print('登錄成功!')
student_manager.show_manager_page(user_name)
# 登錄成功以后的操作寫(xiě)在這里
else:
print('密碼錯(cuò)誤,登錄失敗!')
return
def show_main_page():
while True:
print(file_manager.text_read_file('main_page.txt'))
value = input('請(qǐng)輸入你的選擇(1-3):')
# 根據(jù)不同的選擇做出不同的操作
if value == '1':
login()
elif value == '2':
register()
elif value == '3':
return
else:
print('輸入有誤')
if __name__ == '__main__':
# 顯示登錄頁(yè)面
show_main_page()
student_page.py
import module.file_manager as file_manager
# 這個(gè)全局變量用來(lái)保存登錄成功的用戶(hù)名
current_user = ''
# 添加學(xué)生
"""
1、賬號(hào)和賬號(hào)管理的學(xué)生要一一對(duì)應(yīng)
2、一個(gè)賬號(hào)要管理多個(gè)學(xué)生
3、一個(gè)學(xué)生要存儲(chǔ)多個(gè)信息,每個(gè)學(xué)生存儲(chǔ)的信息數(shù)量一樣
方案一:整個(gè)系統(tǒng)管理的所有的賬號(hào)管理的所有學(xué)生放在一起
{
賬號(hào)1:[{'name': name, 'age':age, 'tel':tell, 'id':id}, ....]
賬號(hào)2:[學(xué)生1,學(xué)生2]
賬號(hào)3:[]....
......
}
將一個(gè)大字典寫(xiě)到一個(gè)json文件中
方案二:
1、一個(gè)賬號(hào)對(duì)應(yīng)一個(gè)json文件
2、json文件名和賬號(hào)名一一對(duì)應(yīng)
{
count: num,
all_student: [{'name': name, 'age':age, 'tel':tell, 'id':id},......]
}
"""
# 通過(guò)全局變量保存常用key值(開(kāi)發(fā)常用)
key_count = 'count'
key_all_students = 'all_student'
key_name = 'name'
key_age = 'age'
key_tel = 'tel'
key_id = 'id'
# 獲取獲取當(dāng)前賬號(hào)對(duì)應(yīng)的json文件中的內(nèi)容
def get_current_userinfo():
"""
獲取當(dāng)前賬號(hào)對(duì)應(yīng)的json文件中的內(nèi)容
:return:
"""
content = file_manager.json_read(current_user + '.json')
if not content:
content = {}
return content
# 格式化打印學(xué)生信息
def show_student_info(stu):
print(' %s %s %s %s ' % (stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))
# print('學(xué)號(hào):%s 姓名:%s 年齡:%s 電話(huà):%s'\
# % (stu[key_id], stu[key_name], stu[key_age], stu[key_tel]))
def show_head():
print(' | 學(xué)號(hào) | 姓名 | 年齡 | 電話(huà) |')
# 按姓名查找學(xué)生信息
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
# 按學(xué)生學(xué)號(hào)查找學(xué)生
def find_student_with_id(stu_id, all_students):
"""
按學(xué)號(hào)查找
:param stu_id:
:param all_students:
:return:
"""
for stu in all_students:
if stu[key_id] == stu_id:
return stu
return None
# 添加學(xué)生
def add_student():
# 獲取當(dāng)前賬號(hào)對(duì)應(yīng)的數(shù)據(jù)
user_info = get_current_userinfo()
# 輸入信息
while True:
name = input('請(qǐng)輸入姓名:')
age = input('請(qǐng)輸入學(xué)生年齡:')
tel = input('請(qǐng)輸入學(xué)生電話(huà):')
# 產(chǎn)生學(xué)號(hào)
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('請(qǐng)選擇:')
if value == '1':
continue
else:
return
# 修改信息
def change_student():
"""
修改學(xué)生
:return:
"""
# 拿到所有學(xué)生信息
user_info = get_current_userinfo()
all_students = user_info.get(key_all_students, [])
if not all_students:
print('沒(méi)有可管理的學(xué)生!')
return
# 用于標(biāo)記需要修改的學(xué)生下標(biāo)
index = 1
show_head()
for stu in all_students:
print(index, end=' ')
show_student_info(stu)
index += 1
while True:
# 給出選擇
print('1. 修改姓名\n2. 修改年齡\n3. 修改電話(huà)\n4. 修改所有信息\n5. 返回上層')
# 記錄選擇值,做出不同的操作
value = input('請(qǐng)輸入你的選擇:')
if value in '1234':
change_stu_id = input('請(qǐng)輸入需要修改的學(xué)生標(biāo)號(hào)(如上所示):')
# change_stu_index = find_student_return_index(change_stu_id, all_students)
# if not str(change_stu_index).isalnum():
# print('該學(xué)號(hào)不存在,請(qǐng)核對(duì)!')
# continue
if not int(change_stu_id) <= len(all_students):
print('輸入有誤,請(qǐng)從新輸入!')
if value == '1':
new_name = input('請(qǐng)輸入新名字:')
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('請(qǐng)輸入新年齡:')
# 更改年齡值
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':
# 獲取新的電話(huà)號(hào)碼
new_tel = input('請(qǐng)輸入新電話(huà)號(hào)碼:')
# 更改數(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('請(qǐng)輸入新姓名:')
new_age = input('請(qǐng)輸入新年齡:')
new_tel = input('請(qǐng)輸入新電話(huà):')
# 更該數(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('輸入錯(cuò)誤,請(qǐng)重新輸入!')
continue
# 刪除學(xué)生信息
def delete_student():
"""
刪除學(xué)生
:return:
"""
# 拿到所有的學(xué)生
user_info = get_current_userinfo()
all_sutdents = user_info.get(key_all_students, [])
if not all_sutdents:
print('當(dāng)前賬號(hào)沒(méi)有可管理的學(xué)生')
return
# 給出選擇
while True:
print('1. 按姓名刪除\n2. 按學(xué)號(hào)刪除\n3. 返回')
value = input('請(qǐng)輸入選擇(1~3):')
if value == '1':
# print('按姓名刪除')
name = input('請(qǐng)輸入姓名:')
students = find_student_with_name(name, all_sutdents)
# 判斷是否有同名的學(xué)生
if not students:
print('該學(xué)生不存在!')
continue
# 如果找到了學(xué)生,先展示找到的學(xué)生
index = 0
show_head()
for stu in students:
print(index,end=' ')
show_student_info(stu)
index += 1
# 選擇需要?jiǎng)h除的學(xué)生的下標(biāo)
print('q. 返回上層')
del_index = input('請(qǐng)輸入需要?jiǎng)h除的學(xué)生對(duì)應(yīng)的標(biāo)號(hào):')
if del_index == 'q':
continue
else:
# 找到要?jiǎng)h除的學(xué)生對(duì)應(yīng)的字典
del_stu = students[int(del_index)]
# 從所有的學(xué)生對(duì)應(yīng)的列表中刪除
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('請(qǐng)輸入學(xué)號(hào):')
# 根據(jù)學(xué)號(hào)查找學(xué)生
del_stu = find_student_with_id(del_id, all_sutdents)
if not del_id:
print('沒(méi)有改學(xué)生!')
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
# 查詢(xún)學(xué)生信息
def find_student():
user_info = get_current_userinfo()
all_sutdents = user_info.get(key_all_students, None)
if not all_sutdents:
print('當(dāng)前賬號(hào)沒(méi)有可管理的學(xué)生')
return
# 給出選擇
while True:
print('1. 查看所有學(xué)生\n2. 按姓名查找\n3. 按學(xué)號(hào)查找\n4. 返回')
value = input('請(qǐng)選擇(1~4):')
if value == '1':
# 打印表頭
show_head()
for stu in all_sutdents:
show_student_info(stu)
elif value == '2':
name = input('請(qǐng)輸入姓名:')
students = find_student_with_name(name, all_sutdents)
if students:
show_head()
for stu in students:
show_student_info(stu)
else:
print('學(xué)生%s不存在!' % name)
elif value == '3':
stu_id = input('請(qǐng)輸入學(xué)號(hào):')
stu = find_student_with_id(stu_id, all_sutdents)
if stu:
show_head()
show_student_info(stu)
else:
print('沒(méi)有找到%s對(duì)應(yīng)的學(xué)生!' % stu_id)
else:
return
# 打印學(xué)生信息
def show_manager_page(user_name):
# 保存登錄成功的用戶(hù)名
global current_user
current_user = user_name
# 顯示管理界面
while True:
content = file_manager.text_read_file('manager_page.txt')
print(content % user_name)
# 做出選擇
value = input('請(qǐng)選擇(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('輸入有誤,請(qǐng)從新輸入!')
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):
"""
普通文本寫(xiě)操作
:param file_name 文件路徑
:param content 需要寫(xiě)入的內(nèi)容
:return: 寫(xiě)入的結(jié)果
"""
try:
with open('./files/' + file_name, 'w', encoding='utf-8') as f:
f.write(content)
return True
except:
print('寫(xiě)入失敗!')
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文件寫(xiě)操作
:param file_name 文件名
:param content: 寫(xiě)入內(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('寫(xiě)入失敗')
return False
if __name__ == '__main__':
pass