Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class HeroesParty {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- HashMap<String, Hero> heroesMap = new HashMap<>();
- int n = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < n; i++) {
- String[] heroString = scanner.nextLine().split("\\s+");
- String heroname = heroString[0];
- int hitpoints = Integer.parseInt(heroString[1]);
- int manapoints = Integer.parseInt(heroString[2]);
- Hero hero = new Hero(heroname, manapoints, hitpoints);
- heroesMap.put(heroname, hero);
- }
- String currentCommand = "";
- while (!currentCommand.equals("End")) {
- currentCommand = scanner.nextLine();
- String[] splittedCommand = currentCommand.split(" - ");
- switch (splittedCommand[0]) {
- case "CastSpell": {
- String heroName = splittedCommand[1];
- int manaPoints = Integer.parseInt(splittedCommand[2]);
- String spellName = splittedCommand[3];
- heroesMap.get(heroName).castSpell(heroName, manaPoints, spellName);
- break;
- }
- case "TakeDamage": {
- String heroName = splittedCommand[1];
- int damage = Integer.parseInt(splittedCommand[2]);
- String attacker = splittedCommand[3];
- Hero currentHero = heroesMap.get(heroName);
- currentHero.takeDamage(heroName, damage, attacker);
- if (!currentHero.isAlive()) heroesMap.remove(heroName);
- break;
- }
- case "Recharge": {
- String heroName = splittedCommand[1];
- int amount = Integer.parseInt(splittedCommand[2]);
- heroesMap.get(heroName).recharge(heroName, amount);
- break;
- }
- case "Heal":
- String heroName = splittedCommand[1];
- int amount = Integer.parseInt(splittedCommand[2]);
- heroesMap.get(heroName).heal(heroName, amount);
- break;
- }
- }
- heroesMap.entrySet()
- .stream()
- .sorted(Comparator.comparing(o -> o.getValue().getHitPoints()))
- .sorted((o1, o2) -> o2.getValue().getHeroName().compareTo(o1.getValue().getHeroName()))
- .forEach(stringHeroEntry -> System.out.println(stringHeroEntry.getValue().toString())
- );
- }
- @SuppressWarnings("FieldCanBeLocal")
- public static class Hero {
- private String heroName;
- private Integer manaPoints;
- private Integer hitPoints;
- private final int maxManaPoints = 200;
- private final int maxHitPoints = 100;
- public Hero(String heroName, int manaPoints, int hitPoints) {
- this.heroName = heroName;
- this.manaPoints = manaPoints;
- this.hitPoints = hitPoints;
- }
- public void castSpell(String heroName, int requiredMana, String spellName) {
- if (manaPoints >= requiredMana) {
- manaPoints = manaPoints - requiredMana;
- System.out.println(heroName + " has successfully cast " + spellName + " and now has " + manaPoints + " MP!");
- } else {
- System.out.println(heroName + " does not have enough MP to cast " + spellName + "!");
- }
- }
- public void takeDamage(String heroName, int damage, String attackerName) {
- if (isAlive()) {
- hitPoints = hitPoints - damage;
- if (!isAlive()) {
- System.out.println(heroName + " has been killed by " + attackerName + "!");
- } else {
- System.out.println(heroName + " was hit for " + damage + " HP by " + attackerName + " and now has " + hitPoints + " HP left!");
- }
- }
- }
- public void recharge(String heroName, int amount) {
- int remainingToMax = maxManaPoints - manaPoints;
- int recharged = 0;
- if ((manaPoints + amount > maxManaPoints)) {
- manaPoints = manaPoints + remainingToMax;
- recharged = remainingToMax;
- } else {
- manaPoints = manaPoints + amount;
- recharged = amount;
- }
- System.out.println(heroName + " recharged for " + recharged + " MP!");
- }
- public void heal(String heroName, int amount) {
- int remainingToMax = maxHitPoints - hitPoints;
- int healed = 0;
- if ((hitPoints + amount > maxHitPoints)) {
- hitPoints = hitPoints + remainingToMax;
- healed = remainingToMax;
- } else {
- hitPoints = hitPoints + amount;
- healed = amount;
- }
- System.out.println(heroName + " healed for " + healed + " HP!");
- }
- public boolean isAlive() {
- return hitPoints > 0;
- }
- public String getHeroName() {
- return heroName;
- }
- public Integer getManaPoints() {
- return manaPoints;
- }
- public Integer getHitPoints() {
- return hitPoints;
- }
- @Override
- public String toString() {
- String stringOfObject = "";
- stringOfObject = stringOfObject.concat(heroName).concat("\n");
- stringOfObject = stringOfObject.concat(" HP: ").concat(String.valueOf(hitPoints)).concat("\n");
- stringOfObject = stringOfObject.concat(" MP: ").concat(String.valueOf(manaPoints));
- return stringOfObject;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement