Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ZadachiOtIzpita;
- import java.util.LinkedHashMap;
- import java.util.LinkedHashSet;
- import java.util.Map;
- import java.util.Scanner;
- import java.util.Set;
- public class HeroRecruitment {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, Set<String>> heroes = new LinkedHashMap<>();
- String input = scanner.nextLine();
- while (!input.equals("End")) {
- String[] tokens = input.split("\\s+");
- String command = tokens[0];
- String heroName = tokens[1];
- switch (command) {
- case "Enroll":
- if (!heroes.containsKey(heroName)) {
- heroes.put(heroName, new LinkedHashSet<>());
- } else {
- System.out.println(heroName + " is already enrolled.");
- }
- break;
- case "Learn":
- String spellName = tokens[2];
- if (heroes.containsKey(heroName)) {
- if (!heroes.get(heroName).contains(spellName)) {
- heroes.get(heroName).add(spellName);
- } else {
- System.out.println(heroName + " has already learnt " + spellName + ".");
- }
- } else {
- System.out.println(heroName + " doesn't exist.");
- }
- break;
- case "Unlearn":
- spellName = tokens[2];
- if (heroes.containsKey(heroName)) {
- if (heroes.get(heroName).contains(spellName)) {
- heroes.get(heroName).remove(spellName);
- } else {
- System.out.println(heroName + " doesn't know " + spellName + ".");
- }
- } else {
- System.out.println(heroName + " doesn't exist.");
- }
- break;
- }
- input = scanner.nextLine();
- }
- System.out.println("Heroes:");
- for (Map.Entry<String, Set<String>> entry : heroes.entrySet()) {
- System.out.print("== " + entry.getKey() + ": ");
- if (!entry.getValue().isEmpty()) {
- System.out.print(String.join(", ", entry.getValue()));
- }
- System.out.println();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement