RGAlex

ProblemO3

Apr 5th, 2021
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.41 KB | None | 0 0
  1. package Final0304;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class ProblemO3 {
  7.     static Map<String, Integer> personHealth = new HashMap<>();
  8.     static Map<String, Integer> personEnergy = new HashMap<>();
  9.  
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         String command;
  13.         while (!"Results".equals(command = scanner.nextLine())) {
  14.             String[] commandParts = command.split(":");
  15.             String commandType = commandParts[0];
  16.             switch (commandType) {
  17.                 case "Add":
  18.                     add(commandParts[1], Integer.parseInt(commandParts[2]), Integer.parseInt(commandParts[3]));
  19.                     break;
  20.                 case "Attack":
  21.                     attack(commandParts[1], commandParts[2], Integer.parseInt(commandParts[3]));
  22.                     break;
  23.                 case "Delete":
  24.                     delete(commandParts[1]);
  25.                     break;
  26.             }
  27.         }
  28.         List<String> names = personHealth.entrySet().stream()
  29.                 .sorted((entryA, entryB) -> {
  30.                     int healthA = entryA.getValue();
  31.                     String nameA = entryA.getKey();
  32.                     int healthB = entryB.getValue();
  33.                     String nameB = entryB.getKey();
  34.  
  35.                     if (healthA != healthB) {
  36.                         return Integer.compare(healthB, healthA);
  37.                     } else {
  38.                         return nameA.compareTo(nameB);
  39.                     }
  40.  
  41.                 })
  42.                 .map(Map.Entry::getKey)
  43.                 .collect(Collectors.toList());
  44.         System.out.printf("People count: %d\n", names.size());
  45.         for (String name : names) {
  46.             System.out.printf("%s - %d - %d\n",
  47.                     name, personHealth.get(name), personEnergy.get(name));
  48.         }
  49.     }
  50.  
  51.     private static void delete(String id) {
  52.         if (id.equals("All")) {
  53.             personHealth.clear();
  54.             personEnergy.clear();
  55.         } else {
  56.             disqualify(id, false);
  57.         }
  58.     }
  59.  
  60.     private static void attack(String attacker, String defender, int damage) {
  61.         Integer defenderHealth = personHealth.get(defender);
  62.         Integer attackerEnergy = personEnergy.get(attacker);
  63.  
  64.         if (defenderHealth != null && attackerEnergy != null) {
  65.             defenderHealth -= damage;
  66.             if (defenderHealth <= 0) {
  67.                 disqualify(defender, true);
  68.             } else {
  69.                 personHealth.put(defender, defenderHealth);
  70.             }
  71.  
  72.             attackerEnergy--;
  73.             if (attackerEnergy == 0) {
  74.                 disqualify(attacker, true);
  75.             } else {
  76.                 personEnergy.put(attacker, attackerEnergy);
  77.             }
  78.         }
  79.     }
  80.  
  81.     private static void disqualify(String id, boolean showMessage) {
  82.         personHealth.remove(id);
  83.         personEnergy.remove(id);
  84.         if (showMessage) {
  85.             System.out.printf("%s was disqualified!\n", id);
  86.         }
  87.     }
  88.  
  89.     private static void add(String name, int health, int energy) {
  90.         Integer currentHealth = personHealth.get(name);
  91.         if (currentHealth == null) {
  92.             personHealth.put(name, health);
  93.             personEnergy.put(name, energy);
  94.         } else {
  95.             personHealth.put(name, health + currentHealth);
  96.         }
  97.  
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment