Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class Piece
- {
- public string Name { get; set; }
- public string Composer { get; set; }
- public string Key { get; set; }
- public Piece(string name, string composer, string key)
- {
- this.Key = key;
- this.Name = name;
- this.Composer = composer;
- }
- }
- class Program
- {
- static void Main()
- {
- List<Piece> pieces = new List<Piece>();
- int numberOfPieces = int.Parse(Console.ReadLine());
- for (int i = 0; i < numberOfPieces; i++)
- {
- string[] pieceInfo = Console.ReadLine().Split('|');
- pieces.Add(new Piece(pieceInfo[0], pieceInfo[1], pieceInfo[2]));
- }
- string input;
- while ((input = Console.ReadLine()) != "Stop")
- {
- string[] tokens = input.Split('|');
- switch (tokens[0])
- {
- case "Add":
- Add(pieces, tokens); break;
- case "Remove":
- Remove(pieces, tokens); break;
- case "ChangeKey":
- ChangeKey(pieces, tokens); break;
- }
- }
- Console.WriteLine(String.Join("\n", pieces
- .Select(x => $"{x.Name} -> Composer: {x.Composer}, Key: {x.Key}")));
- }
- private static void ChangeKey(List<Piece> pieces, string[] tokens)
- {
- if (pieces.Any(x => x.Name == tokens[1]))
- {
- pieces.First(x => x.Name == tokens[1]).Key = tokens[2];
- Console.WriteLine($"Changed the key of {tokens[1]} to {tokens[2]}!");
- }
- else
- Console.WriteLine($"Invalid operation! {tokens[1]} does not exist in the collection.");
- }
- private static void Remove(List<Piece> pieces, string[] tokens)
- {
- if (pieces.Any(x => x.Name == tokens[1]))
- {
- Piece pieceToRemove = pieces.Find(x => x.Name == tokens[1]);
- Console.WriteLine($"Successfully removed {pieceToRemove.Name}!");
- pieces.Remove(pieceToRemove);
- }
- else
- Console.WriteLine($"Invalid operation! {tokens[1]} does not exist in the collection.");
- }
- private static void Add(List<Piece> pieces, string[] tokens)
- {
- if (pieces.Any(x => x.Name == tokens[1]))
- Console.WriteLine($"{tokens[1]} is already in the collection!");
- else
- {
- pieces.Add(new Piece(tokens[1], tokens[2], tokens[3]));
- Console.WriteLine($"{tokens[1]} by {tokens[2]} in {tokens[3]} added to the collection!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement