desislava_topuzakova

03. Pianist

Nov 26th, 2021
1,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. package ExamPreparation;
  2.  
  3. import java.util.*;
  4.  
  5. public class ThePianist_03 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         Map<String, List<String>> piecesMap = new TreeMap<>(); //ключовете са сортирани по азбучен ред
  9.         //пиеса -> списък с информация ([0] -> композитор, [1] -> key)
  10.         int countPieces = Integer.parseInt(scanner.nextLine());
  11.         //информация за пиесите
  12.         for (int piece = 1; piece <= countPieces; piece++) {
  13.             String[] pieceParts = scanner.nextLine().split("\\|");
  14.             //"{piece}|{composer}|{key}".split("|") -> ["{piece}", "{composer}", "{key}"]
  15.             String pieceName = pieceParts[0];
  16.             String composer = pieceParts[1];
  17.             String key = pieceParts[2];
  18.             List<String> pieceInfo = new ArrayList<>();
  19.             pieceInfo.add(composer); //[0]
  20.             pieceInfo.add(key); //[1]
  21.             piecesMap.put(pieceName, pieceInfo);
  22.         }
  23.  
  24.         //получаваме команди
  25.         String command = scanner.nextLine();
  26.         while (!command.equals("Stop")) {
  27.             String [] commandParts = command.split("\\|");
  28.             //"Add|{piece}|{composer}|{key}".split("|") -> ["Add", "{piece}", "{composer}", "{key}"]
  29.             //"Remove|{piece}".split("|") -> ["Remove", "{piece}"]
  30.             //"ChangeKey|{piece}|{new key}".split("|") -> ["ChangeKey", "{piece}", "{new key}"]
  31.             String commandName = commandParts[0]; // "Add", "Remove", "ChangeKey"
  32.             switch(commandName) {
  33.                 case "Add":
  34.                     String newPieceName = commandParts[1];
  35.                     String composer = commandParts[2];
  36.                     String key = commandParts[3];
  37.                     //ако я има
  38.                     if (piecesMap.containsKey(newPieceName)) {
  39.                         System.out.printf("%s is already in the collection!%n", newPieceName);
  40.                     }
  41.                     //ако я няма
  42.                     else {
  43.                         //пиеса -> списък с информация
  44.                         List<String> newPieceInfo = new ArrayList<>();
  45.                         newPieceInfo.add(composer);
  46.                         newPieceInfo.add(key);
  47.                         piecesMap.put(newPieceName, newPieceInfo);
  48.                         System.out.printf("%s by %s in %s added to the collection!%n", newPieceName, composer, key);
  49.                     }
  50.                     break;
  51.                 case "Remove":
  52.                     String pieceForRemove = commandParts[1];
  53.                     //ако я има
  54.                     if (piecesMap.containsKey(pieceForRemove)) {
  55.                         piecesMap.remove(pieceForRemove);
  56.                         System.out.printf("Successfully removed %s!%n", pieceForRemove);
  57.                     }
  58.                     //ако я няма
  59.                     else {
  60.                         System.out.printf("Invalid operation! %s does not exist in the collection.%n", pieceForRemove);
  61.                     }
  62.                     break;
  63.                 case "ChangeKey":
  64.                     String pieceName = commandParts[1];
  65.                     String newKey = commandParts[2];
  66.                     //нямаме такава пиеса
  67.                     if (!piecesMap.containsKey(pieceName)) {
  68.                         System.out.printf("Invalid operation! %s does not exist in the collection.%n", pieceName);
  69.                     }
  70.                     //имаме такава пиеса
  71.                     else {
  72.                         //пиеса -> списък с информация
  73.                         //списък с инфорамцията: [{composer}, {key}]
  74.                         List<String> currentPieceInfo = piecesMap.get(pieceName);
  75.                         currentPieceInfo.remove(1);
  76.                         currentPieceInfo.add(newKey);
  77.                         piecesMap.put(pieceName, currentPieceInfo);
  78.                         System.out.printf("Changed the key of %s to %s!%n", pieceName, newKey);
  79.                     }
  80.                     break;
  81.             }
  82.  
  83.             command = scanner.nextLine();
  84.         }
  85.  
  86.         //пиеса -> списък с информация ([0] -> composer, [1] -> key)
  87.         piecesMap.entrySet()
  88.                 .forEach(entry -> System.out.printf("%s -> Composer: %s, Key: %s%n", entry.getKey(), entry.getValue().get(0), entry.getValue().get(1)));
  89.  
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment