Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Maps_Lambda_And_Stream_API_6.more_exercises;
- import java.util.*;
- public class MOBAChallenger {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- HashMap<String, TreeMap<String, Integer>> playerPool = new HashMap<>();
- HashMap<String, Integer> playersAndSkill = new HashMap<>();
- String input = console.nextLine();
- while (!input.equals("Season end")) {
- if (input.contains("->")) {
- String[] tokens = input.split(" -> ");
- String player = tokens[0];
- String position = tokens[1];
- int skill = Integer.parseInt(tokens[2]);
- if (!playerPool.containsKey(player)) {
- playerPool.put(player, new TreeMap<>());
- playerPool.get(player).put(position, skill);
- } else {
- if (playerPool.get(player).containsKey(position)) {
- if (playerPool.get(player).get(position) < skill) {
- playerPool.get(player).put(position, skill);
- }
- } else {
- playerPool.get(player).put(position, skill);
- }
- }
- } else {
- String[] tokens = input.split(" vs ");
- String playerOne = tokens[0];
- String playerTwo = tokens[1];
- if (playerPool.containsKey(playerOne) && playerPool.containsKey(playerTwo)) {
- List<String> firstPlayerPosition = new ArrayList<>();
- int firstPlayerSum = 0;
- List<String> secondPlayerPosition = new ArrayList<>();
- int secondPlayerSum = 0;
- for (Map.Entry<String, TreeMap<String, Integer>> entry : playerPool.entrySet()) {
- for (Map.Entry<String, Integer> position : entry.getValue().entrySet()) {
- if (entry.getKey().equals(playerOne)) {
- firstPlayerPosition.add(String.valueOf(position.getKey()));
- firstPlayerSum += position.getValue();
- } else if (entry.getKey().equals(playerTwo)) {
- secondPlayerPosition.add(String.valueOf(position.getKey()));
- secondPlayerSum += position.getValue();
- }
- }
- }
- int longerList = Integer.max(firstPlayerPosition.size(), secondPlayerPosition.size());
- for (int i = 0; i < longerList; i++) {
- if (firstPlayerPosition.contains(secondPlayerPosition.get(i))) {
- if (firstPlayerSum > secondPlayerSum) {
- playerPool.remove(playerTwo);
- break;
- } else {
- playerPool.remove(playerOne);
- break;
- }
- }
- }
- }
- }
- input = console.nextLine();
- }
- for (Map.Entry<String, TreeMap<String, Integer>> entry : playerPool.entrySet()) {
- int sum = 0;
- for (Integer value : entry.getValue().values()) {
- sum += value;
- playersAndSkill.put(entry.getKey(), sum);
- }
- }
- playersAndSkill.entrySet().stream().sorted((f, s) -> {
- int result = s.getValue() - f.getValue();
- if (result == 0) {
- result = f.getKey().compareTo(s.getKey());
- }
- return result;
- }).forEach(e -> {
- System.out.printf("%s: %d skill%n", e.getKey(), e.getValue());
- playerPool.get(e.getKey()).entrySet().stream().sorted((f, s) -> {
- int result = s.getValue() - f.getValue();
- if (result == 0) {
- result = f.getKey().compareTo(s.getKey());
- }
- return result;
- }).forEach(a -> System.out.printf("- %s <::> %d%n", a.getKey(), a.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment