Advertisement
dobroslav-atanasov

Untitled

Feb 26th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         List<Song> playlist = new List<Song>();
  10.  
  11.         int count = int.Parse(Console.ReadLine());
  12.  
  13.         for (int index = 0; index < count; index++)
  14.         {
  15.             try
  16.             {
  17.                 string[] inputArr = Console.ReadLine()
  18.                 .Split(';', StringSplitOptions.RemoveEmptyEntries)
  19.                 .ToArray();
  20.  
  21.                 string songName = inputArr[1];
  22.  
  23.                 string singerName = inputArr[0];
  24.  
  25.                 string lenght = inputArr[2];
  26.                 string[] lengthArr = lenght.Split(':').ToArray();
  27.                 int minutes;
  28.                 int seconds;
  29.  
  30.                 if (int.TryParse(lengthArr[0], out minutes) && int.TryParse(lengthArr[1], out seconds))
  31.                 {
  32.                     Song song = new Song(singerName, songName, minutes, seconds);
  33.                     playlist.Add(song);
  34.                     Console.WriteLine("Song added.");
  35.                 }
  36.                 else
  37.                 {
  38.                     throw new InvalidSongLengthException();
  39.                 }
  40.                
  41.             }
  42.             catch (Exception e)
  43.             {
  44.  
  45.                 Console.WriteLine(e.Message);
  46.             }
  47.  
  48.            
  49.         }
  50.  
  51.         int playlistLenghtInSecs = playlist.Sum(p => p.SongLength);
  52.  
  53.         TimeSpan time = TimeSpan.FromSeconds(playlistLenghtInSecs);
  54.         string str = time.ToString(@"hh\:mm\:ss");
  55.         Console.WriteLine($"Songs added: {playlist.Count}");
  56.         Console.WriteLine($"Playlist length: {time.Hours:D1}h {time.Minutes:D1}m {time.Seconds:D1}s");
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement