kalinikov

04. Pokemon Evolution

Dec 4th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class PokemonEvolution {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. String input = scanner.nextLine();
  8.  
  9. Map<String, List<String>> pokemons = new LinkedHashMap<>();
  10.  
  11. while (!input.equals("wubbalubbadubdub")) {
  12. if (input.contains("->")) {
  13. String[] tokens = input.split(" -> ");
  14. String pokemonName = tokens[0];
  15. String evolutionType = tokens[1];
  16. String evolutionIndex = tokens[2];
  17. String evolutionTypeIndex = evolutionType + " <-> " + evolutionIndex;
  18.  
  19. if (!pokemons.containsKey(pokemonName)) {
  20. pokemons.put(pokemonName, new ArrayList<>());
  21. pokemons.get(pokemonName).add(evolutionTypeIndex);
  22. } else {
  23. pokemons.get(pokemonName).add(evolutionTypeIndex);
  24. }
  25. } else {
  26. String pokemonName = input;
  27. if (pokemons.containsKey(pokemonName)) {
  28. System.out.println("# " + pokemonName);
  29. for (String s : pokemons.get(pokemonName)) {
  30. System.out.println(s);
  31. }
  32. }
  33. }
  34.  
  35. input = scanner.nextLine();
  36. }
  37.  
  38. pokemons.forEach((name, evolutions) -> {
  39. System.out.printf("# %s%n", name);
  40. evolutions.stream()
  41. .sorted((f, s) -> {
  42. int sEvolution = Integer.parseInt(s.split(" <-> ")[1]);
  43. int fEvolution = Integer.parseInt(f.split(" <-> ")[1]);
  44. return sEvolution - fEvolution;
  45. })
  46. .forEach(System.out::println);
  47. });
  48.  
  49.  
  50. }
  51.  
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment