JulianJulianov

04.ObjectsANDClasses-Songs

Mar 5th, 2020
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. 04.ObjectsANDClasses-Songs
  2. Define a class Song, which holds the following information about songs: Type List, Name and Time.
  3. On the first line you will receive the number of songs - N.
  4. On the next N-lines you will be receiving data in the following format: "{typeList}_{name}_{time}".
  5. On the last line you will receive Type List / "all". Print only the Names of the songs, which are from that Type List / All songs.
  6. Examples
  7. Input                              Output
  8. 3                                  DownTown
  9. favourite_DownTown_3:14            Kiss
  10. favourite_Kiss_4:16                Smooth Criminal
  11. favourite_Smooth Criminal_4:01
  12. favourite  
  13.  
  14. 4                                  Andalouse
  15. favourite_DownTown_3:14            
  16. listenLater_Andalouse_3:24
  17. favourite_In To The Night_3:58
  18. favourite_Live It Up_3:48
  19. listenLater
  20.    
  21. 2                                  Replay
  22. like_Replay_3:15                   Photoshop
  23. ban_Photoshop_3:48
  24. all
  25.  
  26. Solution
  27. Define a class Song with properties: Type List, Name and Time.
  28.  
  29. using System;
  30. using System.Linq;
  31. using System.Collections.Generic;
  32. namespace _04Songs
  33. {
  34.     class Program
  35.     {
  36.         static void Main(string[] args)
  37.         {
  38.             var numberOfSongs = int.Parse(Console.ReadLine());
  39.  
  40.             var songs = new List<Song>();//Създавам празен списък от тип class Song, който да
  41.             for (int i = 1; i <= numberOfSongs; i++)//напълня с всички песни въведени от конзолата!
  42.             {
  43.                 var infoForSongs = Console.ReadLine().Split('_');//Създавам масив за да имам достъп на долните редове
  44.                 var typeList = infoForSongs[0];            // по отделно от елементите му, които
  45.                 var name = infoForSongs[1];                //наименувам с typeList, name и time!
  46.                 var time = infoForSongs[2];
  47.  
  48.                 var createObjectSong = new Song();//Създавам празен обект createObjectSong, който е свързан с class Song
  49.  
  50.                 createObjectSong.TypeList = typeList;//Тук пропъртитата от класа Song взимат, копират(get;) стойностите от
  51.                 createObjectSong.Name = name;          //променливите.
  52.                 createObjectSong.Time = time;
  53.                 songs.Add(createObjectSong);//Тук добавяме към празния списък най-горе трите пропъртита от класа Song към обекта Song
  54.             }
  55.             var type = Console.ReadLine();
  56.             if (type == "all")
  57.             {
  58.                 foreach (Song song in songs)
  59.                 {
  60.                     Console.WriteLine(song.Name);
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 foreach  (Song song in songs)
  66.                 {
  67.                     if (song.TypeList == type)
  68.                     {
  69.                         Console.WriteLine(song.Name);
  70.                     }
  71.                 }
  72.             }
  73.             //Може и така за else:
  74.            // var filteredSongs = songs.Where(x => x.TypeList == type).ToList();
  75.            // foreach (Song songList in filteredSongs)
  76.            // {
  77.            //   Console.WriteLine(songList.Name);    
  78.            // }
  79.            
  80.         }
  81.     }
  82.     class Song
  83.     {
  84.         public string TypeList {get; set;}
  85.         public string Name {get; set;}
  86.         public string Time {get; set;}
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment