Advertisement
Guest User

Untitled

a guest
Oct 5th, 2015
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.61 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. /**
  5.  * Created by SET on 4.10.2015 г..
  6.  */
  7. public class DragonArmy {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.         int n = Integer.parseInt(scanner.nextLine());
  11.         List<Dragon> dragons = new ArrayList<>();
  12.         for (int i = 0; i < n; i++) {
  13.             String color = scanner.next();
  14.             String name = scanner.next();
  15.             String damage = scanner.next();
  16.             String health = scanner.next();
  17.             String armor = scanner.next();
  18.             Dragon next = new Dragon(color,name,damage,health,armor);
  19.             if (dragons.contains(next)) {
  20.                 int index = dragons.indexOf(next);
  21.                 dragons.get(index).setDamage(damage);
  22.                 dragons.get(index).setHealth(health);
  23.                 dragons.get(index).setArmor(armor);
  24.             }else {
  25.                 dragons.add(next);
  26.             }
  27.         }
  28.         LinkedHashMap<String, SortedSet<Dragon>> sorted = new LinkedHashMap<>();
  29.         for (Dragon dragon : dragons) {
  30.             sorted.putIfAbsent(dragon.getColor(), new TreeSet<Dragon>());
  31.             sorted.get(dragon.getColor()).add(dragon);
  32.             }
  33.         for (Map.Entry<String, SortedSet<Dragon>> stringTreeSetEntry : sorted.entrySet()) {
  34.             double damage = stringTreeSetEntry.getValue().stream().mapToDouble(Dragon::getDamage).average().getAsDouble();
  35.             double health = stringTreeSetEntry.getValue().stream().mapToDouble(Dragon::getHealth).average().getAsDouble();
  36.             double armor = stringTreeSetEntry.getValue().stream().mapToDouble(Dragon::getArmor).average().getAsDouble();
  37.             System.out.printf("%s::(%.2f/%.2f/%.2f)%n", stringTreeSetEntry.getKey(), damage, health, armor);
  38.             for (Dragon dragon : stringTreeSetEntry.getValue()) {
  39.                 System.out.println(dragon);
  40.             }
  41.  
  42.         }
  43.  
  44.  
  45.        // System.out.println(sorted);
  46.  
  47.  
  48.  
  49.         }
  50.  
  51.     static class Dragon implements Comparable {
  52.         private String color;
  53.         private String name;
  54.         private int damage;
  55.         private int health;
  56.         private int armor;
  57.  
  58.         public Dragon(String color, String name, String damage, String health, String armor) {
  59.             this.setColor(color);
  60.             this.setName(name);
  61.             this.setDamage(damage);
  62.             this.setHealth(health);
  63.             this.setArmor(armor);
  64.         }
  65.  
  66.         public String getColor() {
  67.             return color;
  68.         }
  69.  
  70.  
  71.         public void setColor(String color) {
  72.             this.color = color;
  73.         }
  74.  
  75.         public String getName() {
  76.             return name;
  77.         }
  78.  
  79.         public void setName(String name) {
  80.             this.name = name;
  81.         }
  82.  
  83.         public Integer getDamage() {
  84.             return damage;
  85.         }
  86.  
  87.         public void setDamage(String damage) {
  88.             if (damage.equals("null")) {
  89.                 this.damage = 45;
  90.             }else {
  91.                 this.damage = Integer.parseInt(damage);
  92.             }
  93.  
  94.         }
  95.  
  96.         public Integer getHealth() {
  97.             return health;
  98.         }
  99.  
  100.         public void setHealth(String health) {
  101.             if (health.equals("null")) {
  102.             this.health = 250;
  103.         }else {
  104.             this.health = Integer.parseInt(health);
  105.         }
  106.         }
  107.  
  108.         public Integer getArmor() {
  109.             return armor;
  110.         }
  111.  
  112.         public void setArmor(String armor) {
  113.             if (armor.equals("null")) {
  114.             this.armor = 10;
  115.         }else {
  116.             this.armor = Integer.parseInt(armor);
  117.         }
  118.         }
  119.  
  120.         @Override
  121.         public boolean equals(Object o) {
  122.             if (this == o) return true;
  123.             if (o == null || getClass() != o.getClass()) return false;
  124.  
  125.             Dragon dragon = (Dragon) o;
  126.  
  127.             if (!color.equals(dragon.color)) return false;
  128.             return name.equals(dragon.name);
  129.  
  130.         }
  131.  
  132.         @Override
  133.         public int hashCode() {
  134.             int result = color.hashCode();
  135.             result = 31 * result + name.hashCode();
  136.             return result;
  137.         }
  138.  
  139.         @Override
  140.         public String toString() {
  141.             return String.format("-%s -> damage: %d, health: %d, armor: %d", this.getName(), this.getDamage(), this.getHealth(), this.getArmor());
  142.            
  143.         }
  144.  
  145.         @Override
  146.         public int compareTo(Object o) {
  147.             return (this.getName()+this.getColor()).compareTo(((Dragon) o).getName() + ((Dragon) o).getColor());
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement