Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class heroes {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- Map<String, List<Integer>> heroesTo_Hp_To_Mp = new TreeMap<>();
- int numOfHeroes = Integer.parseInt(scan.nextLine());
- for (int i = 0; i < numOfHeroes ; i++) {
- String [] splitOfNumOfHeroes = scan.nextLine().split("\\s+");
- String heroe = splitOfNumOfHeroes[0];
- int hitPoints = Integer.parseInt(splitOfNumOfHeroes[1]);
- int manaPoints = Integer.parseInt(splitOfNumOfHeroes[2]);
- List<Integer> listForAdd = new ArrayList<>();
- heroesTo_Hp_To_Mp.put(heroe,listForAdd);
- heroesTo_Hp_To_Mp.get(heroe).add(0,hitPoints);
- heroesTo_Hp_To_Mp.get(heroe).add(1,manaPoints);
- }
- String secondInput = scan.nextLine();
- while (!secondInput.equals("End")){
- String [] splitSecondInput = secondInput.split(" - ");
- String typeOfCommand = splitSecondInput[0];
- String nameOfhero = splitSecondInput[1];
- switch (typeOfCommand){
- case "CastSpell": {
- String spellname = splitSecondInput[3];
- int MPneeded = Integer.parseInt(splitSecondInput[2]);
- int oldMP = heroesTo_Hp_To_Mp.get(nameOfhero).get(1);
- if (oldMP - MPneeded < 0) {
- System.out.printf("%s does not have enough MP to cast %s!", nameOfhero, spellname).println();
- } else {
- heroesTo_Hp_To_Mp.get(nameOfhero).set(1, oldMP - MPneeded);
- System.out.printf("%s has successfully cast %s and now has %d MP!", nameOfhero, spellname,
- heroesTo_Hp_To_Mp.get(nameOfhero).get(1)).println();
- }
- }
- break;
- case "TakeDamage": {
- String attacker = splitSecondInput[3];
- int damage = Integer.parseInt(splitSecondInput[2]);
- int HPafterDamage = heroesTo_Hp_To_Mp.get(nameOfhero).get(0) - damage;
- if (HPafterDamage > 0) {
- heroesTo_Hp_To_Mp.get(nameOfhero).set(0, HPafterDamage);
- System.out.printf("%s was hit for %d " +
- "HP by %s and now has %d HP left!"
- , nameOfhero, damage, attacker, HPafterDamage).println();
- } else {
- heroesTo_Hp_To_Mp.remove(nameOfhero);
- System.out.printf("%s has been killed by %s!", nameOfhero, attacker).println();
- }
- break;
- }
- case "Recharge": {
- int lastMP = 0;
- int amountForRecharge = Integer.parseInt(splitSecondInput[2]);
- int currentMP = heroesTo_Hp_To_Mp.get(nameOfhero).get(1);
- int MPafterRecharged = heroesTo_Hp_To_Mp.get(nameOfhero).get(1) + amountForRecharge;
- if (MPafterRecharged > 200) {
- lastMP = 200 - currentMP;
- MPafterRecharged = 200;
- heroesTo_Hp_To_Mp.get(nameOfhero).set(1, MPafterRecharged);
- } else {
- lastMP = amountForRecharge;
- heroesTo_Hp_To_Mp.get(nameOfhero).set(1, MPafterRecharged);
- }
- System.out.printf("%s recharged for %d MP!", nameOfhero, lastMP).println();
- }
- break;
- case "Heal": {
- int lastHP = 0;
- int amountHP = Integer.parseInt(splitSecondInput[2]);
- int currentHp = heroesTo_Hp_To_Mp.get(nameOfhero).get(0);
- int newHP = heroesTo_Hp_To_Mp.get(nameOfhero).get(0) + amountHP;
- if (newHP > 100) {
- lastHP = 100 - currentHp;
- newHP = 100;
- } else {
- lastHP = amountHP;
- }
- heroesTo_Hp_To_Mp.get(nameOfhero).set(0, newHP);
- System.out.printf("%s healed for " +
- "%d HP!", nameOfhero, lastHP).println();
- }
- break;
- }
- secondInput =scan.nextLine();
- }
- heroesTo_Hp_To_Mp
- .entrySet()
- .stream()
- .sorted((a,b)->Integer.compare(b.getValue().get(0),a.getValue().get(0)))
- .forEach(e->{
- String hero = e.getKey();
- System.out.println(hero);
- System.out.println(String.format("HP: %d",e.getValue().get(0)));
- System.out.println(String.format("MP: %d",e.getValue().get(1)));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement