Advertisement
Kutov

Untitled

Oct 22nd, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. <?php
  2. class Song
  3. {
  4. protected $artist;
  5. protected $song;
  6. protected $length;
  7. /**
  8. * Song constructor.
  9. * @param $artist
  10. * @param $song
  11. * @param $length
  12. * @throws Exception
  13. */
  14. public function __construct($artist, $song, $length)
  15. {
  16. $this->setArtist($artist);
  17. $this->setSong($song);
  18. $this->setLength($length);
  19. }
  20. /**
  21. * @return mixed
  22. */
  23. public function getArtist()
  24. {
  25. return $this->artist;
  26. }
  27. /**
  28. * @param mixed $artist
  29. * @throws Exception
  30. */
  31. public function setArtist($artist): void
  32. {
  33. if (strlen($artist) < 3 || strlen($artist) > 20 ){
  34. throw new Exception('Artist name should be between 3 and 20 symbols.');
  35. }
  36. $this->artist = $artist;
  37. }
  38. /**
  39. * @return mixed
  40. */
  41. public function getSong()
  42. {
  43. return $this->song;
  44. }
  45. /**
  46. * @param mixed $song
  47. * @throws Exception
  48. */
  49. public function setSong($song): void
  50. {
  51. if (strlen($song) < 3 || strlen($song) > 30){
  52.  
  53. throw new Exception('Song name should be between 3 and 30 symbols.');
  54. }
  55. $this->song = $song;
  56. }
  57. /**
  58. * @return mixed
  59. */
  60. public function getLength()
  61. {
  62. return $this->length;
  63. }
  64. /**
  65. * @param mixed $length
  66. * @throws Exception
  67. */
  68. public function setLength($length): void
  69. {
  70. $time = explode(':', $length);
  71. if(isset($time[0])&&is_numeric($time[0])){
  72. $minutes = $time[0];
  73. if ($minutes < 0 || $minutes > 14){
  74. throw new Exception('Song minutes should be between 0 and 14.');
  75. }
  76. }
  77. else{
  78. throw new Exception('Invalid song length.');
  79. }
  80. if(isset($time[1])&&is_numeric($time[1])){
  81. $seconds = $time[1];
  82. if ($seconds < 0 || $seconds > 59){
  83. throw new Exception('Song seconds should be between 0 and 59.');
  84. }
  85. }
  86. else{
  87.  
  88. throw new Exception('Invalid song length.');
  89. }
  90. // var_dump($seconds);
  91. $total = $minutes * 60 + $seconds;
  92. // if ($total < 0 || $total > 899){
  93. // throw new Exception('Invalid song length.'.$total);
  94. // }
  95.  
  96.  
  97. $this->length = $total;
  98. }
  99. }
  100. $num = readline();
  101. $total = 0;
  102. $count = 0;
  103. for ($i = 0; $i < $num; $i++){
  104. $input = explode(';', readline());
  105. $artist = $input[0];
  106. $songName = $input[1];
  107. $length = $input[2];
  108. try{
  109. $song = new Song($artist, $songName, $length);
  110. echo "Song added.\n";
  111. $count++;
  112. $total += $song->getLength();
  113. }
  114. catch (Exception $e){
  115. echo $e->getMessage() . "\n";
  116. }
  117. }
  118. echo "Songs added: " . $count . "\n";
  119. $hours=gmdate("G", $total);
  120. $mins=gmdate("i", $total);
  121. $seconds=gmdate("s", $total);
  122. echo "Playlist length: " . $hours."h ".$mins."m ".$seconds."s";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement