Compare commits
No commits in common. 'master' and 'main' have entirely different histories.
@ -1,60 +0,0 @@
|
|||||||
import json
|
|
||||||
import os
|
|
||||||
from typing import Dict, Any
|
|
||||||
|
|
||||||
|
|
||||||
class StudentDataAccess:
|
|
||||||
def __init__(self, file_path: str = 'student_data.json'):
|
|
||||||
self.file_path = file_path
|
|
||||||
|
|
||||||
def load_data(self) -> Dict[str, Any]:
|
|
||||||
"""从文件加载所有学生数据"""
|
|
||||||
try:
|
|
||||||
if os.path.exists(self.file_path):
|
|
||||||
with open(self.file_path, 'r', encoding='utf-8') as f:
|
|
||||||
return json.load(f)
|
|
||||||
return {}
|
|
||||||
except Exception as e:
|
|
||||||
raise Exception(f"加载数据时出错: {e}")
|
|
||||||
|
|
||||||
def save_data(self, data: Dict[str, Any]) -> bool:
|
|
||||||
"""保存所有学生数据到文件"""
|
|
||||||
try:
|
|
||||||
with open(self.file_path, 'w', encoding='utf-8') as f:
|
|
||||||
json.dump(data, f, ensure_ascii=False, indent=4)
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
raise Exception(f"保存数据时出错: {e}")
|
|
||||||
|
|
||||||
def get_student(self, student_id: str) -> Dict[str, Any]:
|
|
||||||
"""根据学号获取单个学生信息"""
|
|
||||||
data = self.load_data()
|
|
||||||
return data.get(student_id)
|
|
||||||
|
|
||||||
def get_all_students(self) -> Dict[str, Any]:
|
|
||||||
"""获取所有学生信息"""
|
|
||||||
return self.load_data()
|
|
||||||
|
|
||||||
def add_student(self, student_id: str, student_info: Dict[str, Any]) -> bool:
|
|
||||||
"""添加新学生"""
|
|
||||||
data = self.load_data()
|
|
||||||
if student_id in data:
|
|
||||||
raise Exception("该学号已存在!")
|
|
||||||
data[student_id] = student_info
|
|
||||||
return self.save_data(data)
|
|
||||||
|
|
||||||
def update_student(self, student_id: str, student_info: Dict[str, Any]) -> bool:
|
|
||||||
"""更新学生信息"""
|
|
||||||
data = self.load_data()
|
|
||||||
if student_id not in data:
|
|
||||||
raise Exception("该学号不存在!")
|
|
||||||
data[student_id] = student_info
|
|
||||||
return self.save_data(data)
|
|
||||||
|
|
||||||
def delete_student(self, student_id: str) -> bool:
|
|
||||||
"""删除学生"""
|
|
||||||
data = self.load_data()
|
|
||||||
if student_id not in data:
|
|
||||||
raise Exception("该学号不存在!")
|
|
||||||
del data[student_id]
|
|
||||||
return self.save_data(data)
|
|
||||||
Loading…
Reference in new issue