Advertisement
dragonbs

The Pianist

Mar 27th, 2023
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Piece
  6. {
  7.     public string Name { get; set; }
  8.     public string Composer { get; set; }
  9.     public string Key { get; set; }
  10.  
  11.     public Piece(string name, string composer, string key)
  12.     {
  13.         this.Key = key;
  14.         this.Name = name;
  15.         this.Composer = composer;
  16.     }
  17. }
  18.  
  19. class Program
  20. {
  21.     static void Main()
  22.     {
  23.         List<Piece> pieces = new List<Piece>();
  24.         int numberOfPieces = int.Parse(Console.ReadLine());
  25.         for (int i = 0; i < numberOfPieces; i++)
  26.         {
  27.             string[] pieceInfo = Console.ReadLine().Split('|');
  28.             pieces.Add(new Piece(pieceInfo[0], pieceInfo[1], pieceInfo[2]));
  29.         }
  30.         string input;
  31.         while ((input = Console.ReadLine()) != "Stop")
  32.         {
  33.             string[] tokens = input.Split('|');
  34.             switch (tokens[0])
  35.             {
  36.                 case "Add":
  37.                     Add(pieces, tokens); break;
  38.                 case "Remove":
  39.                     Remove(pieces, tokens); break;
  40.                 case "ChangeKey":
  41.                     ChangeKey(pieces, tokens); break;
  42.             }
  43.         }
  44.         Console.WriteLine(String.Join("\n", pieces
  45.             .Select(x => $"{x.Name} -> Composer: {x.Composer}, Key: {x.Key}")));
  46.     }
  47.  
  48.     private static void ChangeKey(List<Piece> pieces, string[] tokens)
  49.     {
  50.         if (pieces.Any(x => x.Name == tokens[1]))
  51.         {
  52.             pieces.First(x => x.Name == tokens[1]).Key = tokens[2];
  53.             Console.WriteLine($"Changed the key of {tokens[1]} to {tokens[2]}!");
  54.         }
  55.         else
  56.             Console.WriteLine($"Invalid operation! {tokens[1]} does not exist in the collection.");
  57.     }
  58.  
  59.     private static void Remove(List<Piece> pieces, string[] tokens)
  60.     {
  61.         if (pieces.Any(x => x.Name == tokens[1]))
  62.         {
  63.             Piece pieceToRemove = pieces.Find(x => x.Name == tokens[1]);
  64.             Console.WriteLine($"Successfully removed {pieceToRemove.Name}!");
  65.             pieces.Remove(pieceToRemove);
  66.         }
  67.         else
  68.             Console.WriteLine($"Invalid operation! {tokens[1]} does not exist in the collection.");
  69.     }
  70.  
  71.     private static void Add(List<Piece> pieces, string[] tokens)
  72.     {
  73.         if (pieces.Any(x => x.Name == tokens[1]))
  74.             Console.WriteLine($"{tokens[1]} is already in the collection!");
  75.         else
  76.         {
  77.             pieces.Add(new Piece(tokens[1], tokens[2], tokens[3]));
  78.             Console.WriteLine($"{tokens[1]} by {tokens[2]} in {tokens[3]} added to the collection!");
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement