Advertisement
Guest User

Untitled

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