diff --git a/面向对象学生系统/ui/__init__.py b/面向对象学生系统/ui/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/面向对象学生系统/utils/validator.py b/面向对象学生系统/utils/validator.py new file mode 100644 index 0000000..70bd7bc --- /dev/null +++ b/面向对象学生系统/utils/validator.py @@ -0,0 +1,117 @@ +import json +import os +from typing import List, Optional +from models.student import Student +from dal.idal import IStudentDAL + + +class JsonStudentDAL(IStudentDAL): + """JSON存储实现""" + + def __init__(self, file_path: str = 'data/students.json'): + self.file_path = file_path + self._ensure_file_exists() + + def _ensure_file_exists(self): + """确保JSON文件存在""" + os.makedirs(os.path.dirname(self.file_path), exist_ok=True) + if not os.path.exists(self.file_path): + with open(self.file_path, 'w', encoding='utf-8') as f: + json.dump([], f) + + def _load_data(self) -> List[dict]: + """加载JSON数据""" + with open(self.file_path, 'r', encoding='utf-8') as f: + return json.load(f) + + def _save_data(self, data: List[dict]): + """保存数据到JSON文件""" + with open(self.file_path, 'w', encoding='utf-8') as f: + json.dump(data, f, ensure_ascii=False, indent=2) + + def add_student(self, student: Student) -> bool: + data = self._load_data() + + # 检查学号和身份证号是否已存在 + for item in data: + if item['id_card'] == student.id_card or item['stu_id'] == student.stu_id: + return False + + data.append(student.to_dict()) + self._save_data(data) + return True + + def delete_student(self, id_card: str) -> bool: + data = self._load_data() + original_length = len(data) + + data = [item for item in data if item['id_card'] != id_card] + + if len(data) != original_length: + self._save_data(data) + return True + return False + + def update_student(self, student: Student) -> bool: + data = self._load_data() + updated = False + + for i, item in enumerate(data): + if item['id_card'] == student.id_card: + data[i] = student.to_dict() + updated = True + break + + if updated: + self._save_data(data) + return updated + + def get_by_id(self, id_card: str) -> Optional[Student]: + data = self._load_data() + for item in data: + if item['id_card'] == id_card: + return Student.from_dict(item) + return None + + def get_by_stu_id(self, stu_id: str) -> Optional[Student]: + data = self._load_data() + for item in data: + if item['stu_id'] == stu_id: + return Student.from_dict(item) + return None + + def get_all(self) -> List[Student]: + data = self._load_data() + return [Student.from_dict(item) for item in data] + + def get_by_name(self, name: str) -> List[Student]: + data = self._load_data() + return [Student.from_dict(item) for item in data if name.lower() in item['name'].lower()] + + def get_by_class(self, class_name: str) -> List[Student]: + data = self._load_data() + return [Student.from_dict(item) for item in data + if item['class_name'] and class_name.lower() in item['class_name'].lower()] + + def get_by_major(self, major: str) -> List[Student]: + data = self._load_data() + return [Student.from_dict(item) for item in data + if item['major'] and major.lower() in item['major'].lower()] + + def count_students(self) -> int: + data = self._load_data() + return len(data) + + def count_by_major(self) -> dict: + data = self._load_data() + majors = {} + + for item in data: + major = item.get('major', '未指定') + majors[major] = majors.get(major, 0) + 1 + + return majors + + def clear_all(self) -> bool: + self._save_data([]) + return True \ No newline at end of file