Didart

The Pianist

Oct 21st, 2021
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.74 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class ThePianist {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         int n = Integer.parseInt(scanner.nextLine());
  8.         Map<String, List<String>> pieces = new TreeMap<>();
  9.  
  10.         for (int i = 0; i < n; i++) {
  11.             String[] input = scanner.nextLine().split("\\|");
  12.             String onePiece = input[0];
  13.             String composer = input[1];
  14.             String key = input[2];
  15.             pieces.put(onePiece, new ArrayList<>());
  16.             pieces.get(onePiece).add(composer);
  17.             pieces.get(onePiece).add(key);
  18.         }
  19.  
  20.         String input = scanner.nextLine();
  21.         while (!input.equals("Stop")) {
  22.             String[] commands = input.split("\\|");
  23.             String typeCom = commands[0];
  24.             String piece = commands[1];
  25.             switch (typeCom) {
  26.                 case "Add":
  27.                     String composer = commands[2];
  28.                     String key = commands[3];
  29.                     if (pieces.containsKey(piece)) {
  30.                         System.out.printf("%s is already in the collection!%n", piece);
  31.                     } else if (!(pieces.containsKey(piece))) {
  32.                         pieces.put(piece, new ArrayList<>());
  33.                         pieces.get(piece).add(composer);
  34.                         pieces.get(piece).add(key);
  35.                         System.out.printf("%s by %s in %s added to the collection!%n", piece, composer, key);
  36.                     }
  37.                     break;
  38.                 case "Remove":
  39.                     if (pieces.containsKey(piece)) {
  40.                         pieces.remove(piece);
  41.                         System.out.printf("Successfully removed %s!%n", piece);
  42.                     } else {
  43.                         System.out.printf("Invalid operation! %s does not exist in the collection.%n", piece);
  44.                     }
  45.                     break;
  46.                 case "ChangeKey":
  47.                     String newKey = commands[2];
  48.                     if (pieces.containsKey(piece)) {
  49.                         pieces.get(piece).set(1, newKey);
  50.                         System.out.printf("Changed the key of %s to %s!%n", piece, newKey);
  51.                     } else {
  52.                         System.out.printf("Invalid operation! %s does not exist in the collection.%n", piece);
  53.                     }
  54.                     break;
  55.             }
  56.             input = scanner.nextLine();
  57.         }
  58.  
  59.         pieces.entrySet().stream()
  60.  
  61.                 .sorted(Map.Entry.comparingByKey())
  62.  
  63.                 .forEach(kvp -> System.out.printf("%s -> Composer: %s, Key: %s%n",
  64.  
  65.                         kvp.getKey(), kvp.getValue().get(0), kvp.getValue().get(1)));
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment