purshink

Pokemon Trainer

Jun 12th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.50 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. class Pokemonn {
  6.  
  7.     private String name;
  8.     private String element;
  9.     private int health;
  10.  
  11.     Pokemonn(String name, String element, int health) {
  12.         this.name = name;
  13.         this.element = element;
  14.         this.health = health;
  15.     }
  16.  
  17.  
  18.  
  19.     String getElement() {
  20.         return element;
  21.     }
  22.  
  23.  
  24.     int getHealth() {
  25.         return health;
  26.     }
  27.  
  28.     void setHealth(int health) {
  29.         this.health = health;
  30.     }
  31. }
  32.  
  33. class Trainerr {
  34.     private String name;
  35.     private int badges = 0;
  36.  
  37.  
  38.  
  39.     Trainerr(String name) {
  40.         this.name = name;
  41.  
  42.     }
  43.  
  44.     String getName() {
  45.         return name;
  46.     }
  47.  
  48.  
  49.     int getBadges() {
  50.         return badges;
  51.     }
  52.  
  53.     void setBadges(int badges) {
  54.         this.badges = badges;
  55.     }
  56.  
  57.  
  58. }
  59.  
  60. public class Main {
  61.     public static void main(String[] args) {
  62.  
  63.         Scanner scanner = new Scanner(System.in);
  64.  
  65.         LinkedHashMap<Trainerr, ArrayList<Pokemonn>> trainers  = new LinkedHashMap<>();
  66.  
  67.  
  68.      String command;
  69.         while (!(command = scanner.nextLine()).equals("Tournament")){
  70.  
  71.             String[] tokens = command.split("\\s+");
  72.                 Trainerr trainer = null;
  73.                 boolean isFound = false;
  74.  
  75.  
  76.             for (Map.Entry<Trainerr, ArrayList<Pokemonn>> entry : trainers.entrySet()) {
  77.                 if (entry.getKey().getName().equals(tokens[0])){
  78.                     isFound = true;
  79.                     trainer = entry.getKey();
  80.                     break;
  81.                 }
  82.             }
  83.  
  84.             if (!isFound){
  85.                trainer = new Trainerr(tokens[0]);
  86.                 trainers.putIfAbsent(trainer, new ArrayList<>());
  87.             }
  88.  
  89.  
  90.  
  91.  
  92.                 Pokemonn pokemon = new Pokemonn(tokens[1], tokens[2], Integer.parseInt(tokens[3]));
  93.  
  94.                 trainers.get(trainer).add(pokemon);
  95.  
  96.  
  97.         }
  98.             command = scanner.nextLine();
  99.         while (!command.equals("End")){
  100.  
  101.  
  102.                     for (Map.Entry<Trainerr, ArrayList<Pokemonn>> entry : trainers.entrySet()) {
  103.                         boolean hasElement = false;
  104.                         switch (command) {
  105.  
  106.  
  107.                             case "Fire": {
  108.                             for (int i = 0; i < entry.getValue().size(); i++) {
  109.                                 if (entry.getValue().get(i).getElement().equals("Fire")) {
  110.                                     hasElement = true;
  111.                                     break;
  112.                                     }
  113.                                 }
  114.                                 if (hasElement){
  115.                                     entry.getKey().setBadges(entry.getKey().getBadges() + 1);
  116.                                 }
  117.                                 else {
  118.                                 for (int i = 0; i < entry.getValue().size(); i++) {
  119.                                     for (Pokemonn pokemon : entry.getValue()) {
  120.                                         entry.getValue().get(i).setHealth(entry.getValue().get(i).getHealth() - 10);
  121.                                     }
  122.                                     if (entry.getValue().get(i).getHealth() <= 0) {
  123.                                         entry.getValue().remove(entry.getValue().get(i));
  124.                                     }
  125.                                 }
  126.                             }
  127.                         }
  128.                         break;
  129.  
  130.                             case "Water":{
  131.                                 for (int i = 0; i < entry.getValue().size(); i++) {
  132.                                     if (entry.getValue().get(i).getElement().equals("Water")) {
  133.                                         hasElement = true;
  134.                                         break;
  135.                                     }
  136.                                 }
  137.                                 if (hasElement){
  138.                                     entry.getKey().setBadges(entry.getKey().getBadges() + 1);
  139.                                 }
  140.                                 else {
  141.                                     for (int i = 0; i < entry.getValue().size(); i++) {
  142.                                         for (Pokemonn pokemon : entry.getValue()) {
  143.                                             entry.getValue().get(i).setHealth(entry.getValue().get(i).getHealth() - 10);
  144.                                         }
  145.                                         if (entry.getValue().get(i).getHealth() <= 0) {
  146.                                             entry.getValue().remove(entry.getValue().get(i));
  147.                                         }
  148.                                     }
  149.                                 }
  150.                             }
  151.                             break;
  152.  
  153.                             case "Electricity":{
  154.                                 for (int i = 0; i < entry.getValue().size(); i++) {
  155.                                     if (entry.getValue().get(i).getElement().equals("Electricity")) {
  156.                                         hasElement = true;
  157.                                         break;
  158.                                     }
  159.                                 }
  160.                                 if (hasElement){
  161.                                     entry.getKey().setBadges(entry.getKey().getBadges() + 1);
  162.                                 }
  163.                                 else {
  164.                                     for (int i = 0; i < entry.getValue().size(); i++) {
  165.                                         for (Pokemonn pokemon : entry.getValue()) {
  166.                                             entry.getValue().get(i).setHealth(entry.getValue().get(i).getHealth() - 10);
  167.                                         }
  168.                                         if (entry.getValue().get(i).getHealth() <= 0) {
  169.                                             entry.getValue().remove(entry.getValue().get(i));
  170.                                         }
  171.                                     }
  172.                                 }
  173.                             }
  174.                             break;
  175.                         }
  176.                     }
  177.             command = scanner.nextLine();
  178.  
  179.         }
  180.  
  181.         trainers.entrySet()
  182.                 .stream()
  183.                 .sorted((t1,t2) ->
  184.                      t2.getKey().getBadges() - t1.getKey().getBadges()
  185.                 )
  186.                 .forEach(t -> System.out.println(t.getKey().getName() + " " + t.getKey().getBadges() +" " + t.getValue().size()));
  187.  
  188.     }
  189.  
  190.     }
Advertisement
Add Comment
Please, Sign In to add comment