Advertisement
mark79

Java Fundamentals - Ranking

Jul 28th, 2019
1,693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.84 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Ranking {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         LinkedHashMap<String, String> contests = new LinkedHashMap<>();
  9.         TreeMap<String, LinkedHashMap<String, Integer>> users = new TreeMap<>();
  10.  
  11.         String input = scanner.nextLine();
  12.         while (!input.equals("end of contests")) {
  13.             String[] tokens = input.split(":");
  14.             String contest = tokens[0];
  15.             String password = tokens[1];
  16.  
  17.             if (!contests.containsKey(contest)) {
  18.                 contests.put(contest, password);
  19.             } else {
  20.                 contests.put(contest, password);
  21.             }
  22.  
  23.             input = scanner.nextLine();
  24.         }
  25.  
  26.         input = scanner.nextLine();
  27.         while (!input.equals("end of submissions")) {
  28.             String[] tokens = input.split("=>");
  29.             String contest = tokens[0];
  30.             String password = tokens[1];
  31.             String userName = tokens[2];
  32.             int userPoints = Integer.parseInt(tokens[3]);
  33.  
  34.             if (contests.containsKey(contest)) {
  35.                 if (contests.get(contest).equals(password)) {
  36.  
  37.                     LinkedHashMap<String, Integer> course = new LinkedHashMap<>();
  38.                     course.put(contest, userPoints);
  39.  
  40.                     if (!users.containsKey(userName)) {
  41.                         users.put(userName, course);
  42.                     } else {
  43.                         if (!users.get(userName).containsKey(contest)) {
  44.                             users.get(userName).put(contest, userPoints);
  45.                         } else {
  46.                             users.get(userName).put(contest, Math.max(userPoints, users.get(userName).get(contest)));
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.             input = scanner.nextLine();
  52.         }
  53.  
  54.         int totalSum = 0;
  55.         for (Map.Entry<String, LinkedHashMap<String, Integer>> user : users.entrySet()) {
  56.             int sum = user.getValue().values().stream().mapToInt(i -> i).sum();
  57.             if (sum > totalSum) {
  58.                 totalSum = sum;
  59.             }
  60.         }
  61.  
  62.         for (Map.Entry<String, LinkedHashMap<String, Integer>> user : users.entrySet()) {
  63.             if (user.getValue().values().stream().mapToInt(i -> i).sum() == totalSum) {
  64.                 System.out.printf("Best candidate is %s with total %d points.%n", user.getKey(), totalSum);
  65.             }
  66.         }
  67.  
  68.         System.out.println("Ranking:");
  69.         users.forEach((key, value) -> {
  70.             System.out.printf("%s%n", key);
  71.             value.entrySet().stream().
  72.                     sorted((f, s) -> s.getValue() - f.getValue()).
  73.                     forEach(i -> System.out.printf("#  %s -> %d%n", i.getKey(), i.getValue()));
  74.         });
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement