Advertisement
at90

PokemonTrainer

Jun 14th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.67 KB | None | 0 0
  1. package javaoppbasic.definingclasses.exercise.PokemonTrainer;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.*;
  7. import java.util.stream.Collectors;
  8.  
  9. public class PokemonTrainer {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         Map<String, Trainer> trainers = new LinkedHashMap<>();
  14.         String input;
  15.  
  16.         while (true) {
  17.             if ("Tournament".equals(input = reader.readLine()))
  18.                 break;
  19.  
  20.             String[] tokens = input.split("\\s+");
  21.             String trainerName = tokens[0];
  22.             String pokemonName = tokens[1];
  23.             String pokemonElement = tokens[2];
  24.             int pokemonHealth = Integer.parseInt(tokens[3]);
  25.  
  26.             Pokemon pokemon = new Pokemon(pokemonName, pokemonElement, pokemonHealth);
  27.             if (trainers.containsKey(trainerName)) {
  28.                 Trainer trainer = trainers.get(trainerName);
  29.                 trainer.getPokemons().add(pokemon);
  30.                 trainers.put(trainerName, trainer);
  31.             } else {
  32.                 List<Pokemon> pokemons = new ArrayList<>();
  33.                 pokemons.add(pokemon);
  34.                 Trainer trainer = new Trainer(trainerName, 0, pokemons);
  35.                 trainers.put(trainerName, trainer);
  36.             }
  37.  
  38.         }
  39.  
  40.         while (true) {
  41.             if ("End".equals(input = reader.readLine()))
  42.                 break;
  43.  
  44.                 for (Map.Entry<String, Trainer> trainer : trainers.entrySet()) {
  45.  
  46.                     List<Pokemon> pokemonList = trainer.getValue().getPokemons();
  47.                     if (containsElement(pokemonList, input)) {
  48.                         trainer.getValue().addBatc();
  49.                     } else {
  50.                         for (Iterator<Pokemon> iterator = pokemonList.iterator();iterator.hasNext(); ) {
  51.                             Pokemon pokemon = iterator.next();
  52.                             pokemon.decreaseHealth();
  53.                             if (pokemon.getHealth() <= 0)
  54.                                 iterator.remove();
  55.                         }
  56.  
  57.  
  58.                     }
  59.                 }
  60.         }
  61.  
  62.         trainers = trainers.entrySet().stream()
  63.                 .sorted((a, b) -> Integer.compare(b.getValue().getBadges(), a.getValue().getBadges()))
  64.                 .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> a, LinkedHashMap::new));
  65.  
  66.         for (Map.Entry<String, Trainer> trainer : trainers.entrySet()) {
  67.             System.out.println(trainer.getValue().toString());
  68.         };
  69.  
  70.     }
  71.  
  72.     private static boolean containsElement(List<Pokemon> pokemons, String element) {
  73.         for (Pokemon pokemon : pokemons) {
  74.             if (pokemon.getElement().equals(element))
  75.                 return true;
  76.  
  77.         }
  78.         return false;
  79.     }
  80. }
  81.  
  82. class Trainer {
  83.     private String name;
  84.     private int badges;
  85.     private List<Pokemon> pokemons;
  86.  
  87.     public Trainer() {
  88.     }
  89.  
  90.     public Trainer(String name, int badges, List<Pokemon> pokemons) {
  91.         this.name = name;
  92.         this.badges = badges;
  93.         this.pokemons = pokemons;
  94.     }
  95.  
  96.     public String getName() {
  97.         return name;
  98.     }
  99.  
  100.     public void setName(String name) {
  101.         this.name = name;
  102.     }
  103.  
  104.     public int getBadges() {
  105.         return badges;
  106.     }
  107.  
  108.     public void setBadges(int badges) {
  109.         this.badges = badges;
  110.     }
  111.  
  112.     public List<Pokemon> getPokemons() {
  113.         return pokemons;
  114.     }
  115.  
  116.     public void setPokemons(List<Pokemon> pokemons) {
  117.         this.pokemons = pokemons;
  118.     }
  119.  
  120.     public void addBatc() {
  121.         this.badges++;
  122.     }
  123.  
  124.     @Override
  125.     public String toString() {
  126.         return String.format("%s %d %d", this.name, this.badges, this.pokemons.size());
  127.     }
  128. }
  129.  
  130. class Pokemon {
  131.     private String name;
  132.     private String element;
  133.     private int health;
  134.  
  135.     public Pokemon() {
  136.     }
  137.  
  138.     public Pokemon(String name, String element, int health) {
  139.         this.name = name;
  140.         this.element = element;
  141.         this.health = health;
  142.     }
  143.  
  144.     public String getName() {
  145.         return name;
  146.     }
  147.  
  148.     public void setName(String name) {
  149.         this.name = name;
  150.     }
  151.  
  152.     public String getElement() {
  153.         return element;
  154.     }
  155.  
  156.     public void setElement(String element) {
  157.         this.element = element;
  158.     }
  159.  
  160.     public int getHealth() {
  161.         return health;
  162.     }
  163.  
  164.     public void setHealth(int health) {
  165.         this.health = health;
  166.     }
  167.  
  168.     public void decreaseHealth() {
  169.         this.health -= 10;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement