Advertisement
kaloyan99

Untitled

Jul 5th, 2021
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import java.util.Scanner;
  4. import java.util.TreeSet;
  5.  
  6. public class PokemonCatch {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. String input;
  11. String[] fullCommand;
  12. String command = "No command yet";
  13. LinkedList<Pokemon> ranking = new LinkedList<>();
  14. TreeSet<Pokemon> typeSet = new TreeSet<>();
  15.  
  16. while (!command.equals("end")) {
  17. input = scanner.nextLine();
  18. fullCommand = input.split(" ");
  19. command = fullCommand[0];
  20. if (command.equals("add")) {
  21. String name = fullCommand[1];
  22. String type = fullCommand[2];
  23. int power = Integer.parseInt(fullCommand[3]);
  24. int position = Integer.parseInt(fullCommand[4]);
  25.  
  26. Pokemon pokemon = new Pokemon(name, type, power, position);
  27. typeSet.add(pokemon);
  28. if (ranking.size() == 0) {
  29. ranking.add(pokemon);
  30. } else {
  31. ranking.add(position - 1, pokemon);
  32. }
  33. System.out.printf("Added pokemon %s to position %s%n", name, position);
  34. } else if (command.equals("find")) {
  35. String type = fullCommand[1];
  36. StringBuilder builder = new StringBuilder();
  37. builder.append(String.format("Type %s: ", type));
  38. int counter = 0;
  39. int added = 0;
  40. int topFive = 0;
  41. for (Pokemon poke : typeSet) {
  42. if (counter != 0 && poke.type.equals(type)) {
  43. builder.append("; ");
  44. }
  45. if (poke.type.equals(type)) {
  46. builder.append(String.format("%s(%s)", poke.name, poke.power));
  47. added++;
  48. counter++;
  49. topFive++;
  50. }
  51. if (topFive == 5){
  52. break;
  53. }
  54. }
  55. if(added == 0){
  56. System.out.printf("Type %s: %n", type);
  57. }else {
  58. System.out.println(builder);
  59. }
  60.  
  61. } else if (command.equals("ranklist")) {
  62. int from = Integer.parseInt(fullCommand[1]) - 1;
  63. int to = Integer.parseInt(fullCommand[2]);
  64. StringBuilder builder = new StringBuilder();
  65. int counter = 0;
  66. for (int i = from; i < to; i++) {
  67. Pokemon poke = ranking.get(i);
  68. if (counter != 0) {
  69. builder.append("; ");
  70. }
  71. builder.append(String.format("%s. %s(%s)", i + 1, poke.name, poke.power));
  72. counter++;
  73. }
  74.  
  75. System.out.println(builder);
  76. }
  77.  
  78.  
  79. }
  80. }
  81.  
  82.  
  83. public static class Pokemon implements Comparable<Pokemon> {
  84. String name;
  85. String type;
  86. int power;
  87. int position;
  88.  
  89. public Pokemon(String name, String type, int power, int position) {
  90. this.name = name;
  91. this.type = type;
  92. this.power = power;
  93. this.position = position;
  94. }
  95.  
  96. @Override
  97. public String toString() {
  98. return "Pokemon{" +
  99. "name='" + name + '\'' +
  100. ", type='" + type + '\'' +
  101. ", power=" + power +
  102. ", position=" + position +
  103. '}';
  104. }
  105.  
  106.  
  107. @Override
  108. public int compareTo(Pokemon pokemon) {
  109. int comperator = 0;
  110. comperator = this.name.compareTo(pokemon.name);
  111. if (comperator == 0) {
  112. if (this.power < pokemon.power) {
  113. return 1;
  114. } else if (this.power > pokemon.power) {
  115. return -1;
  116. }
  117. }
  118. return comperator;
  119. }
  120. }
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement