Advertisement
NadezhdaGeorgieva

E05NetherRealmsObject

Dec 11th, 2020
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.93 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 E05NetherRealmsObject {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.         Map<String, Demon> demonsBook = new TreeMap<>();
  13.  
  14.         String[] input = scanner.nextLine().split("\\s*,\\s*");
  15.         for (int i = 0; i < input.length; i++) {
  16.             String demonsName = input[i];
  17.             int health = Demon.calculateHealth(demonsName);
  18.             double damage = Demon.calculateDamage(demonsName);
  19.             Demon demon = new Demon(demonsName, health, damage);
  20.             demonsBook.put(demon.getName(), demon);
  21.         }
  22.  
  23.         demonsBook.values()
  24.                 .stream()
  25.                 .sorted((a, b) -> a.getName().compareTo(b.getName()))
  26.                 .map(Demon::toString)
  27.                 .forEach(System.out::println);
  28.     }
  29.  
  30.     private static class Demon{
  31.         private final String name;
  32.         private final int health;
  33.         private final double damage;
  34.  
  35.         public Demon(String name, int health, double damage){
  36.             this.name = name;
  37.             this.health = health;
  38.             this.damage = damage;
  39.         }
  40.  
  41.         public static int calculateHealth(String demonsName){
  42.             Pattern healthPattern = Pattern.compile("(?<health>[^\\d*/+\\-.])"); // [^0-9*/+\-.]
  43.             int health = 0;
  44.             Matcher matcher = healthPattern.matcher(demonsName);
  45.             while (matcher.find()){
  46.                 String letter = matcher.group("health");
  47.                 health += letter.charAt(0);
  48.             }
  49.             return health;
  50.         }
  51.  
  52.         public static double calculateDamage(String demonsName){
  53.             Pattern damagePattern = Pattern.compile("(?<damage>[+-]?\\d+\\.?\\d*)");
  54.             double damage = 0.0;
  55.             Matcher matcher = damagePattern.matcher(demonsName);
  56.             while (matcher.find()){
  57.                 double number = Double.parseDouble(matcher.group("damage"));
  58.                 damage += number;
  59.             }
  60.             Pattern symbolPattern = Pattern.compile("(?<symbol>[*/])");
  61.             matcher = symbolPattern.matcher(demonsName);
  62.             while (matcher.find()){
  63.                 String symbol = matcher.group("symbol");
  64.                 switch (symbol){
  65.                     case "*":
  66.                         damage *= 2;
  67.                         break;
  68.                     case "/":
  69.                         damage /= 2;
  70.                         break;
  71.                 }
  72.             }
  73.             return damage;
  74.         }
  75.  
  76.         @Override
  77.         public String toString() {
  78.             return String.format("%s - %d health, %.2f damage",
  79.                     name, health, damage);
  80.         }
  81.  
  82.         public String getName() {
  83.             return name;
  84.         }
  85.     }
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement