Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.88 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class HeroesParty {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         HashMap<String, Hero> heroesMap = new HashMap<>();
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.         for (int i = 0; i < n; i++) {
  10.             String[] heroString = scanner.nextLine().split("\\s+");
  11.             String heroname = heroString[0];
  12.             int hitpoints = Integer.parseInt(heroString[1]);
  13.             int manapoints = Integer.parseInt(heroString[2]);
  14.             Hero hero = new Hero(heroname, manapoints, hitpoints);
  15.             heroesMap.put(heroname, hero);
  16.         }
  17.         String currentCommand = "";
  18.         while (!currentCommand.equals("End")) {
  19.             currentCommand = scanner.nextLine();
  20.             String[] splittedCommand = currentCommand.split(" - ");
  21.             switch (splittedCommand[0]) {
  22.                 case "CastSpell": {
  23.                     String heroName = splittedCommand[1];
  24.                     int manaPoints = Integer.parseInt(splittedCommand[2]);
  25.                     String spellName = splittedCommand[3];
  26.                     heroesMap.get(heroName).castSpell(heroName, manaPoints, spellName);
  27.                     break;
  28.                 }
  29.                 case "TakeDamage": {
  30.                     String heroName = splittedCommand[1];
  31.                     int damage = Integer.parseInt(splittedCommand[2]);
  32.                     String attacker = splittedCommand[3];
  33.                     Hero currentHero = heroesMap.get(heroName);
  34.                     currentHero.takeDamage(heroName, damage, attacker);
  35.                     if (!currentHero.isAlive()) heroesMap.remove(heroName);
  36.                     break;
  37.                 }
  38.                 case "Recharge": {
  39.                     String heroName = splittedCommand[1];
  40.                     int amount = Integer.parseInt(splittedCommand[2]);
  41.                     heroesMap.get(heroName).recharge(heroName, amount);
  42.                     break;
  43.                 }
  44.                 case "Heal":
  45.                     String heroName = splittedCommand[1];
  46.                     int amount = Integer.parseInt(splittedCommand[2]);
  47.                     heroesMap.get(heroName).heal(heroName, amount);
  48.                     break;
  49.             }
  50.         }
  51.  
  52.         heroesMap.entrySet()
  53.                 .stream()
  54.                 .sorted(Comparator.comparing(o -> o.getValue().getHitPoints()))
  55.                 .sorted((o1, o2) -> o2.getValue().getHeroName().compareTo(o1.getValue().getHeroName()))
  56.                 .forEach(stringHeroEntry -> System.out.println(stringHeroEntry.getValue().toString())
  57.         );
  58.     }
  59.  
  60.  
  61.     @SuppressWarnings("FieldCanBeLocal")
  62.     public static class Hero {
  63.  
  64.         private String heroName;
  65.         private Integer manaPoints;
  66.         private Integer hitPoints;
  67.         private final int maxManaPoints = 200;
  68.         private final int maxHitPoints = 100;
  69.  
  70.         public Hero(String heroName, int manaPoints, int hitPoints) {
  71.             this.heroName = heroName;
  72.             this.manaPoints = manaPoints;
  73.             this.hitPoints = hitPoints;
  74.         }
  75.  
  76.         public void castSpell(String heroName, int requiredMana, String spellName) {
  77.             if (manaPoints >= requiredMana) {
  78.                 manaPoints = manaPoints - requiredMana;
  79.                 System.out.println(heroName + " has successfully cast " + spellName + " and now has " + manaPoints + " MP!");
  80.             } else {
  81.                 System.out.println(heroName + " does not have enough MP to cast " + spellName + "!");
  82.             }
  83.         }
  84.  
  85.         public void takeDamage(String heroName, int damage, String attackerName) {
  86.             if (isAlive()) {
  87.                 hitPoints = hitPoints - damage;
  88.                 if (!isAlive()) {
  89.                     System.out.println(heroName + " has been killed by " + attackerName + "!");
  90.                 } else {
  91.                     System.out.println(heroName + " was hit for " + damage + " HP by " + attackerName + " and now has " + hitPoints + " HP left!");
  92.                 }
  93.             }
  94.         }
  95.  
  96.         public void recharge(String heroName, int amount) {
  97.             int remainingToMax = maxManaPoints - manaPoints;
  98.             int recharged = 0;
  99.             if ((manaPoints + amount > maxManaPoints)) {
  100.                 manaPoints = manaPoints + remainingToMax;
  101.                 recharged = remainingToMax;
  102.             } else {
  103.                 manaPoints = manaPoints + amount;
  104.                 recharged = amount;
  105.             }
  106.             System.out.println(heroName + " recharged for " + recharged + " MP!");
  107.         }
  108.  
  109.         public void heal(String heroName, int amount) {
  110.             int remainingToMax = maxHitPoints - hitPoints;
  111.             int healed = 0;
  112.             if ((hitPoints + amount > maxHitPoints)) {
  113.                 hitPoints = hitPoints + remainingToMax;
  114.                 healed = remainingToMax;
  115.             } else {
  116.                 hitPoints = hitPoints + amount;
  117.                 healed = amount;
  118.             }
  119.             System.out.println(heroName + " healed for " + healed + " HP!");
  120.         }
  121.  
  122.         public boolean isAlive() {
  123.             return hitPoints > 0;
  124.         }
  125.  
  126.         public String getHeroName() {
  127.             return heroName;
  128.         }
  129.  
  130.         public Integer getManaPoints() {
  131.             return manaPoints;
  132.         }
  133.  
  134.         public Integer getHitPoints() {
  135.             return hitPoints;
  136.         }
  137.  
  138.         @Override
  139.         public String toString() {
  140.             String stringOfObject = "";
  141.             stringOfObject = stringOfObject.concat(heroName).concat("\n");
  142.             stringOfObject = stringOfObject.concat("  HP: ").concat(String.valueOf(hitPoints)).concat("\n");
  143.             stringOfObject = stringOfObject.concat("  MP: ").concat(String.valueOf(manaPoints));
  144.             return stringOfObject;
  145.         }
  146.  
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement