sunfresh

Pokemon

Jan 20th, 2023
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.99 KB | None | 0 0
  1. Gotta catch 'em all
  2. You are given the task to implement a system that ranks different pokemons. Each one of them has a name, type, power and current position on the leaderboard. The system should be able to add new pokemons, find all pokemons of a given type and get the ranking in some range (start and end positions).
  3.  
  4. Here are the commands that must be implemented:
  5.  
  6. add POKEMON_NAME POKEMON_TYPE POKEMON_POWER POKEMON_POSITION - adds new pokemon to the rank list;
  7. POKEMON_NAME can be any sequence from 1 to 20 characters;
  8. POKEMON_TYPE can be any sequence from 1 to 10 characters;
  9. POKEMON_POWER can be any integer between 10 and 50;
  10. POKEMON_POSITION can be any integer between 1 and the current pokemons count plus one (e.g. if there are 2 pokemons already, POKEMON_POSITION can be 1, 2 or 3). If a pokemon is inserted to an already used position, all pokemons' positions from this position till the end are incremented by one (e.g. if the ranking system has Pokemon1 in position 1, Pokemon2 in position 2 and is inserting Pokemon3 in position 1 => Pokemon1 goes to position 2 and Pokemon2 goes to position 3).
  11. Prints: Added pokemon POKEMON_NAME to position POKEMON_POSITION;
  12. find POKEMON_TYPE - finds the top 5 pokemons, first ordered by name in ascending order and then by power in descending order;
  13. Prints the results in the following format "Type POKEMON_TYPE: POKEMON; POKEMON; POKEMON" where POKEMON should be printed in the format POKEMON_NAME(POKEMON_POWER). If no pokemons are found, just print Type POKEMON_TYPE: (ending whit one space).
  14. ranklist START END - prints the rank list from START to END positions;
  15. START can be any integer between 1 and current pokemons count;
  16. END can be any integer between 1 and current pokemons count (and will be greater than or equal to START);
  17. end - marks the end of the commands and no other commands will follow afterward.
  18.  
  19.  
  20.  
  21. import java.util.*;
  22. import java.util.stream.Collectors;
  23.  
  24. import static java.lang.String.format;
  25.  
  26. public class GottaCatchEmAll {
  27.  
  28.     static HashMap<String, TreeSet<Pokemon>> pokemonByType = new HashMap<>();
  29.     static ArrayList<Pokemon> listPokemon = new ArrayList<>();
  30.     static StringBuilder result = new StringBuilder();
  31.  
  32.     public static void main(String[] args) {
  33.         Scanner scan = new Scanner(System.in);
  34.  
  35.         String[] line = scan.nextLine().split(" ");
  36.  
  37.         while (!line[0].equals("end")) {
  38.             switch (line[0]) {
  39.                 case ("add"):
  40.                     createPokemon(line[1], line[2], Integer.parseInt(line[3]), Integer.parseInt(line[4]));
  41.                     break;
  42.                 case ("find"):
  43.                     findByType(line[1]);
  44.                     break;
  45.                 case ("ranklist"):
  46.                     ranklist(Integer.parseInt(line[1]), Integer.parseInt(line[2]));
  47.             }
  48.  
  49.             line = scan.nextLine().split(" ");
  50.  
  51.         }
  52.         result.trimToSize();
  53.         System.out.println(result);
  54.  
  55.  
  56.     }
  57.  
  58.     public static void createPokemon(String name, String type, int power, int position) {
  59.  
  60.         Pokemon pokemon = new Pokemon(name, type, power, position);
  61.         result.append(format("Added pokemon %s to position %d%n", pokemon.getName(), pokemon.getPosition()));
  62.         if (listPokemon.size() == 0) {
  63.             listPokemon.add(pokemon);
  64.         } else {
  65.             if (position <= listPokemon.size()) {
  66.                 listPokemon.add(position - 1, pokemon);
  67.             } else {
  68.                 listPokemon.add(pokemon);
  69.             }
  70.         }
  71.  
  72. //        TreeSet<Pokemon> sortedPok = new TreeSet<>();
  73. //        pokemonByType.put(type, sortedPok);
  74. //        sortedPok = pokemonByType.get(type);
  75. //        sortedPok.add(pokemon);
  76.         pokemonByType.putIfAbsent(type, new TreeSet<>());
  77.         pokemonByType.get(type).add(pokemon);
  78.  
  79.  
  80.     }
  81.  
  82.     //Type Water: Squirtle(40); Squirtle(22)
  83.     public static void findByType(String type) {
  84.         result.append(String.format("Type %s: ", type));
  85.         if (pokemonByType.containsKey(type)) {
  86.             result.append(pokemonByType.get(type).stream()
  87.                     .limit(5)
  88.                     .map(Pokemon::toString)
  89.                     .collect(Collectors.joining("; ")));
  90.         }
  91.         result.append(System.lineSeparator());
  92.     }
  93.  
  94.     public static void ranklist(int start, int end) {
  95.  
  96.  
  97.         for (int i = start-1; i < end; i++) {
  98.  
  99.             if(i == end-1){
  100.                 result.append(i+1).append(". ").append(listPokemon.get(i));
  101.                 break;
  102.             }
  103.  
  104.             result.append(i+1).append(". ").append(listPokemon.get(i)).append("; ");
  105.         }
  106.         result.append(System.lineSeparator());
  107.     }
  108.  
  109.     static class Pokemon implements Comparable<Pokemon> {
  110.         private String name;
  111.         private String type;
  112.         private int power;
  113.         private int position;
  114.  
  115.         public Pokemon(String name, String type, int power, int position) {
  116.             setName(name);
  117.             setType(type);
  118.             setPower(power);
  119.             this.position = position;
  120.         }
  121.  
  122.         public String getName() {
  123.             return name;
  124.         }
  125.  
  126.         public void setName(String name) {
  127.             if (name.length() > 20 || name.length() < 1) {
  128.                 throw new InputMismatchException("Name must be between 1 and 20 chars");
  129.             }
  130.             this.name = name;
  131.         }
  132.  
  133.         public String getType() {
  134.             if (type.length() > 20 || type.length() < 1) {
  135.                 throw new InputMismatchException("Type must be between 1 and 20 chars");
  136.             }
  137.             return type;
  138.         }
  139.  
  140.         public void setType(String type) {
  141.             this.type = type;
  142.         }
  143.  
  144.         public int getPower() {
  145.             return power;
  146.         }
  147.  
  148.         public void setPower(int power) {
  149.             if (power > 50 || power < 10) {
  150.                 throw new InputMismatchException("Power must be between 10 and 50 chars");
  151.             }
  152.             this.power = power;
  153.         }
  154.  
  155.         public int getPosition() {
  156.             return position;
  157.         }
  158.  
  159.  
  160.         @Override
  161.         public int hashCode() {
  162.             return Objects.hash(name, type, power);
  163.         }
  164.  
  165.         @Override
  166.         public boolean equals(Object o) {
  167.             if (this == o) return true;
  168.             if (o == null || getClass() != o.getClass()) return false;
  169.             Pokemon pokemon = (Pokemon) o;
  170.             return Objects.equals(name, pokemon.name) && Objects.equals(type, pokemon.type);
  171.         }
  172.  
  173.         @Override
  174.         public int compareTo(Pokemon pokemon) {
  175.             int compareName = this.getName().compareTo(pokemon.getName());
  176.             if (compareName == 0) {
  177.                 return pokemon.getPower() - this.getPower();
  178.             } else {
  179.                 return compareName;
  180.             }
  181.  
  182.         }
  183.  
  184.         @Override
  185.         public String toString() {
  186.             return String.format("%s(%d)", getName(), getPower());
  187.         }
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment