55 lines
1.2 KiB
Java
55 lines
1.2 KiB
Java
package fr.gasser.daoexample.dao;
|
|
|
|
import fr.gasser.daoexample.model.Student;
|
|
import fr.gasser.daoexample.sql.Connection;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.List;
|
|
|
|
public class StudentDao extends Dao<Student> {
|
|
|
|
// Emulate student table
|
|
private final static List<Student> students = new ArrayList<>(Arrays.asList(
|
|
new Student(0, "Azerty", "Uiop"),
|
|
new Student(1, "Foo", "Bar"),
|
|
new Student(2, "Qsdf", "Jklm"))
|
|
);
|
|
|
|
StudentDao(Connection connection) {
|
|
super(connection);
|
|
}
|
|
|
|
@Override
|
|
public boolean create(Student obj) {
|
|
students.add(obj);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public Student find(int id) {
|
|
return students.get(id);
|
|
}
|
|
|
|
@Override
|
|
public List<Student> findAll() {
|
|
return students;
|
|
}
|
|
|
|
@Override
|
|
public boolean update(Student obj) {
|
|
if (!containsId(obj.getId())) return false;
|
|
students.set(obj.getId(), obj);
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean delete(Student obj) {
|
|
return students.remove(obj);
|
|
}
|
|
|
|
private boolean containsId(int id) {
|
|
return students.stream().anyMatch(student -> id == student.getId());
|
|
}
|
|
}
|