Ejemplo n.º 1
0
        public void Run()
        {
            int numberOfSongs = int.Parse(Console.ReadLine());

            for (int i = 0; i < numberOfSongs; i++)
            {
                string[] song = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);

                if (song.Length != 3)
                {
                    Exception fe = new InvalidSongException();
                    Console.WriteLine(fe.Message);
                    continue;
                }

                string   artist   = song[0];
                string   songName = song[1];
                string[] timeSong = song[2].Split(":");

                int songMinutes = 0;
                int songSeconds = 0;

                bool isMinutes = int.TryParse(timeSong[0], out songMinutes);
                bool isSeconds = int.TryParse(timeSong[1], out songSeconds);
                if (!isMinutes)
                {
                    Exception fe = new InvalidSongLengthException();
                    Console.WriteLine(fe.Message);
                    continue;
                }
                if (!isSeconds)
                {
                    Exception fe = new InvalidSongLengthException();
                    Console.WriteLine(fe.Message);
                    continue;
                }

                Song currentSong = new Song(artist, songName, songMinutes, songSeconds);

                if (currentSong.isValidSong) //Add only valid songs to a collection of songs
                {
                    playlist.Add(currentSong);
                    Console.WriteLine("Song added.");
                }
            }

            var      playlistLengthInSeconds = playlist.Sum(x => x.Minutes * 60) + playlist.Sum(x => x.Seconds); //Sum songs times in seconds
            TimeSpan playlistTime            = TimeSpan.FromSeconds(playlistLengthInSeconds);                    //Convert all seconds to hh mm ss
            int      hours   = playlistTime.Hours;
            int      minutes = playlistTime.Minutes;
            int      seconds = playlistTime.Seconds;

            Console.WriteLine($"Songs added: {playlist.Count}");
            Console.WriteLine($"Playlist length: {hours}h {minutes}m {seconds}s");
        }
Ejemplo n.º 2
0
        public void Run()
        {
            int n            = int.Parse(Console.ReadLine());
            int totalSeconds = 0;
            int songsCounter = 0;

            for (int i = 0; i < n; i++)
            {
                string[] data       = Console.ReadLine().Split(";", StringSplitOptions.RemoveEmptyEntries);
                string   artistName = data[0];
                string   songName   = data[1];
                int      minutes    = 0;
                int      seconds    = 0;

                bool isMinutes = int.TryParse(data[2].Split(":")[0], out minutes);
                bool isSeconds = int.TryParse(data[2].Split(":")[1], out seconds);

                if (!isMinutes || !isSeconds)
                {
                    InvalidSongLengthException ex = new InvalidSongLengthException();
                    Console.WriteLine(ex.Message);
                    continue;
                }

                try
                {
                    Song radio = new Song(artistName, songName, minutes, seconds);
                    Console.WriteLine("Song added.");
                    totalSeconds += (minutes * 60) + seconds;
                    songsCounter++;
                }
                catch (Exception ex)
                {
                    if (ex is InvalidSongException || ex is InvalidArtistNameException ||
                        ex is InvalidSongNameException || ex is InvalidSongLengthException ||
                        ex is InvalidSongMinutesException || ex is InvalidSongSecondsException)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            Console.WriteLine($"Songs added: {songsCounter}");
            var time = TimeSpan.FromSeconds(totalSeconds);

            Console.WriteLine($"Playlist length: {time.Hours}h {time.Minutes}m {time.Seconds}s");
        }