NadezhdaGeorgieva

NetherRealms2

Dec 9th, 2020
532
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 E05NetherRealms2 {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         Map<String, double[]> demonsBook = new TreeMap<>();
  13.  
  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.             double health = 0.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.             double[] healthAndDamage = new double[]{health, damage};
  56.             demonsBook.put(demonName, healthAndDamage);
  57.         }
  58.         demonsBook.entrySet()
  59.                 .stream()
  60.                 .sorted((a, b) -> a.getKey().compareTo(b.getKey()))
  61.                 .forEach(entry -> System.out.printf("%s - %.0f health, %.2f damage%n",
  62.                         entry.getKey(),
  63.                         entry.getValue()[0],
  64.                         entry.getValue()[1]));
  65.  
  66.     }
  67. }
RAW Paste Data