Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.08 KB | None | 0 0
  1. import java.awt.image.AreaAveragingScaleFilter;
  2. import java.lang.reflect.Array;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class BattleManager {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         Map<String, int[]> people = new TreeMap<>();
  11.         String[] command = scanner.nextLine().split(":");
  12.  
  13.         while (!command[0].equals("Results")) {
  14.  
  15.             switch (command[0]) {
  16.                 case "Add":
  17.                     String name = command[1];
  18.                     int health = Integer.parseInt(command[2]);
  19.                     int energy = Integer.parseInt(command[3]);
  20.                     int[] healthEnergy = new int[2];
  21.                     if (!people.containsKey(name)) {
  22.                         healthEnergy[0] = health;
  23.                         healthEnergy[1] = energy;
  24.                         people.put(name, healthEnergy);
  25.                     } else {
  26.                         healthEnergy[0] = people.get(name)[0] + health;
  27.                         people.put(name, healthEnergy);
  28.                     }
  29.                     break;
  30.                    
  31.                 case "Attack":
  32.                     String attackerName = command[1];
  33.                     String defenderName = command[2];
  34.                     int damage = Integer.parseInt(command[3]);
  35.  
  36.                     if (people.containsKey(attackerName) && people.containsKey(defenderName)) {
  37.                         people.get(defenderName)[0] = people.get(defenderName)[0] - damage;
  38.                         if (people.get(defenderName)[0] <= 0) {
  39.                             System.out.println(defenderName + " was disqualified!");
  40.                             people.remove(defenderName);
  41.                         }
  42.                         people.get(attackerName)[1] = people.get(attackerName)[1] - 1;
  43.                         if (people.get(attackerName)[1] <= 0) {
  44.                             System.out.println(attackerName + " was disqualified!");
  45.                             people.remove(attackerName);
  46.                         }
  47.                     }
  48.                     break;
  49.  
  50.                 case "Delete":
  51.                     String username = command[1];
  52.                     if (username.equals("All")) {
  53.                         people = new TreeMap<>();
  54.                     } else people.remove(username);
  55.                     break;
  56.             }
  57.             command = scanner.nextLine().split(":");
  58.         }
  59.         if (people.size() > 0) {
  60.             System.out.println("People count: " + people.size());
  61.         }
  62.         people.entrySet()
  63.                 .stream()
  64.                 .sorted((a, b) -> {
  65.                     int compare = b.getValue()[0] - a.getValue()[0];
  66.                     if (compare == 0) {
  67.                         return a.getKey().compareTo(b.getKey());
  68.                     } else {
  69.                         return compare;
  70.                     }
  71.                 }).forEach((e) -> System.out.printf("%s - %d - %d%n", e.getKey(), e.getValue()[0], e.getValue()[1]));
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement