borovaneca

WithMap

Mar 28th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.01 KB | None | 0 0
  1. package FinalExam;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.LinkedHashMap;
  5. import java.util.List;
  6. import java.util.Scanner;
  7.  
  8. public class HeroesOfCodeAndLogicVII {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.  
  13.         int n = Integer.parseInt(scanner.nextLine());
  14.  
  15.         LinkedHashMap<String, List<Integer>> heroes = new LinkedHashMap<>();
  16.  
  17.         for (int i = 0; i < n; i++) {
  18.             String[] inputData = scanner.nextLine().split(" ");
  19.             String name = inputData[0];
  20.             int health = Math.min(Integer.parseInt(inputData[1]), 100);
  21.             int mana = Math.min(Integer.parseInt(inputData[2]), 200);
  22.  
  23.             heroes.put(name, new ArrayList<>());
  24.             heroes.get(name).add(health);
  25.             heroes.get(name).add(mana);
  26.  
  27.         }
  28.  
  29.         String command;
  30.         while (!"End".equals(command = scanner.nextLine())) {
  31.             String[] commandsArr = command.split(" - ");
  32.             String currentCommand = commandsArr[0];
  33.             String heroName = commandsArr[1];
  34.  
  35.             switch (currentCommand) {
  36.                 case "CastSpell": {
  37.                     int mpNeeded = Integer.parseInt(commandsArr[2]);
  38.                     String spell = commandsArr[3];
  39.                     if (heroes.get(heroName).get(1) >= mpNeeded) {
  40.                         heroes.get(heroName).set(1, heroes.get(heroName).get(1) - mpNeeded);
  41.                         System.out.printf("%s has successfully cast %s and now has %d MP!\n", heroName, spell, heroes.get(heroName).get(1));
  42.                     } else {
  43.                         System.out.printf("%s does not have enough MP to cast %s!\n", heroName, spell);
  44.                     }
  45.                 }
  46.                 break;
  47.                 case "TakeDamage": {
  48.                     int damage = Integer.parseInt(commandsArr[2]);
  49.                     String attacker = commandsArr[3];
  50.                     heroes.get(heroName).set(0, heroes.get(heroName).get(0) - damage);
  51.                     if (heroes.get(heroName).get(0) > 0) {
  52.                         System.out.printf("%s was hit for %d HP by %s and now has %d HP left!\n", heroName, damage, attacker, heroes.get(heroName).get(0));
  53.                     } else {
  54.                         heroes.remove(heroName);
  55.                         System.out.printf("%s has been killed by %s!\n", heroName, attacker);
  56.                     }
  57.                 }
  58.                 break;
  59.                 case "Recharge": {
  60.                     int amount = Integer.parseInt(commandsArr[2]);
  61.                     int recovered = 0;
  62.                     if (amount + heroes.get(heroName).get(1) > 200) {
  63.                         recovered = 200 - heroes.get(heroName).get(1);
  64.                         heroes.get(heroName).set(1, 200);
  65.                     } else {
  66.                         heroes.get(heroName).set(1, heroes.get(heroName).get(1) + amount);
  67.                         recovered = amount;
  68.                     }
  69.                     System.out.printf("%s recharged for %d MP!\n", heroName, recovered);
  70.  
  71.                 }
  72.                 break;
  73.                 case "Heal": {
  74.                     int healAmount = Integer.parseInt(commandsArr[2]);
  75.                     int recovered = 0;
  76.                     if (healAmount + heroes.get(heroName).get(0) > 100) {
  77.                         recovered = 100 - heroes.get(heroName).get(0);
  78.                         heroes.get(heroName).set(0, 100);
  79.                     } else {
  80.                         recovered = healAmount;
  81.                         heroes.get(heroName).set(0, healAmount + heroes.get(heroName).get(0));
  82.                     }
  83.                     System.out.printf("%s healed for %d HP!\n", heroName, recovered);
  84.                 }
  85.                 break;
  86.             }
  87.         }
  88.  
  89.         heroes.forEach((key, value) -> {
  90.             System.out.println(key);
  91.             System.out.println("  HP: " + value.get(0));
  92.             System.out.println("  MP: " + value.get(1));
  93.         });
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment