forked from p3qolzsn9/stu1
大
parent
c6959191ea
commit
423766fd7f
@ -0,0 +1,197 @@
|
|||||||
|
import sys
|
||||||
|
from datetime import date
|
||||||
|
from typing import Optional
|
||||||
|
from models.student import Student
|
||||||
|
from bll.student_service import StudentService
|
||||||
|
|
||||||
|
|
||||||
|
class ConsoleUI:
|
||||||
|
"""控制台用户界面"""
|
||||||
|
|
||||||
|
def __init__(self, service: StudentService):
|
||||||
|
self.service = service
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
"""运行主界面"""
|
||||||
|
while True:
|
||||||
|
self._display_main_menu()
|
||||||
|
choice = input("请选择操作: ").strip()
|
||||||
|
|
||||||
|
if choice == '1':
|
||||||
|
self._add_student()
|
||||||
|
elif choice == '2':
|
||||||
|
self._delete_student()
|
||||||
|
elif choice == '3':
|
||||||
|
self._update_student()
|
||||||
|
elif choice == '4':
|
||||||
|
self._query_students()
|
||||||
|
elif choice == '5':
|
||||||
|
self._statistics()
|
||||||
|
elif choice == '6':
|
||||||
|
self._import_export()
|
||||||
|
elif choice == '7':
|
||||||
|
self._clear_all_data()
|
||||||
|
elif choice == '0':
|
||||||
|
print("感谢使用学生信息管理系统,再见!")
|
||||||
|
sys.exit(0)
|
||||||
|
else:
|
||||||
|
print("无效选择,请重新输入!")
|
||||||
|
|
||||||
|
def _display_main_menu(self):
|
||||||
|
"""显示主菜单"""
|
||||||
|
print("\n" + "=" * 30)
|
||||||
|
print(" 学生信息管理系统 ")
|
||||||
|
print("=" * 30)
|
||||||
|
print("1. 添加学生信息")
|
||||||
|
print("2. 删除学生信息")
|
||||||
|
print("3. 更新学生信息")
|
||||||
|
print("4. 查询学生信息")
|
||||||
|
print("5. 统计分析")
|
||||||
|
print("6. 数据导入导出")
|
||||||
|
print("7. 清空所有学生信息")
|
||||||
|
print("0. 退出系统")
|
||||||
|
print("=" * 30)
|
||||||
|
|
||||||
|
def _add_student(self):
|
||||||
|
"""添加学生"""
|
||||||
|
print("\n" + "=" * 30)
|
||||||
|
print(" 添加学生信息 ")
|
||||||
|
print("=" * 30)
|
||||||
|
|
||||||
|
student = self._input_student_info()
|
||||||
|
if student is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
success, msg = self.service.add_student(student)
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
def _delete_student(self):
|
||||||
|
"""删除学生"""
|
||||||
|
print("\n" + "=" * 30)
|
||||||
|
print(" 删除学生信息 ")
|
||||||
|
print("=" * 30)
|
||||||
|
|
||||||
|
id_card = input("请输入要删除学生的身份证号: ").strip()
|
||||||
|
success, msg = self.service.delete_student(id_card)
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
def _update_student(self):
|
||||||
|
"""更新学生信息"""
|
||||||
|
print("\n" + "=" * 30)
|
||||||
|
print(" 更新学生信息 ")
|
||||||
|
print("=" * 30)
|
||||||
|
|
||||||
|
id_card = input("请输入要更新学生的身份证号: ").strip()
|
||||||
|
existing = self.service.get_student_by_id(id_card)
|
||||||
|
if not existing:
|
||||||
|
print("学生不存在")
|
||||||
|
return
|
||||||
|
|
||||||
|
print("\n当前学生信息:")
|
||||||
|
self._display_student_details(existing)
|
||||||
|
|
||||||
|
print("\n请输入新的学生信息(留空保持原值):")
|
||||||
|
student = self._input_student_info(existing)
|
||||||
|
if student is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
success, msg = self.service.update_student(student)
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
def _query_students(self):
|
||||||
|
"""查询学生信息"""
|
||||||
|
while True:
|
||||||
|
print("\n" + "=" * 30)
|
||||||
|
print(" 查询学生信息 ")
|
||||||
|
print("=" * 30)
|
||||||
|
print("1. 查询所有学生")
|
||||||
|
print("2. 按身份证号查询")
|
||||||
|
print("3. 按学号查询")
|
||||||
|
print("4. 按姓名查询")
|
||||||
|
print("5. 按班级查询")
|
||||||
|
print("6. 按专业查询")
|
||||||
|
print("7. 返回上一级")
|
||||||
|
print("=" * 30)
|
||||||
|
|
||||||
|
choice = input("请选择操作: ").strip()
|
||||||
|
|
||||||
|
if choice == '1':
|
||||||
|
self._list_all_students()
|
||||||
|
elif choice == '2':
|
||||||
|
self._query_by_id_card()
|
||||||
|
elif choice == '3':
|
||||||
|
self._query_by_stu_id()
|
||||||
|
elif choice == '4':
|
||||||
|
self._query_by_name()
|
||||||
|
elif choice == '5':
|
||||||
|
self._query_by_class()
|
||||||
|
elif choice == '6':
|
||||||
|
self._query_by_major()
|
||||||
|
elif choice == '7':
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
print("无效选择,请重新输入!")
|
||||||
|
|
||||||
|
def _statistics(self):
|
||||||
|
"""统计分析"""
|
||||||
|
while True:
|
||||||
|
print("\n" + "=" * 30)
|
||||||
|
print(" 统计分析 ")
|
||||||
|
print("=" * 30)
|
||||||
|
print("1. 学生总数")
|
||||||
|
print("2. 各专业人数统计")
|
||||||
|
print("3. 平均身高")
|
||||||
|
print("4. 平均体重")
|
||||||
|
print("5. 返回上一级")
|
||||||
|
print("=" * 30)
|
||||||
|
|
||||||
|
choice = input("请选择操作: ").strip()
|
||||||
|
|
||||||
|
if choice == '1':
|
||||||
|
count = self.service.count_total_students()
|
||||||
|
print(f"\n学生总数: {count}")
|
||||||
|
elif choice == '2':
|
||||||
|
stats = self.service.count_by_major()
|
||||||
|
print("\n各专业人数统计:")
|
||||||
|
for major, count in stats.items():
|
||||||
|
print(f"{major}: {count}人")
|
||||||
|
elif choice == '3':
|
||||||
|
avg_height = self.service.calculate_avg_height()
|
||||||
|
print(f"\n全体学生平均身高: {avg_height['all']:.1f}cm")
|
||||||
|
elif choice == '4':
|
||||||
|
# 类似实现平均体重
|
||||||
|
pass
|
||||||
|
elif choice == '5':
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
print("无效选择,请重新输入!")
|
||||||
|
|
||||||
|
def _input_student_info(self, existing: Student = None) -> Optional[Student]:
|
||||||
|
"""输入学生信息"""
|
||||||
|
try:
|
||||||
|
name = self._get_input("姓名", existing.name if existing else None, required=True)
|
||||||
|
id_card = self._get_input("身份证号", existing.id_card if existing else None, required=True)
|
||||||
|
stu_id = self._get_input("学号", existing.stu_id if existing else None, required=True)
|
||||||
|
gender = self._get_gender_input(existing.gender if existing else None)
|
||||||
|
height = self._get_int_input("身高(cm)", existing.height if existing else None, 50, 250)
|
||||||
|
weight = self._get_float_input("体重(kg)", existing.weight if existing else None, 5, 300)
|
||||||
|
enrollment_date = self._get_date_input("入学日期", existing.enrollment_date if existing else None)
|
||||||
|
class_name = self._get_input("班级", existing.class_name if existing else None)
|
||||||
|
major = self._get_input("专业", existing.major if existing else None)
|
||||||
|
|
||||||
|
return Student(
|
||||||
|
name=name,
|
||||||
|
id_card=id_card,
|
||||||
|
stu_id=stu_id,
|
||||||
|
gender=gender,
|
||||||
|
height=height,
|
||||||
|
weight=weight,
|
||||||
|
enrollment_date=enrollment_date,
|
||||||
|
class_name=class_name,
|
||||||
|
major=major
|
||||||
|
)
|
||||||
|
except ValueError as e:
|
||||||
|
print(f"输入错误: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
# 其他辅助方法...
|
Loading…
Reference in new issue