Advertisement
meteor4o

JF-Maps-Exercise-10.SoftUniExamResults

Jul 16th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.06 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class SoftUniExamResults_2 {
  7.     public static void main(String[] args) {
  8.         Scanner sc = new Scanner(System.in);
  9.  
  10.         TreeMap<String,Integer> languages = new TreeMap<>();
  11.         TreeMap<String, Integer> users = new TreeMap<>();
  12.  
  13.         String input = sc.nextLine();
  14.  
  15.         while (!input.toLowerCase().equals("exam finished")) {
  16.             String[] tokens = input.split("\\-");
  17.             String username = tokens[0];
  18.             String language = tokens[1];
  19.  
  20.             if (language.equals("banned")) {
  21.                 users.remove(username);
  22.                 input = sc.nextLine();
  23.                 continue;
  24.             }
  25.             int points = Integer.parseInt(tokens[2]);
  26.  
  27.             users.putIfAbsent(username, points);
  28.             if (users.containsKey(username)) {
  29.                 if (users.get(username) < points) {
  30.                     users.put(username, points);
  31.                 }
  32.             }
  33.  
  34.             if (languages.containsKey(language)) {
  35.                 int count = languages.get(language) + 1;
  36.                 languages.put(language, count);
  37.             } else {
  38.                 languages.put(language, 1);
  39.             }
  40.  
  41.             input = sc.nextLine();
  42.         }
  43.         System.out.println("Results:");
  44.         users.entrySet().stream().sorted((a,b) -> {
  45.             int result = b.getValue() - a.getValue();
  46.             if (result==0) {
  47.                 result = a.getKey().compareTo(b.getKey());
  48.             }
  49.             return result;
  50.         }).forEach(entry -> System.out.printf("%s | %d%n", entry.getKey(), entry.getValue()));
  51.  
  52.         System.out.println("Submissions:");
  53.         languages.entrySet().stream().sorted((a,b) -> {
  54.             int result = b.getValue() - a.getValue();
  55.             if (result==0) {
  56.                 result = a.getKey().compareTo(b.getKey());
  57.             }
  58.             return result;
  59.         }).forEach(entry -> System.out.printf("%s - %d%n", entry.getKey(), entry.getValue()));
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement