ChonoChonovUk

SoftUni Exam Results

Nov 10th, 2019
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class SoftUniExamSubmissions {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         Map<String,Integer> languageSubmissions = new LinkedHashMap<>();
  7.         Map<String, List<Integer>> namePoints = new LinkedHashMap<>();
  8.         String command;
  9.         while (!"exam finished".equals(command = scanner.nextLine())){
  10.             String[] nameLangPoint = command.split("-");
  11.             String name = nameLangPoint[0];
  12.             String lang = nameLangPoint[1];
  13.             if (nameLangPoint.length > 2) {
  14.                 Integer points = Integer.parseInt(nameLangPoint[2]);
  15.                 languageSubmissions.merge(lang, 1, Integer::sum);
  16.                 List<Integer> nP = namePoints.computeIfAbsent(name, k -> new ArrayList<>());
  17.                 nP.add(points);
  18.             }else {
  19.                 namePoints.remove(name);
  20.             }
  21.  
  22.         }
  23.  
  24.  
  25.         for (Map.Entry<String, List<Integer>> stuPoint : namePoints.entrySet()) {
  26.             Collections.reverse(stuPoint.getValue());
  27.         }
  28.  
  29.  
  30.         System.out.println("Results:");
  31.         namePoints.entrySet().stream().sorted((a1,a2) -> {
  32.             int res = Integer.compare(a2.getValue().get(0),a1.getValue().get(0));
  33.             if (res == 0){
  34.                 res = a1.getKey().compareTo(a2.getKey());
  35.             }
  36.             return res;
  37.         }).forEach(c ->
  38.                 System.out.printf("%s | %d%n",c.getKey(),c.getValue().get(0)));
  39.  
  40.         System.out.println("Submissions:");
  41.         languageSubmissions.entrySet().stream().sorted((a1,a2) -> {
  42.             int res = Integer.compare(a2.getValue(),a1.getValue());
  43.             if (res == 0){
  44.                 res = a1.getKey().compareTo(a2.getKey());
  45.             }
  46.             return res;
  47.         }).forEach(f ->
  48.                 System.out.printf("%s - %d%n",f.getKey(),f.getValue()));
  49.  
  50.  
  51.  
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment