Advertisement
petur_stoqnov

Java

Mar 27th, 2020
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.TreeMap;
  3.  
  4. public class BattleManager {
  5.  
  6. public static class Person {
  7. int health;
  8. int energy;
  9.  
  10. public Person(int health, int energy) {
  11. this.health = health;
  12. this.energy = energy;
  13. }
  14.  
  15. public Person() {
  16.  
  17. }
  18.  
  19. public void setEnergy(int energy) {
  20. this.energy = energy;
  21. }
  22.  
  23. public void setHealth(int health) {
  24. this.health = health;
  25. }
  26.  
  27. public int getEnergy() {
  28. return energy;
  29. }
  30.  
  31. public int getHealth() {
  32. return health;
  33. }
  34. }
  35.  
  36. public static void main(String[] args) {
  37. Scanner sc = new Scanner(System.in);
  38. String[] command = sc.nextLine().split(":");
  39. TreeMap<String, Person> treeMap = new TreeMap<>();
  40.  
  41. while (!command[0].equals("Results")) {
  42.  
  43. switch (command[0]) {
  44. case "Add": {
  45. Person person = new Person();
  46. String name = command[1];
  47. int health = Integer.parseInt(command[2]);
  48. int energy = Integer.parseInt(command[3]);
  49. person.setEnergy(energy);
  50. person.setHealth(health);
  51. if (treeMap.containsKey(name)) {
  52. treeMap.get(name).setHealth(health + treeMap.get(name).getHealth());
  53. treeMap.get(name).setEnergy(energy + treeMap.get(name).getEnergy());
  54. } else {
  55. treeMap.put(name, person);
  56. }
  57. }
  58. break;
  59. case "Attack": {
  60. String attackerName = command[1];
  61. String defenderName = command[2];
  62. int damage = Integer.parseInt(command[3]);
  63. if (treeMap.containsKey(attackerName) && treeMap.containsKey(defenderName)) {
  64. int newHealth = treeMap.get(defenderName).getHealth() - damage;
  65. treeMap.get(defenderName).setHealth(newHealth);
  66. if (treeMap.get(defenderName).getHealth() <= 0) {
  67. treeMap.remove(defenderName);
  68. System.out.printf("%s was disqualified!%n", defenderName);
  69. }
  70. int newAttackerEnergy = treeMap.get(attackerName).getEnergy() - 1;
  71. treeMap.get(attackerName).setEnergy(newAttackerEnergy);
  72. if (treeMap.get(attackerName).getEnergy() == 0) {
  73. treeMap.remove(attackerName);
  74. System.out.printf("%s was disqualified!%n", attackerName);
  75. }
  76. }
  77. }
  78. break;
  79. case "Delete": {
  80. String username = command[1];
  81. if(username.equals("All")){
  82. treeMap = new TreeMap<>();
  83. } else {
  84. treeMap.remove(username);
  85. }
  86. }
  87. break;
  88. }
  89.  
  90. command = sc.nextLine().split(":");
  91. }
  92. System.out.printf("People count: %d%n", treeMap.size());
  93. treeMap.entrySet()
  94. .stream()
  95. .sorted((e1, e2) -> {
  96. int first = e2.getValue().getHealth() - e1.getValue().getHealth();
  97. if(first == 0){
  98. return e1.getKey().compareTo(e2.getKey());
  99. }
  100. return first;
  101. }).forEach(e-> System.out.printf("%s - %d - %d%n",e.getKey(), e.getValue().getHealth(), e.getValue().getEnergy()));
  102.  
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement