Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.LinkedList;
- import java.util.Scanner;
- import java.util.TreeSet;
- public class PokemonCatch {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input;
- String[] fullCommand;
- String command = "No command yet";
- LinkedList<Pokemon> ranking = new LinkedList<>();
- TreeSet<Pokemon> typeSet = new TreeSet<>();
- while (!command.equals("end")) {
- input = scanner.nextLine();
- fullCommand = input.split(" ");
- command = fullCommand[0];
- if (command.equals("add")) {
- String name = fullCommand[1];
- String type = fullCommand[2];
- int power = Integer.parseInt(fullCommand[3]);
- int position = Integer.parseInt(fullCommand[4]);
- Pokemon pokemon = new Pokemon(name, type, power, position);
- typeSet.add(pokemon);
- if (ranking.size() == 0) {
- ranking.add(pokemon);
- } else {
- ranking.add(position - 1, pokemon);
- }
- System.out.printf("Added pokemon %s to position %s%n", name, position);
- } else if (command.equals("find")) {
- String type = fullCommand[1];
- StringBuilder builder = new StringBuilder();
- builder.append(String.format("Type %s: ", type));
- int counter = 0;
- int added = 0;
- int topFive = 0;
- for (Pokemon poke : typeSet) {
- if (counter != 0 && poke.type.equals(type)) {
- builder.append("; ");
- }
- if (poke.type.equals(type)) {
- builder.append(String.format("%s(%s)", poke.name, poke.power));
- added++;
- counter++;
- topFive++;
- }
- if (topFive == 5){
- break;
- }
- }
- if(added == 0){
- System.out.printf("Type %s: %n", type);
- }else {
- System.out.println(builder);
- }
- } else if (command.equals("ranklist")) {
- int from = Integer.parseInt(fullCommand[1]) - 1;
- int to = Integer.parseInt(fullCommand[2]);
- StringBuilder builder = new StringBuilder();
- int counter = 0;
- for (int i = from; i < to; i++) {
- Pokemon poke = ranking.get(i);
- if (counter != 0) {
- builder.append("; ");
- }
- builder.append(String.format("%s. %s(%s)", i + 1, poke.name, poke.power));
- counter++;
- }
- System.out.println(builder);
- }
- }
- }
- public static class Pokemon implements Comparable<Pokemon> {
- String name;
- String type;
- int power;
- int position;
- public Pokemon(String name, String type, int power, int position) {
- this.name = name;
- this.type = type;
- this.power = power;
- this.position = position;
- }
- @Override
- public String toString() {
- return "Pokemon{" +
- "name='" + name + '\'' +
- ", type='" + type + '\'' +
- ", power=" + power +
- ", position=" + position +
- '}';
- }
- @Override
- public int compareTo(Pokemon pokemon) {
- int comperator = 0;
- comperator = this.name.compareTo(pokemon.name);
- if (comperator == 0) {
- if (this.power < pokemon.power) {
- return 1;
- } else if (this.power > pokemon.power) {
- return -1;
- }
- }
- return comperator;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement