Advertisement
IvanIYankov

Battle Manager

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