Lab 6, working, undocumented.

This commit is contained in:
Rhinemann 2023-06-07 15:37:53 +03:00
parent 59f315cacc
commit f68dc1a8a7
7 changed files with 105 additions and 0 deletions

19
Java/lab_6/Album.kt Normal file
View File

@ -0,0 +1,19 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class Album(val albumName: String, val artist: String, val tracks: Array<Track>) {
fun tracksInDurationRange(durationRange: ClosedRange<Duration>): Array<Track> {
return this.tracks.filter { it.duration in durationRange }.toTypedArray()
}
fun totalDuration(): Duration {
var totalDuration: Duration = Duration.ZERO
this.tracks.forEach { totalDuration += it.duration }
return totalDuration
}
override fun toString(): String {
return "${this.albumName} by ${this.artist}\nTotal duration: ${this.totalDuration()}\nTracks:\n${this.tracks.joinToString("\n")}"
}
}

7
Java/lab_6/DancePop.kt Normal file
View File

@ -0,0 +1,7 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class DancePop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Dance-pop"
}

7
Java/lab_6/ElectroPop.kt Normal file
View File

@ -0,0 +1,7 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class ElectroPop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Electropop"
}

7
Java/lab_6/Interlude.kt Normal file
View File

@ -0,0 +1,7 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class Interlude(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Interlude"
}

7
Java/lab_6/SynthPop.kt Normal file
View File

@ -0,0 +1,7 @@
package OOP.Java.lab_6
import kotlin.time.Duration
class SynthPop(numberInAlbum: Int, trackName: String, feature: String?, duration: Duration): Track(numberInAlbum, trackName, feature, duration) {
override val style = "Synth-pop"
}

15
Java/lab_6/Track.kt Normal file
View File

@ -0,0 +1,15 @@
package OOP.Java.lab_6
import kotlin.time.Duration
open class Track(val numberInAlbum: Int, val trackName: String, val feature: String?, val duration: Duration) {
open val style = ""
override fun toString(): String {
return if (this.feature == null) {
"#${this.numberInAlbum}: ${this.trackName} (${this.duration})"
} else {
"#${this.numberInAlbum}: ${this.trackName} ft. ${this.feature} (${this.duration})"
}
}
}

43
Java/lab_6/main.kt Normal file
View File

@ -0,0 +1,43 @@
package OOP.Java.lab_6
import kotlin.time.Duration.Companion.minutes
import kotlin.time.Duration.Companion.seconds
fun main() {
val chromatica = Album(
"Chromatica",
"Lady Gaga",
arrayOf(
Interlude(1, "Chromatica I", null, 1.minutes),
SynthPop(2, "Alice", null, 2.minutes + 57.seconds),
DancePop(3, "Stupid Love", null, 3.minutes + 13.seconds),
DancePop(4, "Rain On Me", "Ariana Grande", 3.minutes + 2.seconds),
SynthPop(5, "Free Woman", null, 3.minutes + 11.seconds),
ElectroPop(6, "Fun Tonight", null, 2.minutes + 53.seconds),
Interlude(7, "Chromatica II", null, 41.seconds),
SynthPop(8, "911", null, 2.minutes + 52.seconds),
ElectroPop(9, "Plastic Doll", null, 3.minutes + 41.seconds),
DancePop(10, "Sour Candy", "Blackpink", 2.minutes + 37.seconds),
DancePop(11, "Enigma", null, 2.minutes + 59.seconds),
SynthPop(12, "Replay", null, 3.minutes + 6.seconds),
Interlude(13, "Chromatica III", null, 27.seconds),
ElectroPop(14, "Sine From Above", "Elton John", 4.minutes + 4.seconds),
SynthPop(15, "1000 Doves", null, 3.minutes + 35.seconds),
DancePop(16, "Babylon", null, 2.minutes + 41.seconds)
)
)
println("$chromatica\n")
chromatica.tracks.sortBy { it.style }
println("Album sorted by musical style:\n$chromatica\n")
val durationRange = 1.minutes.. 3.minutes + 30.seconds
println(
"${chromatica.albumName} tracks in a duration range ($durationRange):\n${chromatica.tracksInDurationRange(durationRange).joinToString("\n")}"
)
}