Advertisement
NadezhdaGeorgieva

03. Heroes Of Code And Logic VII

Dec 4th, 2020 (edited)
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 KB | None | 0 0
  1. package bg.softuni.javafundamentals;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6. public class Fin01_20Apr_HeroesOfCodeAndLogicVII {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         HashMap<String, Integer> heroesHit = new HashMap<>();
  11.         HashMap<String, Integer> heroesMana = new HashMap<>();
  12.  
  13.         int n = Integer.parseInt(scanner.nextLine());
  14.  
  15.         for (int i = 0; i < n; i++) {
  16.             String[] tokens = scanner.nextLine().split("\\s+");
  17.             String heroName = tokens[0];
  18.             int hitPoints = Integer.parseInt(tokens[1]);
  19.             int manaPoints = Integer.parseInt(tokens[2]);
  20.  
  21.             heroesHit.put(heroName, hitPoints);
  22.             heroesMana.put(heroName, manaPoints);
  23.         }
  24.  
  25.         String input = scanner.nextLine();
  26.         while (!"End".equals(input)){
  27.             String[] tokens = input.split(" - ");
  28.             String command = tokens[0];
  29.             String heroName = tokens[1];
  30.             switch (command){
  31.                 case "CastSpell":
  32.                     int neededMP = Integer.parseInt(tokens[2]);
  33.                     String spellName = tokens[3];
  34.                     if (heroesMana.get(heroName) - neededMP >= 0){
  35.                         heroesMana.put(heroName,heroesMana.get(heroName) - neededMP);
  36.                         System.out.printf("%s has successfully cast %s and now has %d MP!%n",
  37.                                 heroName, spellName, heroesMana.get(heroName));
  38.                     } else{
  39.                         System.out.printf("%s does not have enough MP to cast %s!%n", heroName, spellName);
  40.                     }
  41.                     break;
  42.                 case "TakeDamage":
  43.                     int damage = Integer.parseInt(tokens[2]);
  44.                     String attacker = tokens[3];
  45.                     if (heroesHit.get(heroName) - damage > 0){
  46.                         heroesHit.put(heroName, heroesHit.get(heroName) - damage);
  47.                         System.out.printf("%s was hit for %d HP by %s and now has %d HP left!%n",
  48.                                 heroName, damage, attacker, heroesHit.get(heroName));
  49.                     } else{
  50.                         heroesHit.remove(heroName);
  51.                         heroesMana.remove(heroName);
  52.                         System.out.printf("%s has been killed by %s!%n", heroName, attacker);
  53.                     }
  54.                     break;
  55.                 case "Recharge":
  56.                     int amount = Integer.parseInt(tokens[2]);
  57.                     if (heroesMana.get(heroName) + amount > 200){
  58.                         amount = 200 - heroesMana.get(heroName);
  59.                         heroesMana.put(heroName, 200);
  60.                     } else {
  61.                         heroesMana.put(heroName, heroesMana.get(heroName) + amount);
  62.                     }
  63.                     System.out.printf("%s recharged for %d MP!%n", heroName, amount);
  64.                     break;
  65.                 case "Heal":
  66.                     amount = Integer.parseInt(tokens[2]);
  67.                     if (heroesHit.get(heroName) + amount > 100){
  68.                         amount = 100 - heroesHit.get(heroName);
  69.                         heroesHit.put(heroName, 100);
  70.                     } else {
  71.                         heroesHit.put(heroName, heroesHit.get(heroName) + amount);
  72.  
  73.                     }
  74.                     System.out.printf("%s healed for %d HP!%n", heroName, amount);
  75.                     break;
  76.             }
  77.             input = scanner.nextLine();
  78.         }
  79.         heroesHit.entrySet()
  80.                 .stream()
  81.                 .sorted((f, s) -> {
  82.                     int result = s.getValue().compareTo(f.getValue());
  83.                     if (result == 0){
  84.                         result = f.getKey().compareTo(s.getKey());
  85.                     }
  86.                     return result;
  87.                 }).forEach(hero -> {
  88.                     System.out.println(hero.getKey());
  89.                     System.out.printf(" HP: %d%n", hero.getValue());
  90.                     System.out.printf(" MP: %d%n", heroesMana.get(hero.getKey()));
  91.         });
  92.     }
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement