Advertisement
nikeza

Ranking_More_Exercises

Jul 13th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package TechFundamentals;
  2.  
  3. import com.sun.source.tree.WhileLoopTree;
  4.  
  5. import java.util.*;
  6.  
  7.  
  8. public class array_temp {
  9.  
  10.  
  11.     public static void main(String[] args) {
  12.         Scanner scanner = new Scanner(System.in);
  13.  
  14.         Map<String, String> contestAndPass = new HashMap<>();
  15.         String inputUser = scanner.nextLine();
  16.  
  17.         while (!inputUser.equals("end of contests")) {
  18.             String[] tokens = inputUser.split(":");
  19.             String contest = tokens[0];
  20.             String pass = tokens[1];
  21.  
  22.             contestAndPass.put(contest, pass);
  23.  
  24.             inputUser = scanner.nextLine();
  25.         }
  26.  
  27.         Map<String, Map<String, Integer>> contestUser = new TreeMap<>();
  28.  
  29.         String inputUsersContest = scanner.nextLine();
  30.  
  31.  
  32.         while (!inputUsersContest.equals("end of submissions")) {
  33.             String[] tokens = inputUsersContest.split("=>");
  34.             String contest = tokens[0];
  35.             String pass = tokens[1];
  36.             String userName = tokens[2];
  37.             int points = Integer.parseInt(tokens[3]);
  38.  
  39.             Map<String, Integer> subject = contestUser.get(userName);
  40.  
  41.             for (Map.Entry<String, String> entry : contestAndPass.entrySet()) {
  42.                 if (entry.getKey().equals(contest)) {
  43.                     if (entry.getValue().equals(pass)) {
  44.                         if (!contestUser.containsKey(userName)) {
  45.                             subject = new TreeMap<>();
  46.                             subject.put(contest, points);
  47.                             contestUser.put(userName, subject);
  48.  
  49.                         } else {
  50.                             if (!subject.containsKey(contest)) {
  51.                                 subject.put(contest, points);
  52.                                 contestUser.put(userName, subject);
  53.                             } else {
  54.                                 if (points > subject.get(contest))
  55.                                     subject.put(contest, points);
  56.                                 contestUser.put(userName, subject);
  57.                             }
  58.  
  59.                         }
  60.  
  61.                     }
  62.                 }
  63.             }
  64.  
  65.             inputUsersContest = scanner.nextLine();
  66.         }
  67.  
  68.         int bestPoint = 0;
  69.         String bestName = "";
  70.  
  71.         for (Map.Entry<String, Map<String, Integer>> entry : contestUser.entrySet()) {
  72.             int tempBestPoints = 0;
  73.             Map<String, Integer> subject = entry.getValue();
  74.             for (Map.Entry<String, Integer> e : subject.entrySet()) {
  75.                 tempBestPoints += e.getValue();
  76.             }
  77.             if (bestPoint < tempBestPoints) {
  78.                 bestPoint = tempBestPoints;
  79.                 bestName = entry.getKey();
  80.             }
  81.         }
  82.  
  83.         System.out.printf("Best candidate is %s with total %d points.%n", bestName, bestPoint);
  84.  
  85.         System.out.println("Ranking: ");
  86.         for (Map.Entry<String, Map<String, Integer>> entry : contestUser.entrySet()) {
  87.             System.out.printf("%s%n", entry.getKey());
  88.             Map<String, Integer> subject = entry.getValue();
  89.             subject.entrySet().stream().sorted((f, s) -> s.getValue().compareTo(f.getValue()))
  90.                     .forEach(e -> {
  91.                         System.out.printf("#  %s -> %d%n", e.getKey(), e.getValue());
  92.                     });
  93.  
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement