ADD file via upload

main
pc9pizjb6 4 months ago
parent de18907404
commit c552b4c655

@ -0,0 +1,130 @@
package com.studentmanagement.controller;
import com.studentmanagement.model.Student;
import com.studentmanagement.model.StudentDAO;
import com.studentmanagement.exception.StudentNotFoundException;
import com.studentmanagement.exception.DuplicateStudentException;
import java.util.List;
/**
*
* MVCControllerModelView
*/
public class StudentController {
private StudentDAO studentDAO;
/**
*
*/
public StudentController() {
this.studentDAO = StudentDAO.getInstance();
}
/**
*
* @return
*/
public List<Student> getAllStudents() {
return studentDAO.getAllStudents();
}
/**
* ID
* @param id ID
* @return
* @throws StudentNotFoundException
*/
public Student getStudentById(int id) throws StudentNotFoundException {
Student student = studentDAO.getStudentById(id);
if (student == null) {
throw new StudentNotFoundException(id);
}
return student;
}
/**
*
* @param student
* @throws DuplicateStudentException ID
*/
public void addStudent(Student student) throws DuplicateStudentException {
boolean added = studentDAO.addStudent(student);
if (!added) {
throw new DuplicateStudentException(student.getId());
}
}
/**
*
* @param student
* @throws StudentNotFoundException
*/
public void updateStudent(Student student) throws StudentNotFoundException {
boolean updated = studentDAO.updateStudent(student);
if (!updated) {
throw new StudentNotFoundException(student.getId());
}
}
/**
*
* @param id ID
* @throws StudentNotFoundException
*/
public void deleteStudent(int id) throws StudentNotFoundException {
boolean deleted = studentDAO.deleteStudent(id);
if (!deleted) {
throw new StudentNotFoundException(id);
}
}
/**
*
* @param course
* @return
*/
public List<Student> getStudentsByCourse(String course) {
return studentDAO.getStudentsByCourse(course);
}
/**
*
* @param grade
* @return
*/
public List<Student> getStudentsByGradeAbove(double grade) {
return studentDAO.getStudentsByGradeAbove(grade);
}
/**
*
* @param course
* @return
*/
public double calculateAverageGradeByCourse(String course) {
List<Student> students = getStudentsByCourse(course);
if (students.isEmpty()) {
return 0;
}
return students.stream()
.mapToDouble(Student::getGrade)
.average()
.orElse(0);
}
/**
*
* @return
*/
public double calculateAverageAge() {
List<Student> students = getAllStudents();
if (students.isEmpty()) {
return 0;
}
return students.stream()
.mapToDouble(Student::getAge)
.average()
.orElse(0);
}
}
Loading…
Cancel
Save