Advertisement
meteor4o

JF-Maps-MoreExercise-01.Ranking

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