feat: add genre filtering and longest streak display in TopSongs

This commit is contained in:
Rémi Heredero 2025-04-07 13:05:13 +02:00
parent 3d52397ef8
commit 7f01af5d93
Signed by: Klagarge
GPG Key ID: 735B36B074A65F0F

View File

@ -31,7 +31,11 @@ enum MusicGenre:
case Rock, Soul, Blues, Folk, Funk, Reggae, HipHop, NewWave, Electro, Metal case Rock, Soul, Blues, Folk, Funk, Reggae, HipHop, NewWave, Electro, Metal
// Define an Album with a union between a String and a MusicGenre // 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) { class Streak(val s: String) {
val streak: Option[Int] = { 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 // This function counts the number of songs by each artist
// It uses flatMap to extract the artist names from the songs // It uses flatMap to extract the artist names from the songs
// Then it groups the songs by artist name and counts the occurrences // 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") 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("----------") println("----------")
// print the top N songs by weeks on chart // print the top N songs by weeks on chart