Advertisement
Didart

Hero Recruitment

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