Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- * Created by SET on 4.10.2015 г..
- */
- public class DragonArmy {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- List<Dragon> dragons = new ArrayList<>();
- for (int i = 0; i < n; i++) {
- String color = scanner.next();
- String name = scanner.next();
- String damage = scanner.next();
- String health = scanner.next();
- String armor = scanner.next();
- Dragon next = new Dragon(color,name,damage,health,armor);
- if (dragons.contains(next)) {
- int index = dragons.indexOf(next);
- dragons.get(index).setDamage(damage);
- dragons.get(index).setHealth(health);
- dragons.get(index).setArmor(armor);
- }else {
- dragons.add(next);
- }
- }
- LinkedHashMap<String, SortedSet<Dragon>> sorted = new LinkedHashMap<>();
- for (Dragon dragon : dragons) {
- sorted.putIfAbsent(dragon.getColor(), new TreeSet<Dragon>());
- sorted.get(dragon.getColor()).add(dragon);
- }
- for (Map.Entry<String, SortedSet<Dragon>> stringTreeSetEntry : sorted.entrySet()) {
- double damage = stringTreeSetEntry.getValue().stream().mapToDouble(Dragon::getDamage).average().getAsDouble();
- double health = stringTreeSetEntry.getValue().stream().mapToDouble(Dragon::getHealth).average().getAsDouble();
- double armor = stringTreeSetEntry.getValue().stream().mapToDouble(Dragon::getArmor).average().getAsDouble();
- System.out.printf("%s::(%.2f/%.2f/%.2f)%n", stringTreeSetEntry.getKey(), damage, health, armor);
- for (Dragon dragon : stringTreeSetEntry.getValue()) {
- System.out.println(dragon);
- }
- }
- // System.out.println(sorted);
- }
- static class Dragon implements Comparable {
- private String color;
- private String name;
- private int damage;
- private int health;
- private int armor;
- public Dragon(String color, String name, String damage, String health, String armor) {
- this.setColor(color);
- this.setName(name);
- this.setDamage(damage);
- this.setHealth(health);
- this.setArmor(armor);
- }
- public String getColor() {
- return color;
- }
- public void setColor(String color) {
- this.color = color;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Integer getDamage() {
- return damage;
- }
- public void setDamage(String damage) {
- if (damage.equals("null")) {
- this.damage = 45;
- }else {
- this.damage = Integer.parseInt(damage);
- }
- }
- public Integer getHealth() {
- return health;
- }
- public void setHealth(String health) {
- if (health.equals("null")) {
- this.health = 250;
- }else {
- this.health = Integer.parseInt(health);
- }
- }
- public Integer getArmor() {
- return armor;
- }
- public void setArmor(String armor) {
- if (armor.equals("null")) {
- this.armor = 10;
- }else {
- this.armor = Integer.parseInt(armor);
- }
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Dragon dragon = (Dragon) o;
- if (!color.equals(dragon.color)) return false;
- return name.equals(dragon.name);
- }
- @Override
- public int hashCode() {
- int result = color.hashCode();
- result = 31 * result + name.hashCode();
- return result;
- }
- @Override
- public String toString() {
- return String.format("-%s -> damage: %d, health: %d, armor: %d", this.getName(), this.getDamage(), this.getHealth(), this.getArmor());
- }
- @Override
- public int compareTo(Object o) {
- return (this.getName()+this.getColor()).compareTo(((Dragon) o).getName() + ((Dragon) o).getColor());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement