interface Playable {
fun play(): String
fun stop(): String = "Воспроизведение остановлено"
}
sealed class MediaItem : Playable {
data class Song(
val title: String,
val artist: String,
val durationSec: Int,
val genre: String
) : MediaItem() {
override fun play(): String = "Играет: $title — $artist [$genre]"
}
data class Podcast(
val title: String,
val host: String,
val durationSec: Int,
val episodeNumber: Int
) : MediaItem() {
override fun play(): String = "Подкаст $episodeNumber: $title — ведущий $host"
}
data class Audiobook(
val title: String,
val author: String,
val durationSec: Int,
val chapter: Int
) : MediaItem() {
override fun play(): String = "Аудиокнига $author — $title, глава $chapter"
}
}
object PlaylistManager {
fun createDefaultPlaylist(): List<MediaItem> {
return listOf(
MediaItem.Song("Bohemian Rhapsody", "Queen", 354, "Rock"),
MediaItem.Song("Shape of You", "Ed Sheeran", 233, "Pop"),
MediaItem.Podcast("IT Новости", "Алексей", 1800, 42),
MediaItem.Podcast("Дизайн сегодня", "Мария", 2400, 15),
MediaItem.Audiobook("Мастер и Маргарита", "М. Булгаков", 1200, 1),
MediaItem.Audiobook("1984", "Дж. Оруэлл", 1500, 3)
)
}
fun findByTitle(playlist: List<MediaItem>, query: String): List<MediaItem> {
return playlist.filter { item ->
when (item) {
is MediaItem.Song -> item.title.contains(query, ignoreCase = true)
is MediaItem.Podcast -> item.title.contains(query, ignoreCase = true)
is MediaItem.Audiobook -> item.title.contains(query, ignoreCase = true)
}
}
}
}
fun main() {
val playlist = PlaylistManager.createDefaultPlaylist()
println("=== Воспроизведение треков ===")
for (item in playlist) {
println(item.play())
println(item.stop())
}
val totalDurationSec = playlist.sumOf { item ->
when (item) {
is MediaItem.Song -> item.durationSec
is MediaItem.Podcast -> item