Advertisement
Ivelin_Arsov

The Pianist

Aug 27th, 2020
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.88 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Problem3 {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         int nLines = Integer.parseInt(scan.nextLine());
  7.         Map<String,String> pieceComp = new LinkedHashMap<>();
  8.         Map<String,String> pieceNote = new LinkedHashMap<>();
  9.         for (int i = 0; i < nLines; i++) {
  10.             String input = scan.nextLine();
  11.             String[] tokens = input.split("\\|");
  12.             String piece = tokens[0];
  13.             String compositor = tokens[1];
  14.             String keyNote = tokens[2];
  15.             pieceComp.put(piece,compositor);
  16.             pieceNote.put(piece,keyNote);
  17.         }
  18.         String input = scan.nextLine();
  19.         while (!input.equals("Stop")) {
  20.             String[] tokens = input.split("\\|");
  21.             String command = tokens[0];
  22.             String piece = tokens[1];
  23.  
  24.             switch (command) {
  25.                 case "Add":
  26.                     String compositor = tokens[2];
  27.                     String keyNote = tokens[3];
  28.                     if(pieceComp.containsKey(piece)){
  29.                         System.out.println(String.format("%s is already in the collection!",piece));
  30.                     }else{
  31.                         pieceComp.put(piece,compositor);
  32.                         pieceNote.put(piece,keyNote);
  33.                         System.out.println(String.format("%s by %s in %s added to the collection!",piece,compositor,keyNote));
  34.                     }
  35.                     break;
  36.                 case "Remove":
  37.                     if(pieceComp.containsKey(piece)){
  38.                         pieceComp.remove(piece);
  39.                         pieceNote.remove(piece);
  40.                         System.out.println(String.format("Successfully removed %s!",piece));
  41.                     }else{
  42.                         System.out.println(String.format("Invalid operation! %s does not exist in the collection.",piece));
  43.                     }
  44.                     break;
  45.                 case "ChangeKey":
  46.                     String newKey = tokens[2];
  47.                         if(pieceNote.containsKey(piece)){
  48.                             pieceNote.put(piece,newKey);
  49.                             System.out.println(String.format("Changed the key of %s to %s!",piece,newKey));
  50.                         }else{
  51.                             System.out.println(String.format("Invalid operation! %s does not exist in the collection.",piece));
  52.                         }
  53.  
  54.                     break;
  55.             }
  56.             input = scan.nextLine();
  57.         }
  58.         pieceComp.entrySet().stream().sorted((e1,e2)-> {
  59.             int result = e1.getKey().compareTo(e2.getKey());
  60.             return result;
  61.         }).forEach(e -> {
  62.             System.out.println(String.format("%s -> Composer: %s, Key: %s",e.getKey(),e.getValue(),pieceNote.get(e.getKey())));
  63.         });
  64.  
  65.     }
  66.  
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement