Advertisement
ivo_petkov01

03. The Pianist

Mar 19th, 2023
730
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._The_Pianist
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int piecesCount = int.Parse(Console.ReadLine());
  12.  
  13.             Dictionary<string, string[]> collection = new Dictionary<string, string[]>();
  14.  
  15.             for (int i = 1; i <= piecesCount; i++)
  16.             {
  17.                 string pieceInfo = Console.ReadLine();
  18.                 string[] pieceTokens = pieceInfo.Split("|", StringSplitOptions.RemoveEmptyEntries);
  19.                 string piece = pieceTokens[0];
  20.                 string composer = pieceTokens[1];
  21.                 string key = pieceTokens[2];
  22.  
  23.                 if (!collection.ContainsKey(piece))
  24.                 {
  25.                     collection.Add(piece, new string[2]);
  26.                     collection[piece][0] = composer;
  27.                     collection[piece][1] = key;
  28.                 }
  29.             }
  30.  
  31.             string commandLine = Console.ReadLine();
  32.  
  33.             while (commandLine != "Stop")
  34.             {
  35.                 string[] tokens = commandLine.Split("|", StringSplitOptions.RemoveEmptyEntries);
  36.                 string command = tokens[0];
  37.                 string piece = tokens[1];
  38.  
  39.                 if (command == "Add")
  40.                 {
  41.                     string composer = tokens[2];
  42.                     string key = tokens[3];
  43.  
  44.                     if (collection.ContainsKey(piece))
  45.                     {
  46.                         Console.WriteLine($"{piece} is already in the collection!");
  47.                     }
  48.                     else
  49.                     {
  50.                         collection[piece] = new string[2];
  51.                         collection[piece][0] = composer;
  52.                         collection[piece][1] = key;
  53.                         Console.WriteLine($"{piece} by {composer} in {key} added to the collection!");
  54.                     }
  55.                 }
  56.                 else if (command == "Remove")
  57.                 {
  58.                     if (collection.ContainsKey(piece))
  59.                     {
  60.                         collection.Remove(piece);
  61.                         Console.WriteLine($"Successfully removed {piece}!");
  62.                     }
  63.                     else
  64.                     {
  65.                         Console.WriteLine($"Invalid operation! {piece} does not exist in the collection.");
  66.                     }
  67.                 }
  68.                 else if (command == "ChangeKey")
  69.                 {
  70.                     string newKey = tokens[2];
  71.  
  72.                     if (collection.ContainsKey(piece))
  73.                     {
  74.                         collection[piece][1] = newKey;
  75.                         Console.WriteLine($"Changed the key of {piece} to {newKey}!");
  76.                     }
  77.                     else
  78.                     {
  79.                         Console.WriteLine($"Invalid operation! {piece} does not exist in the collection.");
  80.                     }
  81.                 }
  82.  
  83.                 commandLine = Console.ReadLine();
  84.             }
  85.  
  86.             foreach (KeyValuePair<string, string[]> pieces in collection)
  87.             {
  88.                 string piece = pieces.Key;
  89.                 string composer = pieces.Value[0];
  90.                 string key = pieces.Value[1];
  91.  
  92.                 Console.WriteLine($"{piece} -> Composer: {composer}, Key: {key}");
  93.             }
  94.         }
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement