Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2017
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.98 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         ArrayList<Student> students = new ArrayList<>();
  8.  
  9.         int n = scanner.nextInt();
  10.         scanner.nextLine();
  11.  
  12.         for (int i = 0; i < n; i++) {
  13.             String[] input = scanner.nextLine().split(" ");
  14.             String name = input[0];
  15.             ArrayList<Double> grades = new ArrayList<>();
  16.             for (int j = 1; j < input.length; j++) {
  17.                 grades.add(Double.parseDouble(input[j]));
  18.             }
  19.             students.add(new Student(name, grades));
  20.         }
  21.  
  22.         students.stream()
  23.                 .filter(s -> s.getAverage() >= 5.00)
  24.                 .sorted((a, b) -> {
  25.                     int result = a.getName().compareTo(b.getName());
  26.  
  27.                     if (result == 0) {
  28.                         result = Double.compare(b.getAverage(), a.getAverage());
  29.                     }
  30.  
  31.                     return result;
  32.                 })
  33.                 .forEach(s -> {
  34.                     System.out.printf("%s -> %.2f%n", s.getName(), s.getAverage());
  35.                 });
  36.     }
  37. }
  38.  
  39. class Student {
  40.     public Student(String name, ArrayList<Double> grades) {
  41.         this.name = name;
  42.         this.grades = grades;
  43.  
  44.         Double total =0.0;
  45.         for(Double grade : grades){
  46.             total += grade;
  47.         }
  48.         this.average = total / grades.size();
  49.     }
  50.  
  51.     public Student() {
  52.     }
  53.  
  54.     public String getName() {
  55.         return name;
  56.     }
  57.  
  58.     public ArrayList<Double> getGrades() {
  59.         return grades;
  60.     }
  61.  
  62.     public Double getAverage() {
  63.         return average;
  64.     }
  65.  
  66.     public void setName(String name) {
  67.         this.name = name;
  68.     }
  69.  
  70.     public void setGrades(ArrayList<Double> grades) {
  71.         this.grades = grades;
  72.     }
  73.  
  74.     private String name;
  75.     private ArrayList<Double> grades = new ArrayList<>();
  76.     private Double average;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement