Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 04.ObjectsANDClasses-Songs
- Define a class Song, which holds the following information about songs: Type List, Name and Time.
- On the first line you will receive the number of songs - N.
- On the next N-lines you will be receiving data in the following format: "{typeList}_{name}_{time}".
- 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.
- Examples
- Input Output
- 3 DownTown
- favourite_DownTown_3:14 Kiss
- favourite_Kiss_4:16 Smooth Criminal
- favourite_Smooth Criminal_4:01
- favourite
- 4 Andalouse
- favourite_DownTown_3:14
- listenLater_Andalouse_3:24
- favourite_In To The Night_3:58
- favourite_Live It Up_3:48
- listenLater
- 2 Replay
- like_Replay_3:15 Photoshop
- ban_Photoshop_3:48
- all
- Solution
- Define a class Song with properties: Type List, Name and Time.
- using System;
- using System.Linq;
- using System.Collections.Generic;
- namespace _04Songs
- {
- class Program
- {
- static void Main(string[] args)
- {
- var numberOfSongs = int.Parse(Console.ReadLine());
- var songs = new List<Song>();//Създавам празен списък от тип class Song, който да
- for (int i = 1; i <= numberOfSongs; i++)//напълня с всички песни въведени от конзолата!
- {
- var infoForSongs = Console.ReadLine().Split('_');//Създавам масив за да имам достъп на долните редове
- var typeList = infoForSongs[0]; // по отделно от елементите му, които
- var name = infoForSongs[1]; //наименувам с typeList, name и time!
- var time = infoForSongs[2];
- var createObjectSong = new Song();//Създавам празен обект createObjectSong, който е свързан с class Song
- createObjectSong.TypeList = typeList;//Тук пропъртитата от класа Song взимат, копират(get;) стойностите от
- createObjectSong.Name = name; //променливите.
- createObjectSong.Time = time;
- songs.Add(createObjectSong);//Тук добавяме към празния списък най-горе трите пропъртита от класа Song към обекта Song
- }
- var type = Console.ReadLine();
- if (type == "all")
- {
- foreach (Song song in songs)
- {
- Console.WriteLine(song.Name);
- }
- }
- else
- {
- foreach (Song song in songs)
- {
- if (song.TypeList == type)
- {
- Console.WriteLine(song.Name);
- }
- }
- }
- //Може и така за else:
- // var filteredSongs = songs.Where(x => x.TypeList == type).ToList();
- // foreach (Song songList in filteredSongs)
- // {
- // Console.WriteLine(songList.Name);
- // }
- }
- }
- class Song
- {
- public string TypeList {get; set;}
- public string Name {get; set;}
- public string Time {get; set;}
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment