shaunaa

Untitled

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