Advertisement
ledkaa

Pokemon2

May 25th, 2023
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class GottaCatchThemAll {
  4. public static void main(String[] args) {
  5.  
  6. Scanner scanner = new Scanner(System.in);
  7. Map<String, Pokemon> pokemons = new HashMap<>();
  8. List<Pokemon> leaderboard = new ArrayList<>();
  9.  
  10. String[] input = scanner.nextLine().split(" ");
  11.  
  12.  
  13. while (!(input[0].equals("end"))) {
  14. String command = input[0];
  15. switch (command) {
  16. case "add":
  17. String pokemonName = input[1];
  18. String pokemonType = input[2];
  19. int pokemonPower = Integer.parseInt(input[3]);
  20. int position = Integer.parseInt(input[4]);
  21.  
  22. Pokemon newPokemon = new Pokemon(pokemonName, pokemonType, pokemonPower);
  23. // ne zapisva vtori put Squirtle!!
  24. if (pokemons.containsKey(pokemonName)) {
  25. String id1 = "id_1";
  26. pokemons.put(pokemonName + "_" + id1, newPokemon);
  27. } else {
  28. pokemons.putIfAbsent(pokemonName, newPokemon);
  29. }
  30. // System.out.println(pokemons.size());
  31. leaderboard.add(position - 1, newPokemon);
  32. for (int i = position; i < leaderboard.size(); i++) {
  33. Pokemon pokemon = leaderboard.get(i);
  34. pokemon.setPosition(i + 1);
  35.  
  36. }
  37. System.out.println("Added pokemon " + pokemonName + " to position " + position);
  38.  
  39. break;
  40. case "find":
  41. String type = input[1];
  42. List<Pokemon> foundPokemons = new ArrayList<>();
  43.  
  44. for (Pokemon pokemon : pokemons.values()) {
  45. if (pokemon.getType().equals(type)) {
  46. foundPokemons.add(pokemon);
  47. }
  48. }
  49.  
  50. foundPokemons.sort(Comparator.comparing(Pokemon::getName).thenComparing(Pokemon::getPower).reversed());
  51. System.out.print("Type " + type + ": ");
  52. if (foundPokemons.isEmpty()) {
  53. System.out.println();
  54. } else {
  55. for (int i = 0; i < Math.min(foundPokemons.size() - 1, 4); i++) {
  56. System.out.print(foundPokemons.get(i).name + "(" + foundPokemons.get(i).power + "); ");
  57. }
  58. System.out.print(foundPokemons.get(Math.min(foundPokemons.size() - 1, 4)).name
  59. + "(" + foundPokemons.get(Math.min(foundPokemons.size() - 1, 4)).power + ")");
  60. System.out.println();
  61. }
  62. break;
  63. case "ranklist":
  64. int start = Integer.parseInt(input[1]);
  65. int end = Integer.parseInt(input[2]);
  66. if (start < 1 || start > leaderboard.size() || end < 1 || end > leaderboard.size() || start > end) {
  67. System.out.println("Error: Invalid range");
  68. return;
  69. }
  70.  
  71.  
  72. for (int i = start - 1; i < end; i++) {
  73. Pokemon pokemon = leaderboard.get(i);
  74. System.out.print(i + 1 + ". " + pokemon.getName() + "(" + pokemon.getPower() + ")");
  75. if (i < end - 1) {
  76. System.out.print("; ");
  77. }
  78. }
  79. System.out.println();
  80. }
  81.  
  82. input = scanner.nextLine().split(" ");
  83. }
  84.  
  85.  
  86. }
  87.  
  88. public static class Pokemon {
  89. private String name;
  90. private String type;
  91. private int power;
  92. private int position;
  93.  
  94. public Pokemon(String name, String type, int power) {
  95. setName(name);
  96. setType(type);
  97. setPower(power);
  98. position = -1;
  99. }
  100.  
  101. //setter
  102.  
  103.  
  104. public void setName(String name) {
  105. if (name.length() < 1 || name.length() > 20) {
  106. System.out.println("Name should be between 1 and 20 characters!");
  107. }
  108. this.name = name;
  109. }
  110.  
  111. public void setType(String type) {
  112. if (type.length() < 1 || type.length() > 20) {
  113. System.out.println("Type name should be between 1 and 20 characters!");
  114. }
  115. this.type = type;
  116. }
  117.  
  118. public void setPower(int power) {
  119. if (power < 10 || power > 50) {
  120. System.out.println("Power can be any integer between 10 and 50");
  121. }
  122. this.power = power;
  123. }
  124.  
  125. public void setPosition(int position) {
  126. this.position = position;
  127. }
  128.  
  129. //getter
  130.  
  131. public String getName() {
  132. return name;
  133. }
  134.  
  135. public String getType() {
  136. return type;
  137. }
  138.  
  139. public int getPower() {
  140. return power;
  141. }
  142.  
  143. public int getPosition() {
  144. return position;
  145. }
  146. }
  147.  
  148. }
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement