Advertisement
Lyubohd

03. Hero Recruitment

Mar 26th, 2020
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.40 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.     public static void main(String[] args) {
  7.         Scanner scan = new Scanner(System.in);
  8.         HashMap<String, ArrayList<String>> heroes = new HashMap<>();
  9.  
  10.         String input = scan.nextLine();
  11.  
  12.         while (!"End".equals(input)) {
  13.             String[] tokens = input.split("\\s+");
  14.             String command = tokens[0];
  15.             String heroName = tokens[1];
  16.  
  17.             switch (command) {
  18.                 case "Enroll":
  19.                     if (heroes.containsKey(heroName)) {
  20.                         System.out.println(String.format("%s is already enrolled.", heroName));
  21.                     } else {
  22.                         heroes.put(heroName, new ArrayList<>());
  23.                     }
  24.                     break;
  25.                 case "Learn": {
  26.                     if (heroes.containsKey(heroName)) {
  27.                         String spellName = tokens[2];
  28.                         ArrayList<String> spells = heroes.get(heroName);
  29.                         if (spells.contains(spellName)) {
  30.                             System.out.println(String.format("%s has already learnt %s.", heroName, spellName));
  31.                         } else {
  32.                             spells.add(spellName);
  33.                         }
  34.                     } else {
  35.                         System.out.println(String.format("%s doesn't exist.", heroName));
  36.                     }
  37.                 }
  38.                 break;
  39.                 case "Unlearn": {
  40.                     if (heroes.containsKey(heroName)) {
  41.                         String spellName = tokens[2];
  42.                         ArrayList<String> spells = heroes.get(heroName);
  43.                         if (spells.contains(spellName)) {
  44.                             spells.remove(spellName);
  45.                         } else {
  46.                             System.out.println(String.format("%s doesn't know %s.", heroName, spellName));
  47.                         }
  48.                     } else {
  49.                         System.out.println(String.format("%s doesn't exist.", heroName));
  50.                     }
  51.                 }
  52.                 break;
  53.             }
  54.  
  55.             input = scan.nextLine();
  56.         }
  57.         System.out.println("Heroes:");
  58.         heroes
  59.                 .entrySet()
  60.                 .stream()
  61.                 .sorted((h1, h2) -> {
  62.                     int result = h2.getValue().size() - h1.getValue().size();
  63.                     if (result == 0) {
  64.                         result = h1.getKey().compareTo(h2.getKey());
  65.                     }
  66.                     return result;
  67.                 })
  68.                 .forEach(h -> {
  69.                     System.out.print(String.format("== %s: ", h.getKey()));
  70.                     // System.out.println(String.join(", ", h.getValue()));
  71.                     if (h.getValue().size() == 0) {
  72.                         System.out.println();
  73.                     } else {
  74.                         for (int i = 0; i < h.getValue().size(); i++) {
  75.                             if (i == h.getValue().size() - 1) {
  76.                                 System.out.println(h.getValue().get(i));
  77.                             } else {
  78.                                 System.out.print(h.getValue().get(i) + ", ");
  79.                             }
  80.                         }
  81.                     }
  82.                 });
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement