Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- import java.util.stream.Collectors;
- import java.util.*;
- import java.util.stream.Collectors;
- class LabExercises {
- List<Student> students;
- public LabExercises() {
- students = new ArrayList<>();
- }
- public void printByAveragePoints(boolean b, int n) {
- Comparator<Student> comparator = Comparator.comparing(Student::getAveragePoints).thenComparing(Student::getIndex);
- if(!b)
- comparator = comparator.reversed();
- students.stream().sorted(comparator)
- .limit(n)
- .forEach(i-> System.out.println(i));
- }
- public List<Student> failedStudents () {
- Comparator<Student> comparator = Comparator.comparing(Student::getIndex).thenComparing(Student::getAveragePoints);
- return students.stream().filter(i->!i.hasSignature())
- .sorted(comparator)
- .collect(Collectors.toList());
- }
- public Map<Integer,Double> getStatisticsByYear() {
- return students.stream().filter(i->i.hasSignature()).collect(Collectors.groupingBy(
- Student::getYearOfStudies,
- Collectors.averagingDouble(Student::getAveragePoints)
- ));
- }
- public void addStudent(Student student) {
- students.add(student);
- }
- }
- class Student {
- private String index;
- private List<Integer> points;
- public Student(String index, List<Integer> points) {
- this.index = index;
- this.points = points;
- }
- public boolean hasSignature(){
- return points.size()>7;
- }
- public String getIndex() {
- return index;
- }
- public List<Integer> getPoints() {
- return points;
- }
- public double getAveragePoints(){
- return points.stream().mapToDouble(p -> p).sum()/10.0;
- }
- public int getYearOfStudies(){
- return 20 - Integer.parseInt(index.substring(0,2));
- }
- @Override
- public String toString() {
- return String.format("%s %s %.2f", index, hasSignature() ? "YES" : "NO", getAveragePoints());
- }
- }
- public class LabExercisesTest {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- LabExercises labExercises = new LabExercises();
- while (sc.hasNextLine()) {
- String input = sc.nextLine();
- String[] parts = input.split("\\s+");
- String index = parts[0];
- List<Integer> points = Arrays.stream(parts).skip(1)
- .mapToInt(Integer::parseInt)
- .boxed()
- .collect(Collectors.toList());
- labExercises.addStudent(new Student(index, points));
- }
- System.out.println("===printByAveragePoints (ascending)===");
- labExercises.printByAveragePoints(true, 100);
- System.out.println("===printByAveragePoints (descending)===");
- labExercises.printByAveragePoints(false, 100);
- System.out.println("===failed students===");
- labExercises.failedStudents().forEach(System.out::println);
- System.out.println("===statistics by year");
- labExercises.getStatisticsByYear().entrySet().stream()
- .map(entry -> String.format("%d : %.2f", entry.getKey(), entry.getValue()))
- .forEach(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment