ralitsadzhukeva

03. Heroes of Code and Logic VII

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