Advertisement
NadezhdaGeorgieva

05. Nether Realms

Dec 9th, 2020
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.80 KB | None | 0 0
  1. package bg.softuni.javafundamentals;
  2.  
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class E05NetherRealms {
  10.     public static void main(String[] args) {
  11.         Map<String, Demon> demonsBook = new TreeMap<>();
  12.  
  13.         Scanner scanner = new Scanner(System.in);
  14.         String[] demonNames = scanner.nextLine().split(",\\s*");
  15.  
  16.         String healthExpression = "(?<health>[^\\d*/+\\-.])"; // [^0-9*/+\-.]
  17.         Pattern healthPattern = Pattern.compile(healthExpression);
  18.  
  19.         String damageExpression = "(?<damage>[+-]?\\d+\\.?\\d*)";
  20.         Pattern damagePattern = Pattern.compile(damageExpression);
  21.  
  22.         String symbolExpression = "(?<symbol>[*/])";
  23.         Pattern symbolPattern = Pattern.compile(symbolExpression);
  24.  
  25.  
  26.         for (int i = 0; i < demonNames.length; i++) {
  27.             String demonName = demonNames[i].replaceAll(" ", "");
  28.             Matcher matcher = healthPattern.matcher(demonName);
  29.             int health = 0;
  30.             double damage = 0.0;
  31.  
  32.             while (matcher.find()){
  33.                 String letter = matcher.group("health");
  34.                 health += letter.charAt(0);
  35.             }
  36.  
  37.             matcher = damagePattern.matcher(demonName);
  38.             while (matcher.find()){
  39.                 double number = Double.parseDouble(matcher.group("damage"));
  40.                 damage += number;
  41.             }
  42.  
  43.             matcher = symbolPattern.matcher(demonName);
  44.             while (matcher.find()){
  45.                 String symbol = matcher.group("symbol");
  46.                 switch (symbol){
  47.                     case "*":
  48.                         damage *= 2;
  49.                         break;
  50.                     case "/":
  51.                         damage /= 2;
  52.                         break;
  53.                 }
  54.             }
  55.             Demon demon = new Demon(demonName, health, damage);
  56.             demonsBook.put(demonName, demon);
  57.         }
  58.         demonsBook.entrySet()
  59.                 .stream()
  60.                 .sorted((a, b) -> a.getKey().compareTo(b.getKey()))
  61.                 .forEach(entry -> System.out.printf("%s - %d health, %.2f damage%n",
  62.                         entry.getKey(),
  63.                         entry.getValue().getHealth(),
  64.                         entry.getValue().getDamage()));
  65.     }
  66.     private static class Demon{
  67.         private String name;
  68.         private int health;
  69.         private double damage;
  70.  
  71.         public Demon(String name, int health, double damage) {
  72.             this.name = name;
  73.             this.health = health;
  74.             this.damage = damage;
  75.         }
  76.  
  77.         public int getHealth() {
  78.             return health;
  79.         }
  80.  
  81.         public double getDamage() {
  82.             return damage;
  83.         }
  84.     }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement