Advertisement
Guest User

Ranking

a guest
Mar 19th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package MoreExercise;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7.  
  8. public class Ranking_01 {
  9.     public static void main(String[] args) throws IOException {
  10.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  11.  
  12.         Map<String, String> contestsMap = new HashMap<>();
  13.  
  14.         String strInput = "";
  15.  
  16.         while (!"end of contests".equals(strInput = reader.readLine())) {
  17.  
  18.             String[] input = strInput.split(":");
  19.  
  20.             contestsMap.putIfAbsent(input[0], input[1]);
  21.         }
  22.  
  23.         Map<String, TreeMap<String, Integer>> data = new TreeMap<>();
  24.  
  25.  
  26.  
  27.         String strCommand = "";
  28.  
  29.         while (!"end of submissions".equals(strCommand = reader.readLine())) {
  30.  
  31.             String[] command = strCommand.split("=>");
  32.             String contest = command[0];
  33.             String password = command[1];
  34.             String username = command[2];
  35.             int points = Integer.parseInt(command[3]);
  36.  
  37.             if (contestsMap.containsKey(contest) && contestsMap.get(contest).equals(password)) {
  38.  
  39.                 if (!data.containsKey(username)) {
  40.  
  41.                     data.put(username, new TreeMap<>());
  42.                     data.get(username).putIfAbsent(contest, points);
  43.  
  44.                 } else {
  45.  
  46.                     data.get(username).putIfAbsent(contest, points);
  47.  
  48.                     if (data.get(username).get(contest) < points) {
  49.  
  50.                         data.get(username).put(contest, points);
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         Map<String, Integer> max = new LinkedHashMap<>();
  56.  
  57.         data.forEach((key, value) -> {
  58.             max.putIfAbsent(key, 0);
  59.  
  60.             value.forEach((key2, value2) -> max.put(key, max.get(key) + value2));
  61.         });
  62.  
  63.         int maxValueInMap = Collections.max(max.values());
  64.  
  65.         max.forEach((key, value) -> {
  66.             if (value == maxValueInMap) {
  67.                 System.out.println(String.format("Best candidate is %s with total %d points.",key, maxValueInMap));
  68.             }
  69.         });
  70.  
  71.         System.out.println("Ranking: ");
  72.  
  73.         data.forEach((key, value) -> {
  74.             System.out.println(key);
  75.  
  76.             value.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()))
  77.                     .forEach(e -> System.out.println(String.format("#  %s -> %d",e.getKey(), e.getValue())));
  78.         });
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement