Advertisement
tripTiPscout

05. Dragon Army

Jun 20th, 2022
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. package Fundamentals;
  2.  
  3. import java.util.*;
  4. import java.util.stream.Collectors;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int lines = Integer.parseInt(scanner.nextLine());
  12.  
  13.         List<Dragon> dragons = new ArrayList<>();
  14.  
  15.         for (int i = 1; i <= lines; i++) {
  16.             String[] dragonData = scanner.nextLine().split(" ");
  17.  
  18.             String type = dragonData[0];
  19.             String name = dragonData[1];
  20.             int damage = dragonData[2].equals("null") ? 45 : Integer.parseInt(dragonData[2]);
  21.             int health = dragonData[3].equals("null") ? 250 : Integer.parseInt(dragonData[3]);
  22.             int armor = dragonData[4].equals("null") ? 10 : Integer.parseInt(dragonData[4]);
  23.  
  24.             Dragon newDragon = new Dragon(type, name, damage, health, armor);
  25.  
  26.             dragons.removeIf(currentDragon -> currentDragon.getType().equals(type) && currentDragon.getName().equals(name));
  27.  
  28.             dragons.add(newDragon);
  29.         }
  30.  
  31.         Map<String, List<Dragon>> groupedDragons = dragons.stream().collect(Collectors.groupingBy(Dragon::getType,LinkedHashMap::new, Collectors.toList()));
  32.  
  33.         for (Map.Entry<String, List<Dragon>> entry : groupedDragons.entrySet()) {
  34.             double avgDamage = entry.getValue().stream().mapToDouble(Dragon::getDamage).average().getAsDouble();
  35.             double avgHealth = entry.getValue().stream().mapToDouble(Dragon::getHealth).average().getAsDouble();
  36.             double avgArmor = entry.getValue().stream().mapToDouble(Dragon::getArmor).average().getAsDouble();
  37.             System.out.printf("%s::(%.2f/%.2f/%.2f)\n", entry.getKey(), avgDamage, avgHealth, avgArmor);
  38.             entry.getValue().sort(Comparator.comparing(Dragon::getName));
  39.             entry.getValue().forEach(e -> System.out.printf("-%s -> damage: %d, health: %d, armor: %d\n", e.getName(), e.getDamage(), e.getHealth(), e.getArmor()));
  40.         }
  41.  
  42.     }
  43.  
  44.     static class Dragon {
  45.  
  46.         String type;
  47.         String name;
  48.         int damage;
  49.         int health;
  50.         int armor;
  51.  
  52.         public Dragon(String type, String name, int damage, int health, int armor) {
  53.             this.type = type;
  54.             this.name = name;
  55.             this.damage = damage;
  56.             this.health = health;
  57.             this.armor = armor;
  58.         }
  59.  
  60.         public String getType() {
  61.             return type;
  62.         }
  63.  
  64.         public String getName() {
  65.             return name;
  66.         }
  67.  
  68.         public int getDamage() {
  69.             return damage;
  70.         }
  71.  
  72.         public int getHealth() {
  73.             return health;
  74.         }
  75.  
  76.         public int getArmor() {
  77.             return armor;
  78.         }
  79.  
  80.     }
  81.  
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement