Advertisement
Ligh7_of_H3av3n

03. Hero Recruitment

Mar 31st, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. package ZadachiOtIzpita;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.LinkedHashSet;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.Set;
  8.  
  9. public class HeroRecruitment {
  10.     public static void main(String[] args) {
  11.         Scanner scanner = new Scanner(System.in);
  12.  
  13.  
  14.         Map<String, Set<String>> heroes = new LinkedHashMap<>();
  15.  
  16.         String input = scanner.nextLine();
  17.         while (!input.equals("End")) {
  18.             String[] tokens = input.split("\\s+");
  19.             String command = tokens[0];
  20.             String heroName = tokens[1];
  21.  
  22.             switch (command) {
  23.                 case "Enroll":
  24.                     if (!heroes.containsKey(heroName)) {
  25.                         heroes.put(heroName, new LinkedHashSet<>());
  26.                     } else {
  27.                         System.out.println(heroName + " is already enrolled.");
  28.                     }
  29.                     break;
  30.                 case "Learn":
  31.                     String spellName = tokens[2];
  32.                     if (heroes.containsKey(heroName)) {
  33.                         if (!heroes.get(heroName).contains(spellName)) {
  34.                             heroes.get(heroName).add(spellName);
  35.                         } else {
  36.                             System.out.println(heroName + " has already learnt " + spellName + ".");
  37.                         }
  38.                     } else {
  39.                         System.out.println(heroName + " doesn't exist.");
  40.                     }
  41.                     break;
  42.                 case "Unlearn":
  43.                     spellName = tokens[2];
  44.                     if (heroes.containsKey(heroName)) {
  45.                         if (heroes.get(heroName).contains(spellName)) {
  46.                             heroes.get(heroName).remove(spellName);
  47.                         } else {
  48.                             System.out.println(heroName + " doesn't know " + spellName + ".");
  49.                         }
  50.                     } else {
  51.                         System.out.println(heroName + " doesn't exist.");
  52.                     }
  53.                     break;
  54.             }
  55.  
  56.             input = scanner.nextLine();
  57.         }
  58.  
  59.         System.out.println("Heroes:");
  60.         for (Map.Entry<String, Set<String>> entry : heroes.entrySet()) {
  61.             System.out.print("== " + entry.getKey() + ": ");
  62.             if (!entry.getValue().isEmpty()) {
  63.                 System.out.print(String.join(", ", entry.getValue()));
  64.             }
  65.             System.out.println();
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement