Advertisement
Hey_Donny

GottaCatchEmAll

Jun 6th, 2022
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class GottaCatchEmAllThirdTry {
  5. private static final List<Pokemon> rankList = new ArrayList<>();
  6. private static final Map<String, Set<Pokemon>> pokemonTypes = new Hashtable<>();
  7.  
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10.  
  11. String inputLine = scanner.nextLine();
  12.  
  13. String[] command = inputLine.split(" ");
  14.  
  15. do {
  16. switch (command[0]) {
  17. case "add": {
  18. AddPokemon(command);
  19. break;
  20. }
  21. case "find": {
  22. findByType(command[1]);
  23. break;
  24. }
  25. case "ranklist": {
  26. listByRank(command);
  27. break;
  28. }
  29. }
  30. command = scanner.nextLine().split(" ");
  31. }
  32. while (!command[0].equalsIgnoreCase("end"));
  33. }
  34.  
  35. public static void AddPokemon(String[] command) {
  36. String name = command[1];
  37. String type = command[2];
  38. int power = Integer.parseInt(command[3]);
  39. int position = Integer.parseInt(command[4]);
  40.  
  41. Pokemon newPokemon = new Pokemon(name, type, power);
  42.  
  43. rankList.add(position - 1, newPokemon);
  44.  
  45. pokemonTypes.putIfAbsent(type, new TreeSet<>());
  46. pokemonTypes.get(type).add(newPokemon);
  47.  
  48. System.out.printf("Added pokemon %s to position %d%n", name, position);
  49. }
  50.  
  51. public static void findByType(String type) {
  52. if (!pokemonTypes.containsKey(type)) {
  53. System.out.printf("Type %s: %n", type);
  54. return;
  55. }
  56. if (pokemonTypes.get(type).isEmpty()) {
  57. System.out.printf("Type %s: %n", type);
  58. return;
  59. }
  60. String result = pokemonTypes.get(type)
  61. .stream()
  62. .limit(5)
  63. .map(Pokemon::toString)
  64. .collect(Collectors.joining("; "));
  65.  
  66. System.out.printf("Type %s: %s%n", type, result);
  67. }
  68.  
  69. public static void listByRank(String[] command) {
  70. int start = Integer.parseInt(command[1]);
  71. int end = Integer.parseInt(command[2]);
  72.  
  73. StringJoiner print = new StringJoiner("; ");
  74.  
  75. for (int i = start - 1; i <= end - 1; i++) {
  76. print.add(String.format("%d. %s", i + 1, rankList.get(i).toString()));
  77.  
  78. }
  79. System.out.println(print);
  80. }
  81.  
  82. public static class Pokemon implements Comparable<Pokemon> {
  83. private final String name;
  84. private final String type;
  85. private final int power;
  86.  
  87. public Pokemon(String name, String type, int power) {
  88. this.name = name;
  89. this.type = type;
  90. this.power = power;
  91. }
  92.  
  93. public String getName() {
  94. return name;
  95. }
  96.  
  97. public int getPower() {
  98. return power;
  99. }
  100.  
  101. @Override
  102. public String toString() {
  103. return String.format("%s(%d)", getName(), getPower());
  104. }
  105.  
  106. @Override
  107. public boolean equals(Object o) {
  108. if (this == o) return true;
  109. if (o == null || getClass() != o.getClass()) return false;
  110. Pokemon pokemon = (Pokemon) o;
  111. return power == pokemon.power && Objects.equals(name, pokemon.name) && Objects.equals(type, pokemon.type);
  112. }
  113.  
  114. @Override
  115. public int hashCode() {
  116. return Objects.hash(name, type, power);
  117. }
  118.  
  119. @Override
  120. public int compareTo(Pokemon o) {
  121. return Comparator.comparing(Pokemon::getName)
  122. .thenComparing(Pokemon::getPower, Comparator.reverseOrder())
  123. .compare(this, o);
  124. }
  125. }
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement