NadezhdaGeorgieva

NetherRealms3

Dec 9th, 2020
441
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 E05NetherRealms3 {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         Map<String, Integer> demonsAndHealth = new TreeMap<>();
  13.         Map<String, Double> demonsAndDamage = new TreeMap<>();
  14.  
  15.         String[] demonNames = scanner.nextLine().split(",\\s*");
  16.  
  17.         String healthExpression = "(?<health>[^\\d*/+\\-.])"; // [^0-9*/+\-.]
  18.         Pattern healthPattern = Pattern.compile(healthExpression);
  19.  
  20.         String damageExpression = "(?<damage>[+-]?\\d+\\.?\\d*)";
  21.         Pattern damagePattern = Pattern.compile(damageExpression);
  22.  
  23.         String symbolExpression = "(?<symbol>[*/])";
  24.         Pattern symbolPattern = Pattern.compile(symbolExpression);
  25.  
  26.  
  27.         for (int i = 0; i < demonNames.length; i++) {
  28.             String demonName = demonNames[i].replaceAll(" ", "");
  29.             Matcher matcher = healthPattern.matcher(demonName);
  30.             int health = 0;
  31.             double damage = 0.0;
  32.  
  33.             while (matcher.find()){
  34.                 String letter = matcher.group("health");
  35.                 health += letter.charAt(0);
  36.             }
  37.  
  38.             matcher = damagePattern.matcher(demonName);
  39.             while (matcher.find()){
  40.                 double number = Double.parseDouble(matcher.group("damage"));
  41.                 damage += number;
  42.             }
  43.  
  44.             matcher = symbolPattern.matcher(demonName);
  45.             while (matcher.find()){
  46.                 String symbol = matcher.group("symbol");
  47.                 switch (symbol){
  48.                     case "*":
  49.                         damage *= 2;
  50.                         break;
  51.                     case "/":
  52.                         damage /= 2;
  53.                         break;
  54.                 }
  55.             }
  56.             demonsAndHealth.put(demonName, health);
  57.             demonsAndDamage.put(demonName, damage);
  58.         }
  59.         demonsAndHealth.entrySet()
  60.                 .stream()
  61.                 .sorted((a, b) -> a.getKey().compareTo(b.getKey()))
  62.                 .forEach(demon -> System.out.printf("%s - %d health, %.2f damage%n",
  63.                         demon.getKey(),
  64.                         demon.getValue(),
  65.                         demonsAndDamage.get(demon.getKey())));
  66.     }
  67. }
  68.  
RAW Paste Data