RGAlex

O5. Dragon Army / Maps, Lambda and Stream API - More Exercise

Mar 27th, 2021
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.02 KB | None | 0 0
  1. package MapsLambdaStreamAPI.MoreExercises;
  2.  
  3. import java.util.*;
  4.  
  5. public class O5DragonArmy {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int n = Integer.parseInt(scanner.nextLine());
  9.         Map<String, Map<String, List<Double>>> statistic = new LinkedHashMap<>();
  10.  
  11.         for (int i = 0; i < n; i++) {
  12.             String[] input = scanner.nextLine().split(" ");
  13.             String type = input[0];
  14.             String name = input[1];
  15.             double health = 250;
  16.             double damage = 45;
  17.             double armor = 10;
  18.             if (!input[3].equals("null")) {
  19.                 health = Double.parseDouble(input[3]);
  20.             }
  21.             if (!input[2].equals("null")) {
  22.                 damage = Double.parseDouble(input[2]);
  23.             }
  24.             if (!input[4].equals("null")) {
  25.                 armor = Double.parseDouble(input[4]);
  26.             }
  27.             statistic.putIfAbsent(type, new TreeMap<>());
  28.             double finalArmor = armor;
  29.             double finalHealth = health;
  30.             double finalDamage = damage;
  31.             statistic.get(type).put(name, new ArrayList<>() {{
  32.                 add(0, finalDamage);
  33.                 add(1, finalHealth);
  34.                 add(2, finalArmor);
  35.             }});
  36.         }
  37.         statistic.forEach((key1, value1) -> {
  38.             System.out.printf("%s::(%.2f/%.2f/%.2f)%n", key1,
  39.                     value1.values().stream().map(doubles -> doubles.get(0)).mapToDouble(Double::doubleValue).average().orElseThrow(),
  40.                     value1.values().stream().map(doubles -> doubles.get(1)).mapToDouble(Double::doubleValue).average().orElseThrow(),
  41.                     value1.values().stream().map(doubles -> doubles.get(2)).mapToDouble(Double::doubleValue).average().orElseThrow());
  42.             value1.forEach((key, value) -> System.out.printf("-%s -> damage: %.0f, health: %.0f, armor: %.0f%n"
  43.                     , key, value.get(0), value.get(1), value.get(2)));
  44.         });
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment