Advertisement
ledkaa

pokemons

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