RGAlex

O3. The Pianist / FinalExam - O1

Mar 26th, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.49 KB | None | 0 0
  1. package FinalExam.O1;
  2.  
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7. import java.util.function.Function;
  8.  
  9. public class O3ThePianist {
  10.     public static void main(String[] args) {
  11.  
  12.         Scanner scanner = new Scanner(System.in);
  13.         int n = Integer.parseInt(scanner.nextLine());
  14.         Map<String, String[]> pieceInfo = new HashMap<>();
  15.  
  16.         for (int i = 0; i < n; i++) {
  17.             String[] input = scanner.nextLine().split("\\|");
  18.             pieceInfo.put(input[0], new String[]{input[1], input[2]});
  19.         }
  20.         String input;
  21.  
  22.         while (!"Stop".equals(input = scanner.nextLine())) {
  23.             String[] command = input.split("\\|");
  24.             switch (command[0]) {
  25.                 case "Add":
  26.                     if (pieceInfo.containsKey(command[1])) {
  27.                         System.out.printf("%s is already in the collection!%n", command[1]);
  28.                     } else {
  29.                         pieceInfo.put(command[1], new String[]{command[2], command[3]});
  30.                         System.out.printf("%s by %s in %s added to the collection!%n", command[1], command[2], command[3]);
  31.                     }
  32.                     break;
  33.                 case "Remove":
  34.                     if (pieceInfo.containsKey(command[1])) {
  35.                         pieceInfo.remove(command[1]);
  36.                         System.out.printf("Successfully removed %s!%n", command[1]);
  37.                     } else {
  38.                         System.out.printf("Invalid operation! %s does not exist in the collection.%n", command[1]);
  39.                     }
  40.                     break;
  41.                 case "ChangeKey":
  42.                     if (pieceInfo.containsKey(command[1])) {
  43.                         pieceInfo.get(command[1])[1] = command[2];
  44.                         System.out.printf("Changed the key of %s to %s!%n", command[1], command[2]);
  45.                     } else {
  46.                         System.out.printf("Invalid operation! %s does not exist in the collection.%n", command[1]);
  47.                     }
  48.                     break;
  49.             }
  50.         }
  51.         pieceInfo.entrySet().stream()
  52.                 .sorted(Comparator.comparing((Function<Map.Entry<String, String[]>, String>) Map.Entry::getKey)
  53.                         .thenComparing(a -> a.getValue()[0])).forEach(entry ->
  54.                 System.out.printf("%s -> Composer: %s, Key: %s%n", entry.getKey(), entry.getValue()[0], entry.getValue()[1]));
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment