Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 102 additions & 5 deletions src/main/java/com/kpi/fict/DefaultStudentService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.kpi.fict;

import com.kpi.fict.entities.Exam;
import com.kpi.fict.entities.Student;
import com.kpi.fict.repositories.StudentRepository;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class DefaultStudentService implements StudentService {
private StudentRepository studentRepository;
Expand All @@ -14,28 +18,121 @@ public DefaultStudentService(StudentRepository studentRepository) {

@Override
public Student findStudentWithMaxAvgExamRating() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.max((a, b) -> {
double r1 = a
.getExams()
.stream()
.mapToDouble(Exam::getScore)
.average()
.orElse(0.);
double r2 = b
.getExams()
.stream()
.mapToDouble(Exam::getScore)
.average()
.orElse(0.);
return Double.compare(r1, r2);
})
.orElse(studentRepository.findAll().get(0));
}

@Override
public List<Student> findStudentsWithMathRatingMoreThanAvgAndTakeEngExam() {
throw new UnsupportedOperationException("Need to make implementation");
double avg = studentRepository
.findAll()
.stream()
.flatMap(s -> s.getExams().stream())
.filter(e -> e.getType() == Exam.Type.MATH)
.mapToDouble(Exam::getScore)
.average()
.orElse(0.);
return studentRepository
.findAll()
.stream()
.filter(s ->
s
.getExams()
.stream()
.filter(e -> e.getType() == Exam.Type.MATH)
.findFirst()
.orElse(new Exam(Exam.Type.MATH, 0))
.getScore() > avg &&
s
.getExams()
.stream()
.anyMatch(e -> e.getType() == Exam.Type.ENGLISH))
.collect(Collectors.toList());
}

// c
//Delimiter: ','
@Override
public List<String> getExamSumAndRatingForEachStudent() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.map(s -> {
Optional<Double> sum = s
.getExams()
.stream()
.map(Exam::getScore)
.reduce(Double::sum);
double _sum = sum.isPresent() ? sum.get() : 0;
return _sum + "," + s.getRating() + "," + s.getName();
})
.collect(Collectors.toList());
}

@Override
public List<Student> findStudentsWhoTakeMathEngExamWith180RatingOrMore() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.filter(s ->
s
.getExams()
.stream()
.filter(e -> e.getType() == Exam.Type.MATH)
.findFirst()
.orElse(new Exam(Exam.Type.MATH, 0))
.getScore() > 180 &&
s
.getExams()
.stream()
.filter(e -> e.getType() == Exam.Type.ENGLISH)
.findFirst()
.orElse(new Exam(Exam.Type.ENGLISH, 0))
.getScore() > 180
)
.collect(Collectors.toList());
}

@Override
public List<Student> findTwoStudentsWithMaxEngRating() {
throw new UnsupportedOperationException("Need to make implementation");
return studentRepository
.findAll()
.stream()
.sorted((a, b) -> {
Optional<Exam> aa = a
.getExams()
.stream()
.filter(e -> e.getType() == Exam.Type.ENGLISH)
.findFirst();
Optional<Exam> bb = b
.getExams()
.stream()
.filter(e -> e.getType() == Exam.Type.ENGLISH)
.findFirst();
double _aa = aa.map(Exam::getScore).orElse(0.0);
double _bb = bb.map(Exam::getScore).orElse(0.0);
return Double.compare(_bb, _aa);
})
.limit(2)
.sorted(Comparator.comparing(Student::getName))
.collect(Collectors.toList());
}

public StudentRepository getStudentRepository() {
Expand Down