RGAlex

O3. MOBA Challenger / Maps, Lambda and Stream API - More Exercise

Mar 27th, 2021
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. package MapsLambdaStreamAPI.MoreExercises;
  2. import java.util.*;
  3.  
  4. public class O3MOBAChallenger {
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.         Map<String, Map<String, Integer>> playerInfo = new TreeMap<>();
  9.         String input;
  10.  
  11.         while (!"Season end".equals(input = scan.nextLine())) {
  12.             if (input.contains(" -> ")) {
  13.                 String[] command = input.split(" -> ");
  14.                 String player = command[0];
  15.                 String position = command[1];
  16.                 int points = Integer.parseInt(command[2]);
  17.                 playerInfo.putIfAbsent(player, new LinkedHashMap<>());
  18.                 playerInfo.get(player).putIfAbsent(position, 0);
  19.                 if (playerInfo.get(player).get(position) < points) {
  20.                     playerInfo.get(player).put(position, points);
  21.                 }
  22.             } else {
  23.                 String[] fight = input.split("\\s+vs\\s+");
  24.                 String playerOne = fight[0];
  25.                 String playerTwo = fight[1];
  26.                 if (!playerOne.equals(playerTwo) && playerInfo.containsKey(playerOne) && playerInfo.containsKey(playerTwo)) {
  27.                     int totalPointsPlayerOne = 0;
  28.                     int totalPointsPlayerTwo = 0;
  29.                     for (Map.Entry<String, Integer> playerOneEntry : playerInfo.get(playerOne).entrySet()) {
  30.                         for (Map.Entry<String, Integer> playerTwoEntry : playerInfo.get(playerTwo).entrySet()) {
  31.                             if (playerOneEntry.getKey().equals(playerTwoEntry.getKey())) {
  32.                                 totalPointsPlayerOne += playerOneEntry.getValue();
  33.                                 totalPointsPlayerTwo += playerTwoEntry.getValue();
  34.                             }
  35.                         }
  36.                     }
  37.                     if (totalPointsPlayerOne > totalPointsPlayerTwo) {
  38.                         playerInfo.remove(playerTwo);
  39.                     } else if (totalPointsPlayerTwo > totalPointsPlayerOne) {
  40.                         playerInfo.remove(playerOne);
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.         playerInfo.entrySet().stream().sorted((player1, player2) ->
  46.                 Integer.compare(player2.getValue().values().stream().mapToInt(Integer::intValue).sum(),
  47.                         player1.getValue().values().stream().mapToInt(Integer::intValue).sum())).forEach(entry -> {
  48.             System.out.printf("%s: %d skill%n", entry.getKey(), entry.getValue().values().stream()
  49.                     .mapToInt(Integer::intValue).sum());
  50.             entry.getValue().entrySet().stream().sorted(Map.Entry.<String, Integer>comparingByValue().reversed()
  51.                     .thenComparing(Map.Entry.comparingByKey()))
  52.                     .forEach(value -> System.out.printf("- %s <::> %d%n", value.getKey(), value.getValue()));
  53.         });
  54.     }
  55. }
  56.  
Advertisement
Add Comment
Please, Sign In to add comment