Advertisement
ivo_petkov01

The Pianist 2.0

Mar 21st, 2023
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._The_Pianist_2
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int piecesCount = int.Parse(Console.ReadLine());
  12.  
  13.             List<Piece> collection = new List<Piece>();
  14.  
  15.             for (int i = 0; i < piecesCount; i++)
  16.             {
  17.                 string pieceInfo = Console.ReadLine();
  18.                 string[] pieceTokens = pieceInfo.Split("|", StringSplitOptions.RemoveEmptyEntries);
  19.                 string pieceName = pieceTokens[0];
  20.                 string composer = pieceTokens[1];
  21.                 string key = pieceTokens[2];
  22.  
  23.                 Piece piece = new Piece(pieceName, composer, key);
  24.                 collection.Add(piece);
  25.             }
  26.  
  27.             string commandLine = Console.ReadLine();
  28.  
  29.             while (commandLine != "Stop")
  30.             {
  31.                 string[] tokens = commandLine.Split("|", StringSplitOptions.RemoveEmptyEntries);
  32.                 string command = tokens[0];
  33.                 string pieceName = tokens[1];
  34.  
  35.                 switch (command)
  36.                 {
  37.                     case "Add":
  38.  
  39.                         string composer = tokens[2];
  40.                         string key = tokens[3];
  41.  
  42.                         Piece nextPiece = collection.FirstOrDefault(x => x.Name == pieceName);
  43.                         if (nextPiece == null)
  44.                         {
  45.                             Piece piece = new Piece(pieceName, composer, key);
  46.                             collection.Add(piece);
  47.                             Console.WriteLine($"{pieceName} by {composer} in {key} added to the collection!");
  48.                         }
  49.                         else
  50.                         {
  51.                             Console.WriteLine($"{pieceName} is already in the collection!");
  52.                         }
  53.  
  54.                         break;
  55.  
  56.                     case "Remove":
  57.  
  58.                         Piece pieceToRemove = collection.FirstOrDefault(x => x.Name == pieceName);
  59.                         if (pieceToRemove != null)
  60.                         {
  61.                             collection.Remove(pieceToRemove);
  62.                             Console.WriteLine($"Successfully removed {pieceName}!");
  63.                         }
  64.                         else
  65.                         {
  66.                             Console.WriteLine($"Invalid operation! {pieceName} does not exist in the collection.");
  67.                         }
  68.  
  69.                         break;
  70.  
  71.                     case "ChangeKey":
  72.  
  73.                         string newKey = tokens[2];
  74.  
  75.                         Piece pieceToChange = collection.FirstOrDefault(x => x.Name == pieceName);
  76.  
  77.                         if (pieceToChange == null)
  78.                         {
  79.                             Console.WriteLine($"Invalid operation! {pieceName} does not exist in the collection.");
  80.                         }
  81.                         else
  82.                         {
  83.                             pieceToChange.Key = newKey;
  84.                             Console.WriteLine($"Changed the key of {pieceName} to {newKey}!");
  85.                         }
  86.  
  87.                         break;
  88.                 }
  89.  
  90.                 commandLine = Console.ReadLine();
  91.             }
  92.  
  93.             foreach (Piece piece in collection)
  94.             {
  95.                 Console.WriteLine(piece);
  96.             }
  97.         }
  98.     }
  99.  
  100.     public class Piece
  101.     {
  102.         public Piece(string name, string composer, string key)
  103.         {
  104.             this.Name = name;
  105.             this.Composer = composer;
  106.             this.Key = key;
  107.         }
  108.         public string Name { get; set; }
  109.         public string Composer { get; set; }
  110.         public string Key { get; set; }
  111.         public override string ToString()
  112.         {
  113.             return $"{Name} -> Composer: {Composer}, Key: {Key}";
  114.         }
  115.     }
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement