Advertisement
Guest User

Ranking

a guest
Nov 6th, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. package.BasicExercise.MapsLambdaStream;
  2.  
  3. import java.util.*;
  4. import java.util.function.Function;
  5. import java.util.stream.Collectors;
  6.  
  7. public class Ranking {
  8.     public static void main(String[] args) {
  9.         Scanner scan = new Scanner(System.in);
  10.         Map<String, String> contestsPasswords = new HashMap<>();
  11.         Map<String, Map<String, Integer>> usersContestsPoints = new TreeMap<>();
  12.  
  13.         String line = scan.nextLine();
  14.         while (!line.equals("end of contests")) {
  15.             String[] tokens = line.split(":");
  16.             String contestName = tokens[0];
  17.             String contestPassword = tokens[1];
  18.             contestsPasswords.put(contestName, contestPassword);
  19.             line = scan.nextLine();
  20.         }
  21.  
  22.         line = scan.nextLine();
  23.         while (!line.equals("end of submissions")) {
  24.             String[] tokens = line.split("=>");
  25.             String contestName = tokens[0];
  26.             String contestPassword = tokens[1];
  27.             String userName = tokens[2];
  28.             int points = Integer.parseInt(tokens[3]);
  29.  
  30.             if (contestsPasswords.containsKey(contestName) &&
  31.                     contestsPasswords.get(contestName).equals(contestPassword)) {
  32.                 if (usersContestsPoints.get(userName) == null) {
  33.                     Map<String, Integer> tempUsersPoints = new HashMap<>();
  34.                     tempUsersPoints.put(contestName, points);
  35.                     usersContestsPoints.put(userName, tempUsersPoints);
  36.                 } else {
  37.                     if (!usersContestsPoints.get(userName).containsKey(contestName)) {
  38.                         usersContestsPoints.get(userName).put(contestName, points);
  39.                     } else if (usersContestsPoints.get(userName).get(contestName) < points) {
  40.                         usersContestsPoints.get(userName).put(contestName, points);
  41.                     }
  42.  
  43.                 }
  44.  
  45.  
  46.             }
  47.  
  48.  
  49.             line = scan.nextLine();
  50.         }
  51.  
  52.         Map<String, Integer> bestUserMaxPoints = new HashMap<>();
  53.         for (Map.Entry<String, Map<String, Integer>> entry : usersContestsPoints.entrySet()) {
  54.             for (Map.Entry<String, Integer> userPoints : entry.getValue().entrySet()) {
  55.                 String name = entry.getKey();
  56.                 int points = userPoints.getValue();
  57.  
  58.                 if (!bestUserMaxPoints.containsKey(name)) {
  59.                     bestUserMaxPoints.put(name, points);
  60.                 } else {
  61.                     bestUserMaxPoints.put(name, points + bestUserMaxPoints.get(name));
  62.                 }
  63.             }
  64.         }
  65.  
  66.         String name = bestUserMaxPoints.entrySet().
  67.                 stream().sorted((a, b) -> b.getValue().compareTo(a.getValue()))
  68.                 .findFirst().get().getKey();
  69.         int points = bestUserMaxPoints.entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue())).
  70.                 findFirst().get().getValue();
  71.  
  72.  
  73.         System.out.println(String.format(("Best candidate is %s with total %d points."), name, points));
  74.         System.out.println("Ranking: ");
  75.         usersContestsPoints.entrySet().stream().forEach(e->{
  76.             System.out.println(e.getKey());
  77.             e.getValue().entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue())).forEach(b -> System.out.println(String.format(("#  %s -> %d"), b.getKey(), b.getValue())));
  78.         });
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement