Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Problem3 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int nLines = Integer.parseInt(scan.nextLine());
- Map<String,String> pieceComp = new LinkedHashMap<>();
- Map<String,String> pieceNote = new LinkedHashMap<>();
- for (int i = 0; i < nLines; i++) {
- String input = scan.nextLine();
- String[] tokens = input.split("\\|");
- String piece = tokens[0];
- String compositor = tokens[1];
- String keyNote = tokens[2];
- pieceComp.put(piece,compositor);
- pieceNote.put(piece,keyNote);
- }
- String input = scan.nextLine();
- while (!input.equals("Stop")) {
- String[] tokens = input.split("\\|");
- String command = tokens[0];
- String piece = tokens[1];
- switch (command) {
- case "Add":
- String compositor = tokens[2];
- String keyNote = tokens[3];
- if(pieceComp.containsKey(piece)){
- System.out.println(String.format("%s is already in the collection!",piece));
- }else{
- pieceComp.put(piece,compositor);
- pieceNote.put(piece,keyNote);
- System.out.println(String.format("%s by %s in %s added to the collection!",piece,compositor,keyNote));
- }
- break;
- case "Remove":
- if(pieceComp.containsKey(piece)){
- pieceComp.remove(piece);
- pieceNote.remove(piece);
- System.out.println(String.format("Successfully removed %s!",piece));
- }else{
- System.out.println(String.format("Invalid operation! %s does not exist in the collection.",piece));
- }
- break;
- case "ChangeKey":
- String newKey = tokens[2];
- if(pieceNote.containsKey(piece)){
- pieceNote.put(piece,newKey);
- System.out.println(String.format("Changed the key of %s to %s!",piece,newKey));
- }else{
- System.out.println(String.format("Invalid operation! %s does not exist in the collection.",piece));
- }
- break;
- }
- input = scan.nextLine();
- }
- pieceComp.entrySet().stream().sorted((e1,e2)-> {
- int result = e1.getKey().compareTo(e2.getKey());
- return result;
- }).forEach(e -> {
- System.out.println(String.format("%s -> Composer: %s, Key: %s",e.getKey(),e.getValue(),pieceNote.get(e.getKey())));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement