java-tutorials/play-framework/student-api/app/models/StudentStore.java

46 lines
1.1 KiB
Java
Raw Normal View History

package models;
2016-10-04 11:54:59 -04:00
import java.util.HashMap;
2016-10-04 11:29:49 -04:00
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class StudentStore {
private static StudentStore instance;
private Map<Integer, Student> students = new HashMap<>();
public static StudentStore getInstance() {
2016-10-04 11:54:59 -04:00
if (instance == null) {
instance = new StudentStore();
2016-10-04 11:54:59 -04:00
}
return instance;
}
public Student addStudent(Student student) {
2016-10-04 11:54:59 -04:00
int id = students.size();
student.setId(id);
students.put(id, student);
return student;
}
public Student getStudent(int id) {
return students.get(id);
}
public Set<Student> getAllStudents() {
2016-10-04 11:29:49 -04:00
return new HashSet<>(students.values());
}
public Student updateStudent(Student student) {
2016-10-04 11:54:59 -04:00
int id = student.getId();
if (students.containsKey(id)) {
students.put(id, student);
return student;
}
return null;
}
public boolean deleteStudent(int id) {
2016-10-04 11:24:46 -04:00
return students.remove(id) != null;
}
}