diff --git a/ui/add_student.py b/ui/add_student.py index dc9e64e..56c2ad7 100644 --- a/ui/add_student.py +++ b/ui/add_student.py @@ -80,10 +80,50 @@ def display_students(students: List[Student]): print(f"共找到 {len(students)} 名学生") - class StudentTUI: def __init__(self, bll: StudentBLL): self.bll = bll + # 省份代码映射表 + self.province_codes = { + '11': '北京市', '12': '天津市', '13': '河北省', '14': '山西省', '15': '内蒙古自治区', + '21': '辽宁省', '22': '吉林省', '23': '黑龙江省', '31': '上海市', '32': '江苏省', + '33': '浙江省', '34': '安徽省', '35': '福建省', '36': '江西省', '37': '山东省', + '41': '河南省', '42': '湖北省', '43': '湖南省', '44': '广东省', '45': '广西壮族自治区', + '46': '海南省', '50': '重庆市', '51': '四川省', '52': '贵州省', '53': '云南省', + '54': '西藏自治区', '61': '陕西省', '62': '甘肃省', '63': '青海省', '64': '宁夏回族自治区', + '65': '新疆维吾尔自治区' + } + + def validate_id_card(self, id_card: str) -> bool: + """验证身份证号是否合法""" + if len(id_card) != 18: + return False + + # 校验前17位是否为数字 + if not id_card[:-1].isdigit(): + return False + + # 校验最后一位是否为数字或X + if not (id_card[-1].isdigit() or id_card[-1].upper() == 'X'): + return False + + return True + + def get_gender_from_id_card(self, id_card: str) -> bool: + """从身份证号获取性别(男True/女False)""" + if len(id_card) != 18: + raise ValueError("身份证号长度不正确") + + gender_code = int(id_card[16]) + return gender_code % 2 == 1 # 奇数表示男性,偶数表示女性 + + def get_hometown_from_id_card(self, id_card: str) -> str: + """从身份证号获取籍贯""" + if len(id_card) != 18: + raise ValueError("身份证号长度不正确") + + province_code = id_card[:2] + return self.province_codes.get(province_code, "未知地区") def run(self): while True: @@ -115,8 +155,21 @@ class StudentTUI: try: name = input("姓名: ").strip() id_card = input("身份证号: ").strip() + + # 验证身份证号 + if not self.validate_id_card(id_card): + print("身份证号不合法!") + return + + # 自动获取性别 + gender = self.get_gender_from_id_card(id_card) + print(f"自动识别性别: {'男' if gender else '女'}") + + # 自动获取籍贯 + hometown = self.get_hometown_from_id_card(id_card) + print(f"籍贯: {hometown}") + stu_id = input("学号: ").strip() - gender = input("性别(男/女): ").strip().lower() == '男' height = int(input("身高(cm): ").strip()) weight = float(input("体重(kg): ").strip()) enrollment_date = input("入学日期(YYYY-MM-DD): ").strip() @@ -191,8 +244,6 @@ class StudentTUI: print("\n请输入要更新的字段 (留空表示不更新):") name = input(f"姓名({student.name}): ").strip() or student.name stu_id = input(f"学号({student.stu_id}): ").strip() or student.stu_id - gender = input(f"性别(男/女)({'男' if student.gender else '女'}): ").strip() - gender = gender.lower() == '男' if gender else student.gender height = input(f"身高({student.height}): ").strip() height = int(height) if height else student.height weight = input(f"体重({student.weight}): ").strip() @@ -208,7 +259,7 @@ class StudentTUI: name=name, id_card=id_card, # 身份证号不可更改 stu_id=stu_id, - gender=gender, + gender=student.gender, # 性别从身份证号自动获取,不可更改 height=height, weight=weight, enrollment_date=enrollment_date, @@ -239,6 +290,9 @@ class StudentTUI: id_card = input("请输入身份证号: ").strip() student = self.bll.get_student_by_id_card(id_card) if student: + # 显示籍贯信息 + hometown = self.get_hometown_from_id_card(student.id_card) + print(f"\n籍贯: {hometown}") display_students([student]) else: print("未找到匹配的学生") @@ -457,32 +511,7 @@ def main(): # 创建用户界面 tui = StudentTUI(bll) tui.run() -def fun(self): - """运行UI""" - while True: - self.display_menu() - choice = input("请选择操作: ").strip() - if choice == '1': - self.add_student() - elif choice == '2': - self.delete_student() - - elif choice == '3': - self.update_student() - - elif choice == '5': - self.show_stats() - elif choice == '6': - self.import_export_data() - elif choice == '7': - self.clear_students() - elif choice == '8': - self.query_student() - elif choice == '4': - print("感谢使用学生信息管理系统!") - break - else: - print("无效的选择,请重新输入!") + if __name__ == "__main__": main() \ No newline at end of file