Valik98

3. MOBA Challenger

Nov 13th, 2019
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.29 KB | None | 0 0
  1. package Maps_Lambda_And_Stream_API_6.more_exercises;
  2.  
  3. import java.util.*;
  4.  
  5. public class MOBAChallenger {
  6.     public static void main(String[] args) {
  7.         Scanner console = new Scanner(System.in);
  8.  
  9.         HashMap<String, TreeMap<String, Integer>> playerPool = new HashMap<>();
  10.         HashMap<String, Integer> playersAndSkill = new HashMap<>();
  11.  
  12.         String input = console.nextLine();
  13.  
  14.         while (!input.equals("Season end")) {
  15.  
  16.             if (input.contains("->")) {
  17.                 String[] tokens = input.split(" -> ");
  18.  
  19.                 String player = tokens[0];
  20.                 String position = tokens[1];
  21.                 int skill = Integer.parseInt(tokens[2]);
  22.  
  23.                 if (!playerPool.containsKey(player)) {
  24.                     playerPool.put(player, new TreeMap<>());
  25.                     playerPool.get(player).put(position, skill);
  26.                 } else {
  27.                     if (playerPool.get(player).containsKey(position)) {
  28.                         if (playerPool.get(player).get(position) < skill) {
  29.                             playerPool.get(player).put(position, skill);
  30.                         }
  31.                     } else {
  32.                         playerPool.get(player).put(position, skill);
  33.                     }
  34.                 }
  35.             } else {
  36.                 String[] tokens = input.split(" vs ");
  37.  
  38.                 String playerOne = tokens[0];
  39.                 String playerTwo = tokens[1];
  40.  
  41.                 if (playerPool.containsKey(playerOne) && playerPool.containsKey(playerTwo)) {
  42.                     List<String> firstPlayerPosition = new ArrayList<>();
  43.                     int firstPlayerSum = 0;
  44.  
  45.                     List<String> secondPlayerPosition = new ArrayList<>();
  46.                     int secondPlayerSum = 0;
  47.  
  48.                     for (Map.Entry<String, TreeMap<String, Integer>> entry : playerPool.entrySet()) {
  49.                         for (Map.Entry<String, Integer> position : entry.getValue().entrySet()) {
  50.                             if (entry.getKey().equals(playerOne)) {
  51.                                 firstPlayerPosition.add(String.valueOf(position.getKey()));
  52.                                 firstPlayerSum += position.getValue();
  53.                             } else if (entry.getKey().equals(playerTwo)) {
  54.                                 secondPlayerPosition.add(String.valueOf(position.getKey()));
  55.                                 secondPlayerSum += position.getValue();
  56.                             }
  57.                         }
  58.                     }
  59.                     int longerList = Integer.max(firstPlayerPosition.size(), secondPlayerPosition.size());
  60.                     for (int i = 0; i < longerList; i++) {
  61.                         if (firstPlayerPosition.contains(secondPlayerPosition.get(i))) {
  62.                             if (firstPlayerSum > secondPlayerSum) {
  63.                                 playerPool.remove(playerTwo);
  64.                                 break;
  65.                             } else {
  66.                                 playerPool.remove(playerOne);
  67.                                 break;
  68.                             }
  69.                         }
  70.                     }
  71.                 }
  72.             }
  73.             input = console.nextLine();
  74.         }
  75.  
  76.         for (Map.Entry<String, TreeMap<String, Integer>> entry : playerPool.entrySet()) {
  77.             int sum = 0;
  78.             for (Integer value : entry.getValue().values()) {
  79.                 sum += value;
  80.                 playersAndSkill.put(entry.getKey(), sum);
  81.             }
  82.         }
  83.  
  84.         playersAndSkill.entrySet().stream().sorted((f, s) -> {
  85.             int result = s.getValue() - f.getValue();
  86.             if (result == 0) {
  87.                 result = f.getKey().compareTo(s.getKey());
  88.             }
  89.             return result;
  90.  
  91.         }).forEach(e -> {
  92.             System.out.printf("%s: %d skill%n", e.getKey(), e.getValue());
  93.  
  94.             playerPool.get(e.getKey()).entrySet().stream().sorted((f, s) -> {
  95.                 int result = s.getValue() - f.getValue();
  96.                 if (result == 0) {
  97.                     result = f.getKey().compareTo(s.getKey());
  98.                 }
  99.                 return result;
  100.  
  101.             }).forEach(a -> System.out.printf("- %s <::> %d%n", a.getKey(), a.getValue()));
  102.         });
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment