|
|
|
@ -0,0 +1,117 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
|
|
DATA_FILE = 'students.json'
|
|
|
|
|
|
|
|
|
|
def load_students():
|
|
|
|
|
"""从文件加载学生数据"""
|
|
|
|
|
if os.path.exists(DATA_FILE):
|
|
|
|
|
with open(DATA_FILE, 'r', encoding='utf-8') as f:
|
|
|
|
|
return json.load(f)
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
def save_students(students):
|
|
|
|
|
"""保存学生数据到文件"""
|
|
|
|
|
with open(DATA_FILE, 'w', encoding='utf-8') as f:
|
|
|
|
|
json.dump(students, f, ensure_ascii=False, indent=2)
|
|
|
|
|
|
|
|
|
|
def input_student_info():
|
|
|
|
|
"""输入学生信息并验证"""
|
|
|
|
|
name = input("请输入姓名:").strip()
|
|
|
|
|
while not name:
|
|
|
|
|
print("姓名不能为空!")
|
|
|
|
|
name = input("请输入姓名:").strip()
|
|
|
|
|
|
|
|
|
|
gender = input("请输入性别(男/女):").strip()
|
|
|
|
|
while gender not in ['男', '女']:
|
|
|
|
|
print("性别输入错误,请重新输入!")
|
|
|
|
|
gender = input("请输入性别(男/女):").strip()
|
|
|
|
|
|
|
|
|
|
class_name = input("请输入班级:").strip()
|
|
|
|
|
while not class_name:
|
|
|
|
|
print("班级不能为空!")
|
|
|
|
|
class_name = input("请输入班级:").strip()
|
|
|
|
|
|
|
|
|
|
phone = input("请输入电话号码:").strip()
|
|
|
|
|
while not (phone.isdigit() and len(phone) >= 7):
|
|
|
|
|
print("电话号码必须为数字且至少7位!")
|
|
|
|
|
phone = input("请输入电话号码:").strip()
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'name': name,
|
|
|
|
|
'gender': gender,
|
|
|
|
|
'class': class_name,
|
|
|
|
|
'phone': phone
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def add_student(students):
|
|
|
|
|
"""添加学生"""
|
|
|
|
|
student = input_student_info()
|
|
|
|
|
students.append(student)
|
|
|
|
|
print(f"学生 {student['name']} 添加成功!")
|
|
|
|
|
|
|
|
|
|
def delete_student(students):
|
|
|
|
|
"""删除学生"""
|
|
|
|
|
name = input("请输入要删除的学生姓名:").strip()
|
|
|
|
|
found = [s for s in students if s['name'] == name]
|
|
|
|
|
|
|
|
|
|
if not found:
|
|
|
|
|
print("未找到该学生!")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for s in found:
|
|
|
|
|
students.remove(s)
|
|
|
|
|
|
|
|
|
|
print(f"已删除 {len(found)} 个名为 {name} 的学生")
|
|
|
|
|
|
|
|
|
|
def search_student(students):
|
|
|
|
|
"""查询学生"""
|
|
|
|
|
keyword = input("请输入查询关键字(姓名或班级):").strip()
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
|
|
for s in students:
|
|
|
|
|
if keyword in s['name'] or keyword in s['class']:
|
|
|
|
|
results.append(s)
|
|
|
|
|
|
|
|
|
|
if not results:
|
|
|
|
|
print("未找到匹配的学生!")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
print("\n查询结果:")
|
|
|
|
|
for i, student in enumerate(results, 1):
|
|
|
|
|
print(f"{i}. 姓名:{student['name']}")
|
|
|
|
|
print(f" 性别:{student['gender']}")
|
|
|
|
|
print(f" 班级:{student['class']}")
|
|
|
|
|
print(f" 电话:{student['phone']}\n")
|
|
|
|
|
|
|
|
|
|
def show_menu():
|
|
|
|
|
"""显示主菜单"""
|
|
|
|
|
print("\n学生通讯录管理系统")
|
|
|
|
|
print("1. 添加学生")
|
|
|
|
|
print("2. 删除学生")
|
|
|
|
|
print("3. 查询学生")
|
|
|
|
|
print("4. 退出系统")
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
students = load_students()
|
|
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
show_menu()
|
|
|
|
|
choice = input("请选择操作(1-4):").strip()
|
|
|
|
|
|
|
|
|
|
if choice == '1':
|
|
|
|
|
add_student(students)
|
|
|
|
|
elif choice == '2':
|
|
|
|
|
delete_student(students)
|
|
|
|
|
elif choice == '3':
|
|
|
|
|
search_student(students)
|
|
|
|
|
elif choice == '4':
|
|
|
|
|
save_students(students)
|
|
|
|
|
print("系统已退出,数据已保存!")
|
|
|
|
|
break
|
|
|
|
|
else:
|
|
|
|
|
print("输入无效,请重新输入!")
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|