diff --git a/src/main/scala/TopSongs/TopSongs.scala b/src/main/scala/TopSongs/TopSongs.scala index 888fbef..29e1fb8 100644 --- a/src/main/scala/TopSongs/TopSongs.scala +++ b/src/main/scala/TopSongs/TopSongs.scala @@ -31,7 +31,11 @@ enum MusicGenre: case Rock, Soul, Blues, Folk, Funk, Reggae, HipHop, NewWave, Electro, Metal // Define an Album with a union between a String and a MusicGenre -class Album(title: String, label: String, genre: String|MusicGenre) +class Album(title: String, label: String, genre: String|MusicGenre) { + def getGenre(): String | MusicGenre = { + genre + } +} class Streak(val s: String) { val streak: Option[Int] = { @@ -102,6 +106,11 @@ case class TopSongs(songs: List[Song] = List()) { }) } + // This function filter the songs by a specific music gender + def filterByGenre(genre: MusicGenre): List[Song] = { + songs.filter(song => song.album.exists(_.getGenre() == genre)) + } + // This function counts the number of songs by each artist // It uses flatMap to extract the artist names from the songs // Then it groups the songs by artist name and counts the occurrences @@ -165,7 +174,12 @@ case class TopSongs(songs: List[Song] = List()) { case (artist, count) => println(s"$artist: $count") } - println("----------"); + println("----------") + + // print the longest streak + val longestStreakSong = topSongs.longestStreak() + println(s"Longest streak: ${longestStreakSong.title} by ${longestStreakSong.singer.map(_.person.name).mkString(", ")} - ${longestStreakSong.rank._1.streak.getOrElse(0)} weeks") + println("----------") // print the top N songs by weeks on chart