Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.stream.Collectors;
- public class Pokemons {
- //private static Set<String> names = new HashSet<>();
- private static Map<String, List<Pokemon>> bytType = new HashMap<String, List<Pokemon>>();
- private static LinkedList<Pokemon> items = new LinkedList<>();
- static boolean flag = false;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String command = scanner.nextLine();
- String[] spltCommand = command.split(" ");
- while (!spltCommand[0].equals("end")) {
- if(command.contains("Player0")) {
- flag = true;
- }
- if(spltCommand[0].equals("add")) {
- //add pokemon
- executeAdd(spltCommand[1], spltCommand[2], spltCommand[3], spltCommand[4]);
- } else if(spltCommand[0].equals("find")) {
- //find
- findPokemon(spltCommand[1]);
- } else if(spltCommand[0].equals("ranklist")) {
- //do smth..
- rank(Integer.parseInt(spltCommand[1]), Integer.parseInt(spltCommand[2]));
- }
- /* if(flag) {
- System.out.print(command + "|");
- }*/
- command = scanner.nextLine();
- spltCommand = command.split(" ");
- /* if(flag) {
- System.out.print(command + "|");
- }*/
- }
- //System.out.println(items.toString());
- }
- private static void rank(int start, int end) {
- for (int i = start-1; i < end; i++) {
- Pokemon pokemon = items.get(i);
- String delimeter = "; ";
- if(i+1 == end) {
- delimeter = "";
- }
- System.out.printf("%d. %s(%d)%s", i+1, pokemon.name, pokemon.power, delimeter);
- }
- System.out.printf("%n");
- }
- private static void findPokemon(String type) {
- if(!bytType.containsKey(type)) {
- System.out.printf("Type %s: %n", type);
- return;
- }
- String result = bytType.get(type).stream()
- .sorted()
- .limit(5)
- .map(Pokemon::toString)
- .collect(Collectors.joining("; "));
- System.out.printf("Type %s: %s%n", type, result);
- }
- private static void executeAdd(String name, String type, String strPower, String strPosition) {
- int position = Integer.parseInt(strPosition);
- int power = Integer.parseInt(strPower);
- if(power < 10 || power > 50) {
- return;
- }
- Pokemon pokemon = new Pokemon(name, type, power);
- //System.out.println(name + position);
- items.add(position-1, pokemon);
- bytType.putIfAbsent(type, new ArrayList<>());
- bytType.get(type).add(pokemon);
- //if(flag) return;
- System.out.printf("Added pokemon %s to position %d%n", name, position);
- }
- static class Pokemon implements Comparable<Pokemon> {
- String name;
- String type;
- 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 String getType() {
- return type;
- }
- public int getPower() {
- return power;
- }
- @Override
- public int compareTo(Pokemon o) {
- return Comparator.comparing(Pokemon::getType)
- .thenComparing(Pokemon::getName)
- .thenComparing(Pokemon::getPower, Comparator.reverseOrder())
- .compare(this, o);
- }
- @Override
- public String toString() {
- return getName() + "(" + power + ")";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment