2023-06-07 15:37:53 +03:00
|
|
|
package OOP.Java.lab_6
|
|
|
|
|
|
|
|
import kotlin.time.Duration
|
|
|
|
|
2023-06-10 20:47:59 +03:00
|
|
|
/**
|
|
|
|
* Represents an album by a specific artist, containing multiple tracks.
|
|
|
|
*
|
|
|
|
* @property albumName The name of the album.
|
|
|
|
* @property artist The artist or band associated with the album.
|
|
|
|
* @property tracks An [Array] of tracks included on the album.
|
|
|
|
*/
|
2023-06-07 15:37:53 +03:00
|
|
|
class Album(val albumName: String, val artist: String, val tracks: Array<Track>) {
|
|
|
|
|
2023-06-10 20:47:59 +03:00
|
|
|
/**
|
|
|
|
* Returns an array of tracks within the specified duration range.
|
|
|
|
*/
|
2023-06-07 15:37:53 +03:00
|
|
|
fun tracksInDurationRange(durationRange: ClosedRange<Duration>): Array<Track> {
|
|
|
|
return this.tracks.filter { it.duration in durationRange }.toTypedArray()
|
|
|
|
}
|
|
|
|
|
2023-06-10 20:47:59 +03:00
|
|
|
/**
|
|
|
|
* Returns the total duration of all tracks on the album.
|
|
|
|
*/
|
2023-06-07 15:37:53 +03:00
|
|
|
fun totalDuration(): Duration {
|
|
|
|
var totalDuration: Duration = Duration.ZERO
|
|
|
|
this.tracks.forEach { totalDuration += it.duration }
|
|
|
|
return totalDuration
|
|
|
|
}
|
2023-06-10 20:47:59 +03:00
|
|
|
|
2023-06-07 15:37:53 +03:00
|
|
|
override fun toString(): String {
|
|
|
|
return "${this.albumName} by ${this.artist}\nTotal duration: ${this.totalDuration()}\nTracks:\n${this.tracks.joinToString("\n")}"
|
|
|
|
}
|
|
|
|
}
|