import sys from business_logic import StudentService, IDCardValidator from data_access import StudentDataAccess class StudentManagementSystem: def __init__(self): self.data_access = StudentDataAccess() self.student_service = StudentService(self.data_access) def show_menu(self): """显示主菜单""" menu = """ === 学生信息管理系统 === 1. 显示所有学生信息 2. 按学号查询学生信息 3. 添加学生信息 4. 修改学生信息 5. 删除学生信息 6. 统计学生信息 7. 按总分排序显示 8. 验证身份证号 0. 退出系统 """ print(menu) def run(self): """运行系统""" self.student_service.load_data() print("学生信息已成功加载!") while True: self.show_menu() choice = input("请选择操作(0-8): ").strip() if choice == '0': print("感谢使用学生信息管理系统,再见!") sys.exit() elif choice == '1': self.show_all_students() elif choice == '2': self.search_student() elif choice == '3': self.add_student() elif choice == '4': self.update_student() elif choice == '5': self.delete_student() elif choice == '6': self.show_statistics() elif choice == '7': self.show_sorted_students() elif choice == '8': self.validate_id_card() else: print("无效的选择,请重新输入!") input("\n按回车键继续...") def show_all_students(self): """显示所有学生信息""" students = self.student_service.get_all_students() if not students: print("当前没有学生信息!") return print("\n=== 所有学生信息 ===") print("学号\t姓名\t性别\t年龄\t班级\t总分") for student_id, info in students.items(): basic_info = info['basic_info'] print(f"{student_id}\t{basic_info['name']}\t{basic_info.get('gender', '未知')}\t" f"{basic_info.get('age', '未知')}\t{basic_info['class']}\t{info['score']['sum']}") def search_student(self): """按学号查询学生信息""" student_id = input("请输入要查询的学生学号: ") student = self.student_service.search_student_by_id(student_id) if student: print("\n=== 学生详细信息 ===") print("基本信息:") for key, value in student['basic_info'].items(): print(f" {key}: {value}") print("\n成绩信息:") for subject, score in student['score'].items(): if subject in self.student_service.subject_name: print(f" {self.student_service.subject_name[subject]}: {score}") else: print("未找到该学号的学生!") def add_student(self): """添加新学生""" print("\n=== 添加新学生 ===") # 输入学号 while True: student_id = input("请输入8位学号(如20230001): ").strip() if not student_id.isdigit() or len(student_id) != 8: print("学号必须为8位数字!") continue if self.student_service.search_student_by_id(student_id): print("该学号已存在!") continue break # 输入身份证号 while True: id_card = input("请输入18位身份证号: ").strip().upper() if not id_card: print("身份证号不能为空!") continue is_valid, msg = self.student_service.validate_id_card(id_card) if not is_valid: print(f"身份证号无效: {msg}") continue if self.student_service.is_id_card_exists(id_card): print("该身份证号已存在!") continue break # 自动获取性别和出生日期 gender = IDCardValidator.get_gender(id_card) birth_date = IDCardValidator.get_birth_date(id_card) age = IDCardValidator.calculate_age(id_card) # 输入其他信息 name = input("请输入姓名: ").strip() class_name = input("请输入班级: ").strip() admission_year = input("请输入入学年份: ").strip() # 输入成绩 math_score = float(input("请输入数学成绩: ")) python_score = float(input("请输入Python成绩: ")) english_score = float(input("请输入英语成绩: ")) physics_score = float(input("请输入物理成绩: ")) total_score = math_score + python_score + english_score + physics_score # 创建学生信息字典 student = { 'basic_info': { 'id': student_id, 'id_card': id_card, 'name': name, 'gender': gender, 'birth_date': birth_date.strftime("%Y-%m-%d") if birth_date else "", 'age': age, 'class': class_name, 'admission_year': admission_year }, 'score': { 'math': math_score, 'python': python_score, 'english': english_score, 'physics': physics_score, 'sum': total_score } } try: if self.student_service.add_student(student): print("学生信息添加成功!") except Exception as e: print(f"添加学生失败: {e}") def update_student(self): """修改学生信息""" student_id = input("请输入要修改的学生学号: ") student = self.student_service.search_student_by_id(student_id) if not student: print("未找到该学号的学生!") return print("\n当前学生信息:") print("基本信息:") for key, value in student['basic_info'].items(): print(f" {key}: {value}") print("\n成绩信息:") for subject, score in student['score'].items(): if subject in self.student_service.subject_name: print(f" {self.student_service.subject_name[subject]}: {score}") print("\n请输入新的信息(直接回车保持原值):") # 更新基本信息 basic_info = student['basic_info'] for field in ['name', 'class', 'admission_year']: new_value = input(f"{field}(当前:{basic_info[field]}): ").strip() if new_value: basic_info[field] = new_value # 更新身份证号 while True: new_id_card = input(f"身份证号(当前:{basic_info.get('id_card', '')}): ").strip().upper() if not new_id_card: # 直接回车保持原值 break is_valid, msg = self.student_service.validate_id_card(new_id_card) if not is_valid: print(f"身份证号无效: {msg}") continue if self.student_service.is_id_card_exists(new_id_card, exclude_id=student_id): print("该身份证号已被其他学生使用!") continue basic_info['id_card'] = new_id_card # 自动更新性别和出生日期 basic_info['gender'] = IDCardValidator.get_gender(new_id_card) birth_date = IDCardValidator.get_birth_date(new_id_card) if birth_date: basic_info['birth_date'] = birth_date.strftime("%Y-%m-%d") basic_info['age'] = IDCardValidator.calculate_age(new_id_card) break # 更新成绩 scores = student['score'] for subject in ['math', 'python', 'english', 'physics']: new_score = input(f"{self.student_service.subject_name[subject]}(当前:{scores[subject]}): ").strip() if new_score: scores[subject] = float(new_score) scores['sum'] = sum([scores['math'], scores['python'], scores['english'], scores['physics']]) try: if self.student_service.update_student(student_id, student): print("学生信息修改成功!") except Exception as e: print(f"修改学生信息失败: {e}") def delete_student(self): """删除学生""" student_id = input("请输入要删除的学生学号: ") confirm = input(f"确定要删除学号为 {student_id} 的学生吗?(y/n): ").lower() if confirm == 'y': try: if self.student_service.delete_student(student_id): print("学生信息删除成功!") except Exception as e: print(f"删除学生失败: {e}") def show_statistics(self): """显示统计信息""" stats = self.student_service.get_statistics() if not stats: print("当前没有学生信息!") return print("\n=== 统计信息 ===") print(f"学生总数: {stats['total']}") print(f"数学平均分: {stats['math_avg']:.2f}") print(f"Python平均分: {stats['python_avg']:.2f}") print(f"英语平均分: {stats['english_avg']:.2f}") print(f"物理平均分: {stats['physics_avg']:.2f}") print(f"总分平均分: {stats['total_avg']:.2f}") def show_sorted_students(self): """按总分排序显示""" sorted_students = self.student_service.get_sorted_students() if not sorted_students: print("当前没有学生信息!") return print("\n=== 按总分排序 ===") print("排名\t学号\t姓名\t总分") for rank, student in enumerate(sorted_students, 1): print(f"{rank}\t{student['basic_info']['id']}\t{student['basic_info']['name']}\t{student['score']['sum']}") def validate_id_card(self): """验证身份证号""" id_card = input("请输入要验证的身份证号: ").strip().upper() is_valid, msg = IDCardValidator.validate_id_card(id_card) if is_valid: print("\n=== 身份证号有效 ===") print(f"号码: {id_card}") print(f"性别: {IDCardValidator.get_gender(id_card)}") birth_date = IDCardValidator.get_birth_date(id_card) if birth_date: print(f"出生日期: {birth_date.strftime('%Y-%m-%d')}") print(f"年龄: {IDCardValidator.calculate_age(id_card)}岁") print("校验码验证通过") else: print(f"\n身份证号无效: {msg}") if __name__ == "__main__": system = StudentManagementSystem() system.run()