SHARE
TWEET

Untitled

shniaga Mar 19th, 2019 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package AssociativeArraysMoreExercise;
  2.  
  3.         import java.util.*;
  4.  
  5. public class Ranking {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         Map<String, String> contestPass = new HashMap<>();
  10.         Map<String, Map<String, Integer>> contestsWithPoints = new TreeMap<>();
  11.         Map<String, Integer> countPoints = new HashMap<>();
  12.  
  13.  
  14.         String input = "";
  15.  
  16.         while (!"end of contests".equals(input = scanner.nextLine())) {
  17.             String[] data = input.split(":");
  18.             String contest = data[0];
  19.             String password = data[1];
  20.             contestPass.put(contest, password);
  21.         }
  22.         String input2 = "";
  23.  
  24.         while (!"end of submissions".equals(input2 = scanner.nextLine())) {
  25.             String[] data2 = input2.split("=>");
  26.  
  27.             String contest = data2[0];
  28.             String password = data2[1];
  29.             String userName = data2[2];
  30.             int points = Integer.parseInt(data2[3]);
  31.  
  32.             if (contestPass.containsKey(contest) && contestPass.get(contest).equals(password)) {
  33.                 if (!contestsWithPoints.containsKey(userName)) {
  34.                     contestsWithPoints.put(userName, new LinkedHashMap<>());
  35.                     contestsWithPoints.get(userName).put(contest, points);
  36.                     countPoints.put(userName, points);
  37.                 } else {
  38.                     if (!contestsWithPoints.get(userName).containsKey(contest)) {
  39.                         contestsWithPoints.get(userName).put(contest, points);
  40.                         countPoints.put(userName, countPoints.get(userName) + points);
  41.                     } else {
  42.                         if (contestsWithPoints.get(userName).get(contest) < points) {
  43.                             int diff = points - contestsWithPoints.get(userName).get(contest);
  44.                             countPoints.put(userName, countPoints.get(userName) + diff);
  45.                             contestsWithPoints.get(userName).put(contest, points);
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.  
  51.  
  52.         }
  53.         countPoints.entrySet().stream()
  54.                 .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
  55.                 .limit(1)
  56.                 .forEach(e -> System.out.println(String.format("Best candidate is %s with total %d points.", e.getKey(), e.getValue())));
  57.  
  58.         System.out.println("Ranking: ");
  59.  
  60.         contestsWithPoints.forEach((key, value) -> {
  61.             System.out.println(key);
  62.  
  63.             value.entrySet().stream()
  64.                     .sorted((e1, e2) -> {
  65.                         int sort = Integer.compare(e2.getValue(), e1.getValue());
  66.                         return sort;
  67.                     })
  68.                     .forEach(e-> System.out.println(String.format("#  %s -> %d",e.getKey(),e.getValue())));
  69.  
  70.  
  71.         });
  72.     }
  73. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top