Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.stream.Collectors;
- public class GottaCatchEmAllThirdTry {
- private static final List<Pokemon> rankList = new ArrayList<>();
- private static final Map<String, Set<Pokemon>> pokemonTypes = new Hashtable<>();
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String inputLine = scanner.nextLine();
- String[] command = inputLine.split(" ");
- do {
- switch (command[0]) {
- case "add": {
- AddPokemon(command);
- break;
- }
- case "find": {
- findByType(command[1]);
- break;
- }
- case "ranklist": {
- listByRank(command);
- break;
- }
- }
- command = scanner.nextLine().split(" ");
- }
- while (!command[0].equalsIgnoreCase("end"));
- }
- public static void AddPokemon(String[] command) {
- String name = command[1];
- String type = command[2];
- int power = Integer.parseInt(command[3]);
- int position = Integer.parseInt(command[4]);
- Pokemon newPokemon = new Pokemon(name, type, power);
- rankList.add(position - 1, newPokemon);
- pokemonTypes.putIfAbsent(type, new TreeSet<>());
- pokemonTypes.get(type).add(newPokemon);
- System.out.printf("Added pokemon %s to position %d%n", name, position);
- }
- public static void findByType(String type) {
- if (!pokemonTypes.containsKey(type)) {
- System.out.printf("Type %s: %n", type);
- return;
- }
- if (pokemonTypes.get(type).isEmpty()) {
- System.out.printf("Type %s: %n", type);
- return;
- }
- String result = pokemonTypes.get(type)
- .stream()
- .limit(5)
- .map(Pokemon::toString)
- .collect(Collectors.joining("; "));
- System.out.printf("Type %s: %s%n", type, result);
- }
- public static void listByRank(String[] command) {
- int start = Integer.parseInt(command[1]);
- int end = Integer.parseInt(command[2]);
- StringJoiner print = new StringJoiner("; ");
- for (int i = start - 1; i <= end - 1; i++) {
- print.add(String.format("%d. %s", i + 1, rankList.get(i).toString()));
- }
- System.out.println(print);
- }
- public static class Pokemon implements Comparable<Pokemon> {
- private final String name;
- private final String type;
- private final int power;
- public Pokemon(String name, String type, int power) {
- this.name = name;
- this.type = type;
- this.power = power;
- }
- public String getName() {
- return name;
- }
- public int getPower() {
- return power;
- }
- @Override
- public String toString() {
- return String.format("%s(%d)", getName(), getPower());
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Pokemon pokemon = (Pokemon) o;
- return power == pokemon.power && Objects.equals(name, pokemon.name) && Objects.equals(type, pokemon.type);
- }
- @Override
- public int hashCode() {
- return Objects.hash(name, type, power);
- }
- @Override
- public int compareTo(Pokemon o) {
- return Comparator.comparing(Pokemon::getName)
- .thenComparing(Pokemon::getPower, Comparator.reverseOrder())
- .compare(this, o);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement