Didart

SoftUni Exam Results

Nov 8th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import com.sun.security.jgss.GSSUtil;
  2.  
  3. import java.util.*;
  4.  
  5. public class SoftUniExamResults {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.        
  9.         Map<String, Integer> userPoints = new HashMap<>();
  10.      
  11.         Map<String, Integer> languageCount = new HashMap<>();
  12.         String input = scanner.nextLine();
  13.  
  14.         while (!input.equals("exam finished")) {
  15.             String[] splitArray = input.split("-");
  16.          
  17.             String username = splitArray[0];
  18.             if (splitArray.length == 3) {
  19.                 String language = splitArray[1];
  20.                 int points = Integer.parseInt(splitArray[2]);
  21.                
  22.                 if (!userPoints.containsKey(username)) {
  23.                     userPoints.put(username, points);
  24.                 } else {
  25.                     int currentPoints = userPoints.get(username);
  26.                     if (points > currentPoints) {
  27.                         userPoints.put(username, points);
  28.                     }
  29.                 }
  30.  
  31.                 if (!languageCount.containsKey(language)) {
  32.                     languageCount.put(language, 1);
  33.                 } else {
  34.                     languageCount.put(language, languageCount.get(language) + 1);
  35.                 }
  36.  
  37.             } else {
  38.                 userPoints.remove(username);
  39.             }
  40.             input = scanner.nextLine();
  41.         }
  42.  
  43.         System.out.println("Results:");
  44.         userPoints.entrySet().stream()
  45.                 .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()
  46.                         .thenComparing(Map.Entry.comparingByKey()))
  47.                 .forEach(e -> System.out.println(e.getKey() + " | " + e.getValue()));
  48.  
  49.         System.out.println("Submissions:");
  50.         languageCount.entrySet().stream()
  51.                 .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()
  52.                         .thenComparing(Map.Entry.comparingByKey()))
  53.                 .forEach(e -> System.out.println(e.getKey() + " - " + e.getValue()));
  54.  
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment