Advertisement
psi_mmobile

Untitled

Dec 1st, 2022
824
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.81 KB | None | 0 0
  1. import java.util.*;
  2. public class MyClass {
  3.     public static void main(String args[]) {
  4.         Scanner scanner = new Scanner(System.in);
  5.         int numberOfPieces = Integer.parseInt(scanner.nextLine());
  6.         Map bigMap = new LinkedHashMap<String,LinkedHashMap<String,String>>(numberOfPieces);
  7.         for (int i = 0; i < numberOfPieces; i++) {
  8.             String nextPiece = scanner.nextLine();
  9.             String[] stringArr = nextPiece.split("\\|");
  10.             LinkedHashMap<String,String> composerAndKey = new LinkedHashMap<String,String>();
  11.             composerAndKey.put(stringArr[1], stringArr[2]);
  12.             bigMap.put(stringArr[0], composerAndKey);
  13.         }
  14.         while(true) {
  15.             String nextCommand = scanner.nextLine();
  16.             if (nextCommand.equals("Stop")) {
  17.                 Iterator<LinkedHashMap<String,LinkedHashMap<String,String>>> itr = bigMap.entrySet().iterator();
  18.                 while (itr.hasNext()) {
  19.                     Map.Entry pair = (Map.Entry)itr.next();
  20.                     LinkedHashMap<String,String> composerAndKeyMap = (LinkedHashMap<String,String>)pair.getValue();
  21.                     Iterator<Map.Entry<String,String>> composerAndKeyIterator = composerAndKeyMap.entrySet().iterator();
  22.                     Map.Entry composerAndKey = (Map.Entry)composerAndKeyIterator.next();
  23.                     System.out.printf("%s -> Composer: %s, Key: %s\n",pair.getKey(),composerAndKey.getKey(),composerAndKey.getValue());
  24.                 }
  25.                 break;
  26.             }
  27.             String[] stringArr = nextCommand.split("\\|");
  28.             switch(stringArr[0]) {
  29.                 case "Add" : if (bigMap.containsKey(stringArr[1])) { System.out.printf("%s is already in the collection!\n", stringArr[1]); } else {  LinkedHashMap<String, String> composerAndKey = new LinkedHashMap<String,String>(); composerAndKey.put(stringArr[2], stringArr[3]); bigMap.put(stringArr[1], composerAndKey); System.out.printf("%s by %s in %s added to the collection!\n", stringArr[1], stringArr[2], stringArr[3]);} break;
  30.                 case "Remove" : if(bigMap.containsKey(stringArr[1])) { bigMap.remove(stringArr[1]); System.out.printf("Successfully removed %s!\n",stringArr[1]); } else { System.out.printf("Invalid operation! %s does not exist in the collection.\n",stringArr[1]); } break;
  31.                 case "ChangeKey" : if (bigMap.containsKey(stringArr[1])) {  LinkedHashMap<String, String> composerAndKey = (LinkedHashMap<String,String>)bigMap.get(stringArr[1]); Iterator<String> iterator = composerAndKey.keySet().iterator(); composerAndKey.put(iterator.next(),stringArr[2]); System.out.printf("Changed the key of %s to %s!\n",stringArr[1], stringArr[2]);} else {System.out.printf("Invalid operation! %s does not exist in the collection.\n",stringArr[1]);} break;
  32.             }
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement