forked from p3qolzsn9/stu1
parent
164a21006e
commit
2b8b420d84
@ -0,0 +1,11 @@
|
||||
class StudentDal:
|
||||
def __init__(self,file_path:str):
|
||||
self.file_path=file_path
|
||||
self._ensure_file_exists()
|
||||
|
||||
|
||||
|
||||
def _ensure_file_exists(self):
|
||||
if not os.path.exists(self.file_path):
|
||||
with open(self.file_path,mode='w',encoding='utf-8')as file:
|
||||
json.dump([],file)
|
@ -0,0 +1,8 @@
|
||||
def add_student(self,student:Student)-> bool:
|
||||
students=self.load_students()
|
||||
for existing_student in students:
|
||||
if existing_student.sid==student.sid:
|
||||
return False
|
||||
students.append(student)
|
||||
self.save_students(students)
|
||||
return True
|
@ -0,0 +1,2 @@
|
||||
def check_students_exists(self,sid:str)-> bool:
|
||||
return self.find_studemts_by_sid(sid) is not None
|
@ -0,0 +1,6 @@
|
||||
def clear_all_students(self)-> None:
|
||||
self.save_students([])
|
||||
def get_student_count(self)->int:
|
||||
return len(self.load_students())
|
||||
def check_student_exists(self,sid:str)-> bool:
|
||||
return self.find_student_by_sid(sid) is not None
|
@ -0,0 +1,7 @@
|
||||
def delete_student(self,sid:str)-> bool:
|
||||
students= self.load_students()
|
||||
initial_length=len(students)
|
||||
students=[student for student in students if student.sid!= sid]
|
||||
if len(students)<initial_length:
|
||||
return True
|
||||
return False
|
@ -0,0 +1,3 @@
|
||||
def find_student_by_class_name(self,class_name:str)-> Lise[Student]:
|
||||
students = self.load_students()
|
||||
return[s for s in students if class_name.lower()in s.class_name.lower()]
|
@ -0,0 +1,3 @@
|
||||
def find_students_by_name(self,name:str)-> List[Student]:
|
||||
students =self.load_students()
|
||||
return [student for student in students if name.lower() in student.name.lower()]
|
@ -0,0 +1,2 @@
|
||||
def get_all_students(self)-> List[Student]:
|
||||
return self.load_students()
|
@ -0,0 +1,2 @@
|
||||
def get_student_count(self)-> int:
|
||||
return len(self.load_students())
|
@ -0,0 +1,67 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import List, Optional
|
||||
from models.student import Student
|
||||
|
||||
|
||||
class IStudentDAL(ABC):
|
||||
"""学生信息数据访问层接口"""
|
||||
|
||||
@abstractmethod
|
||||
def add_student(self, student: Student) -> bool:
|
||||
"""添加学生"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def delete_student(self, id_card: str) -> bool:
|
||||
"""删除学生"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def update_student(self, student: Student) -> bool:
|
||||
"""更新学生信息"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_by_id(self, id_card: str) -> Optional[Student]:
|
||||
"""根据身份证号获取学生"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_by_stu_id(self, stu_id: str) -> Optional[Student]:
|
||||
"""根据学号获取学生"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_all(self) -> List[Student]:
|
||||
"""获取所有学生"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_by_name(self, name: str) -> List[Student]:
|
||||
"""按姓名查询(模糊)"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_by_class(self, class_name: str) -> List[Student]:
|
||||
"""按班级查询(模糊)"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_by_major(self, major: str) -> List[Student]:
|
||||
"""按专业查询(模糊)"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def count_students(self) -> int:
|
||||
"""统计学生总数"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def count_by_major(self) -> dict:
|
||||
"""按专业统计人数"""
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def clear_all(self) -> bool:
|
||||
"""清空所有学生数据"""
|
||||
pass
|
@ -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):
|
||||
|
||||
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
|
@ -0,0 +1,4 @@
|
||||
def load_students(self)-> List[Student]:
|
||||
with open(self.file_path,mode='r',encoding='utf-8')as file:
|
||||
student_dicts=json.load(file)
|
||||
return[Student.from_dict(student_dict) for student_dict in student_dicts]
|
@ -0,0 +1,4 @@
|
||||
def save_students(self,students:List[Student])-> None:
|
||||
student_dicts=[student.to_dict() for student in students]
|
||||
with open(self.file_path,mode="w",encoding="utf-8") as file:
|
||||
json.dump(student_dicts,file,ensure_ascii=False,indent=4)
|
@ -0,0 +1,24 @@
|
||||
import json
|
||||
|
||||
from typing_extensions import Optional
|
||||
from xlwings.pro.reports.filters import height
|
||||
|
||||
from 面向对象学生系统.model.test.student import Student
|
||||
class StudentDAL:
|
||||
def __init__(self,file_path:str):
|
||||
self.file_path=file_path
|
||||
self._ensure_file_exists()
|
||||
def _ensure_file_exist(self):
|
||||
if not os.path.exists(self.file_path):
|
||||
with open(self,file_path,mode='w',encoding='utf-8') as file:
|
||||
json.dump([],file)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,8 @@
|
||||
def update_student(self,sid:str,updated_student:Student)-> bool:
|
||||
students =self.load_students()
|
||||
for i, student in enumerate(students):
|
||||
if student.sid== sid:
|
||||
student[i]=upadted_student
|
||||
self.save_students(students)
|
||||
return True
|
||||
return False
|
@ -0,0 +1,18 @@
|
||||
def update_student_partial(self, sid: str, name: Optional[str] = None, height:Optional[float]=None, birth_date: Optional[date] = None, enrollment_date:Optional[date] = None, class_name:Optional[
|
||||
str] = None)->bool:
|
||||
students = self.load_students()
|
||||
for i, student in enumerate(students):
|
||||
if students.sid == sid:
|
||||
if name is not None:
|
||||
students[i].name = name
|
||||
if height is not None:
|
||||
students[i].height = height
|
||||
if birth_date is not None:
|
||||
students[i].birth_date = birth_date
|
||||
if enrollemnt_date is not None:
|
||||
students[i].enrollemnt_date = enrollemnt_date
|
||||
if class_name is not None:
|
||||
students[i].class_name = class_name
|
||||
self.save_students(students)
|
||||
return True
|
||||
return False
|
@ -0,0 +1,3 @@
|
||||
class StudentTUI:
|
||||
def __init__(self,bll:StudentBLL):
|
||||
self.bll=bll
|
@ -0,0 +1,88 @@
|
||||
import json
|
||||
|
||||
from typing_extensions import Optional
|
||||
from xlwings.pro.reports.filters import height
|
||||
|
||||
from 面向对象学生系统.model.test.student import Student
|
||||
class StudentDAL:
|
||||
def __init__(self,file_path:str):
|
||||
self.file_path=file_path
|
||||
self._ensure_file_exists()
|
||||
def _ensure_file_exist(self):
|
||||
if not os.path.exists(self.file_path):
|
||||
with open(self,file_path,mode='w',encoding='utf-8') as file:
|
||||
json.dump([],file)
|
||||
def save_students(self,students:List[Student])-> None:
|
||||
student_dicts=[student.to_dict() for student in students]
|
||||
with open(self.file_path,mode="w",encoding="utf-8") as file:
|
||||
json.dump(student_dicts,file,ensure_ascii=False,indent=4)
|
||||
def load_students(self)-> List[Student]:
|
||||
with open(self.file_path,mode='r',encoding='utf-8')as file:
|
||||
student_dicts=json.load(file)
|
||||
return[Student.from_dict(student_dict) for student_dict in student_dicts]
|
||||
def add_student(self,student:Student)-> bool:
|
||||
students=self.load_students()
|
||||
for existing_student in students:
|
||||
if existing_student.sid==student.sid:
|
||||
return False
|
||||
students.append(student)
|
||||
self.save_students(students)
|
||||
return True
|
||||
def find_student_by_sid(self,sid:str)-> Optional[Student]:
|
||||
students =self.load_students()
|
||||
for student in students:
|
||||
if student.sid==sid:
|
||||
return student
|
||||
return None
|
||||
def find_students_by_name(self,name:str)-> List[Student]:
|
||||
students =self.load_students()
|
||||
return [student for student in students if name.lower() in student.name.lower()]
|
||||
def find_student_by_class_name(slef,class_nmae:str)-> Lise[Student]:
|
||||
students = self.load_students()
|
||||
return[s for s in students if class_name.lower()in s.class_name.lower()]
|
||||
def get_all_students(self)-> List[Student]:
|
||||
return self.load_students()
|
||||
def update_student(self,sid:str,upadted_student:Student)-> bool:
|
||||
students =self.load_students()
|
||||
for i, student in enumerate(students):
|
||||
if student.sid== sid:
|
||||
student[i]=upadted_student
|
||||
self.save_students(students)
|
||||
return True
|
||||
return False
|
||||
def update_student_partial(self,sid:str,name:Optional[str]=None,height=Optional[float]=None,birth_date:Optional[date]=None,enrollment_date:Optional[date]=None,class_name:Optional[str]=None)->bool:
|
||||
students= self.load_students()
|
||||
for i,student in enumerate(students):
|
||||
if students.sid==sid:
|
||||
|
||||
if name is not None:
|
||||
students[i].name=name
|
||||
if height is not None:
|
||||
students[i].height=height
|
||||
if birth_date is not None:
|
||||
students[i].birth_date=birth_date
|
||||
if enrollemnt_date is not None:
|
||||
students[i].enrollemnt_date=enrollemnt_date
|
||||
if class_name is not None:
|
||||
students[i].class_name=class_name
|
||||
self.save_students(students)
|
||||
return True
|
||||
return False
|
||||
def delete_student(self,sid:str)-> bool:
|
||||
students= self.load_students()
|
||||
initial_length=len(students)
|
||||
students=[student for student in students if student.sid!= sid]
|
||||
if len(students)<initial_length:
|
||||
return True
|
||||
return False
|
||||
def clear_all_students(self)-> None:
|
||||
self.save_students([])
|
||||
def get_student_count(self)->int:
|
||||
return len(self.load_students())
|
||||
def check_student_exists(self,sid:str)-> bool:
|
||||
return self.find_student_by_sid(sid) is not None
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
class StudentBll:
|
||||
def __init__(self,dal:StudentDal):
|
||||
self.dal=dal
|
||||
def delete_student(self,sid:str)-> bool:
|
||||
if not self.dal.check_student_exists(sid):
|
||||
raise ValueError(f"学生 ID {sid}不存在。")
|
||||
return self.dal.delete_students(sid)
|
Loading…
Reference in new issue