Advertisement
NadezhdaGeorgieva

03. Heroes Of Code And Logic VII 2

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