ADD file via upload

main
pm4c6ia2v 4 months ago
parent 16ee759403
commit 9778eebc31

@ -0,0 +1,97 @@
package com.student.model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
* 使
*/
public class StudentRepository {
// 使用Map存储学生数据以ID为键
private final Map<String, Student> students;
// 单例实例
private static volatile StudentRepository instance;
/**
*
*/
private StudentRepository() {
students = new HashMap<>();
// 初始化一些示例数据
addSampleData();
}
/**
*
* @return StudentRepository
*/
public static StudentRepository getInstance() {
if (instance == null) {
synchronized (StudentRepository.class) {
if (instance == null) {
instance = new StudentRepository();
}
}
}
return instance;
}
/**
*
*/
private void addSampleData() {
addStudent(new Student("1", "张三", 20, "计算机科学"));
addStudent(new Student("2", "李四", 21, "软件工程"));
addStudent(new Student("3", "王五", 19, "数据结构"));
}
/**
*
* @param student
*/
public void addStudent(Student student) {
students.put(student.getId(), student);
}
/**
* ID
* @param id ID
* @return null
*/
public Student getStudentById(String id) {
return students.get(id);
}
/**
*
* @return
*/
public List<Student> getAllStudents() {
return new ArrayList<>(students.values());
}
/**
*
* @param student
* @return
*/
public boolean updateStudent(Student student) {
if (students.containsKey(student.getId())) {
students.put(student.getId(), student);
return true;
}
return false;
}
/**
*
* @param id ID
* @return
*/
public boolean deleteStudent(String id) {
return students.remove(id) != null;
}
}
Loading…
Cancel
Save