Advertisement
Kent_St_1

HeroesOfCodeAndLogicSeven_03FinalExam

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