Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class GottaCatchThemAll {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Pokemon> pokemons = new HashMap<>();
- List<Pokemon> leaderboard = new ArrayList<>();
- String[] input = scanner.nextLine().split(" ");
- while (!(input[0].equals("end"))) {
- String command = input[0];
- switch (command) {
- case "add":
- String pokemonName = input[1];
- String pokemonType = input[2];
- int pokemonPower = Integer.parseInt(input[3]);
- int position = Integer.parseInt(input[4]);
- Pokemon newPokemon = new Pokemon(pokemonName, pokemonType, pokemonPower);
- // ne zapisva vtori put Squirtle!!
- pokemons.put(pokemonName, newPokemon);
- // System.out.println(pokemons.size());
- leaderboard.add(position - 1, newPokemon);
- for (int i = position; i < leaderboard.size(); i++) {
- Pokemon pokemon = leaderboard.get(i);
- pokemon.setPosition(i + 1);
- }
- System.out.println("Added pokemon " + pokemonName + " to position " + position);
- break;
- case "find":
- String type = input[1];
- List<Pokemon> foundPokemons = new ArrayList<>();
- for (Pokemon pokemon : pokemons.values()) {
- if (pokemon.getType().equals(type)) {
- foundPokemons.add(pokemon);
- }
- }
- foundPokemons.sort(Comparator.comparing(Pokemon::getName).thenComparing(Pokemon::getPower).reversed());
- System.out.print("Type " + type + ": ");
- if (foundPokemons.isEmpty()) {
- System.out.println();
- } else {
- for (Pokemon pokemon : foundPokemons) {
- System.out.print(pokemon.getName() + "(" + pokemon.getPower() + ");");
- }
- System.out.println();
- }
- break;
- case "ranklist" :
- int start = Integer.parseInt(input[1]);
- int end = Integer.parseInt(input[2]);
- if (start < 1 || start > leaderboard.size() || end < 1 || end > leaderboard.size() || start > end) {
- System.out.println("Error: Invalid range");
- return;
- }
- for (int i = start - 1; i < end; i++) {
- Pokemon pokemon = leaderboard.get(i);
- System.out.print(start + ". " + pokemon.getName() + "(" + pokemon.getPower() + ")");
- if (i < end - 1) {
- System.out.print("; ");
- }
- }
- System.out.println();
- }
- input = scanner.nextLine().split(" ");
- }
- }
- public static class Pokemon {
- private String name;
- private String type;
- private int power;
- private int position;
- public Pokemon(String name, String type, int power) {
- setName(name);
- setType(type);
- setPower(power);
- position = -1;
- }
- //setter
- public void setName(String name) {
- if (name.length() < 1 || name.length() > 20) {
- System.out.println("Name should be between 1 and 20 characters!");
- }
- this.name = name;
- }
- public void setType(String type) {
- if (type.length() < 1 || type.length() > 20) {
- System.out.println("Type name should be between 1 and 20 characters!");
- }
- this.type = type;
- }
- public void setPower(int power) {
- if (power < 10 || power > 50) {
- System.out.println("Power can be any integer between 10 and 50");
- }
- this.power = power;
- }
- public void setPosition(int position) {
- this.position = position;
- }
- //getter
- public String getName() {
- return name;
- }
- public String getType() {
- return type;
- }
- public int getPower() {
- return power;
- }
- public int getPosition() {
- return position;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement