Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.softuni;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.concurrent.CopyOnWriteArrayList;
- public class Main {
- public static void main(String[] args) {
- int numberOfHeroes = 0;
- CopyOnWriteArrayList<Hero> heroes = new CopyOnWriteArrayList<>();
- Scanner scanner = new Scanner(System.in);
- numberOfHeroes = Integer.parseInt(scanner.nextLine());
- for (int i = 0; i < numberOfHeroes; i++) {
- Hero hero = new Hero();
- String[] line = scanner.nextLine().split("\\s+");
- hero.setName(line[0]);
- hero.setHitPoints(Integer.parseInt(line[1]));
- hero.setManaPoints(Integer.parseInt(line[2]));
- heroes.add(hero);
- }
- while (true) {
- String line = scanner.nextLine();
- if (line.equals("End")) {
- break;
- }
- String[] commands = line.split(" - ");
- switch (commands[0]) {
- case "CastSpell": {
- String heroName = commands[1];
- for (Hero hero : heroes) {
- if (hero.getName().equals(heroName)) {
- int mp = Integer.parseInt(commands[2]);
- String spellname = commands[3];
- if (mp <= hero.getManaPoints()) {
- hero.setManaPoints(hero.getManaPoints() - mp);
- System.out.println(heroName + " has successfully cast " + spellname + " and now has " + hero.getManaPoints() + " MP!");
- } else {
- System.out.println(heroName + " does not have enough MP to cast " + spellname + "!");
- }
- }
- }
- break;
- }
- case "TakeDamage": {
- String heroName = commands[1];
- for (Hero hero : heroes) {
- if (hero.getName().equals(heroName)) {
- int damage = Integer.parseInt(commands[2]);
- String attacker = commands[3];
- hero.setHitPoints(hero.getHitPoints() - damage);
- if (hero.getHitPoints() > 0) {
- System.out.println(heroName + "was hit for " + damage + " HP by " + attacker + " and now has " + hero.getHitPoints() + " HP left!");
- } else {
- heroes.remove(hero);
- System.out.println(heroName + " has been killed by " + attacker + "!");
- }
- }
- }
- break;
- }
- case "Recharge": {
- String heroName = commands[1];
- for (Hero hero : heroes) {
- if (hero.getName().equals(heroName)) {
- int mp = Integer.parseInt(commands[2]);
- int oldMp = hero.getManaPoints();
- if ((oldMp + mp) > 200) {
- hero.setManaPoints(200);
- } else {
- hero.setManaPoints(oldMp + mp);
- }
- System.out.println(heroName + " recharged for " + (200 - oldMp) + " MP!");
- }
- }
- break;
- }
- case "Heal": {
- String heroName = commands[1];
- for (Hero hero : heroes) {
- if (hero.getName().equals(heroName)) {
- int hp = Integer.parseInt(commands[2]);
- int oldHp = hero.getHitPoints();
- if ((oldHp + hp) > 100) {
- hero.setHitPoints(100);
- } else {
- hero.setHitPoints(oldHp + hp);
- }
- System.out.println(heroName + " healed for " + (100 - oldHp) + " HP!");
- }
- }
- break;
- }
- }
- }
- Collections.sort(heroes, new Comparator<Hero>() {
- @Override
- public int compare(Hero s1, Hero s2) {
- if (s1.getHitPoints() > s2.getHitPoints()) {
- return -1;
- } else if (s1.getHitPoints() < s2.getHitPoints()) {
- return 1;
- }
- return 0;
- }
- });
- for (Hero hero : heroes) {
- System.out.println(hero.getName());
- System.out.println("HP: " + hero.getHitPoints());
- System.out.println("MP: " + hero.getManaPoints());
- }
- }
- }
- class Hero {
- private String name;
- private int hitPoints;
- private int manaPoints;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getHitPoints() {
- return hitPoints;
- }
- public void setHitPoints(int hitPoints) {
- this.hitPoints = hitPoints;
- }
- public int getManaPoints() {
- return manaPoints;
- }
- public void setManaPoints(int manaPoints) {
- this.manaPoints = manaPoints;
- }
- @Override
- public boolean equals(Object obj) {
- if (!Hero.class.isAssignableFrom(obj.getClass())) {
- return false;
- }
- Hero hero = (Hero) obj;
- return this.name.equals(((Hero) obj).name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement