Advertisement
Guest User

Java 3.0

a guest
Apr 4th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.86 KB | None | 0 0
  1. package com.company.softuni;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.Comparator;
  6. import java.util.Scanner;
  7. import java.util.concurrent.CopyOnWriteArrayList;
  8.  
  9. public class Main {
  10.  
  11.     public static void main(String[] args) {
  12.         int numberOfHeroes = 0;
  13.  
  14.         CopyOnWriteArrayList<Hero> heroes = new CopyOnWriteArrayList<>();
  15.  
  16.         Scanner scanner = new Scanner(System.in);
  17.         numberOfHeroes = Integer.parseInt(scanner.nextLine());
  18.  
  19.         for (int i = 0; i < numberOfHeroes; i++) {
  20.             Hero hero = new Hero();
  21.             String[] line = scanner.nextLine().split("\\s+");
  22.             hero.setName(line[0]);
  23.             hero.setHitPoints(Integer.parseInt(line[1]));
  24.             hero.setManaPoints(Integer.parseInt(line[2]));
  25.             heroes.add(hero);
  26.         }
  27.  
  28.         while (true) {
  29.             String line = scanner.nextLine();
  30.             if (line.equals("End")) {
  31.                 break;
  32.             }
  33.             String[] commands = line.split(" - ");
  34.             switch (commands[0]) {
  35.                 case "CastSpell": {
  36.                     String heroName = commands[1];
  37.                     for (Hero hero : heroes) {
  38.                         if (hero.getName().equals(heroName)) {
  39.                             int mp = Integer.parseInt(commands[2]);
  40.                             String spellname = commands[3];
  41.                             if (mp <= hero.getManaPoints()) {
  42.                                 hero.setManaPoints(hero.getManaPoints() - mp);
  43.                                 System.out.println(heroName + " has successfully cast " + spellname + " and now has " + hero.getManaPoints() + " MP!");
  44.                             } else {
  45.                                 System.out.println(heroName + " does not have enough MP to cast " + spellname + "!");
  46.                             }
  47.                         }
  48.                     }
  49.                     break;
  50.                 }
  51.                 case "TakeDamage": {
  52.                     String heroName = commands[1];
  53.                     for (Hero hero : heroes) {
  54.                         if (hero.getName().equals(heroName)) {
  55.                             int damage = Integer.parseInt(commands[2]);
  56.                             String attacker = commands[3];
  57.                             hero.setHitPoints(hero.getHitPoints() - damage);
  58.                             if (hero.getHitPoints() > 0) {
  59.                                 System.out.println(heroName + "was hit for " + damage + " HP by " + attacker + " and now has " + hero.getHitPoints() + " HP left!");
  60.                             } else {
  61.                                 heroes.remove(hero);
  62.                                 System.out.println(heroName + " has been killed by " + attacker + "!");
  63.                             }
  64.                         }
  65.                     }
  66.                     break;
  67.                 }
  68.                 case "Recharge": {
  69.                     String heroName = commands[1];
  70.                     for (Hero hero : heroes) {
  71.                         if (hero.getName().equals(heroName)) {
  72.                             int mp = Integer.parseInt(commands[2]);
  73.                             int oldMp = hero.getManaPoints();
  74.                             if ((oldMp + mp) > 200) {
  75.                                 hero.setManaPoints(200);
  76.                             } else {
  77.                                 hero.setManaPoints(oldMp + mp);
  78.                             }
  79.                             System.out.println(heroName + " recharged for " + (200 - oldMp) + " MP!");
  80.                         }
  81.                     }
  82.                     break;
  83.                 }
  84.                 case "Heal": {
  85.                     String heroName = commands[1];
  86.                     for (Hero hero : heroes) {
  87.                         if (hero.getName().equals(heroName)) {
  88.                             int hp = Integer.parseInt(commands[2]);
  89.                             int oldHp = hero.getHitPoints();
  90.                             if ((oldHp + hp) > 100) {
  91.                                 hero.setHitPoints(100);
  92.                             } else {
  93.                                 hero.setHitPoints(oldHp + hp);
  94.                             }
  95.                             System.out.println(heroName + " healed for " + (100 - oldHp) + " HP!");
  96.                         }
  97.                     }
  98.                     break;
  99.                 }
  100.             }
  101.  
  102.         }
  103.         Collections.sort(heroes, new Comparator<Hero>() {
  104.             @Override
  105.             public int compare(Hero s1, Hero s2) {
  106.                 if (s1.getHitPoints() > s2.getHitPoints()) {
  107.                     return -1;
  108.                 } else if (s1.getHitPoints() < s2.getHitPoints()) {
  109.                     return 1;
  110.                 }
  111.                 return 0;
  112.             }
  113.         });
  114.         for (Hero hero : heroes) {
  115.             System.out.println(hero.getName());
  116.             System.out.println("HP: " + hero.getHitPoints());
  117.             System.out.println("MP: " + hero.getManaPoints());
  118.         }
  119.     }
  120. }
  121.  
  122. class Hero {
  123.     private String name;
  124.     private int hitPoints;
  125.     private int manaPoints;
  126.  
  127.     public String getName() {
  128.         return name;
  129.     }
  130.  
  131.     public void setName(String name) {
  132.         this.name = name;
  133.     }
  134.  
  135.     public int getHitPoints() {
  136.         return hitPoints;
  137.     }
  138.  
  139.     public void setHitPoints(int hitPoints) {
  140.         this.hitPoints = hitPoints;
  141.     }
  142.  
  143.     public int getManaPoints() {
  144.         return manaPoints;
  145.     }
  146.  
  147.     public void setManaPoints(int manaPoints) {
  148.         this.manaPoints = manaPoints;
  149.     }
  150.  
  151.     @Override
  152.     public boolean equals(Object obj) {
  153.         if (!Hero.class.isAssignableFrom(obj.getClass())) {
  154.             return false;
  155.         }
  156.  
  157.         Hero hero = (Hero) obj;
  158.         return this.name.equals(((Hero) obj).name);
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement