Kyaria icon

Praktikum 2 - The Playlist V1.3

Kyaria | PRO | 05/20/19 03:22:59 PM UTC | 0 ⭐ | 10614 👁️ | Never ⏰ | []
Kotlin |

6.04 KB

|

None

|

0 👍

/

0 👎

import java.util.*
 
fun main(args: Array<String>) {
 
    val verwaltung = Musikverwaltung()
 
    val song1 = Song("Led Zeppelin", "Led Zeppelin", 180, 90)
    val song2 = Song("Led Zeppelin II", "Led Zeppelin", 147, 80)
    val song3 = Song("Led Zeppelin III", "Led Zeppelin", 260, 72)
    val song4 = Song("[Led Zeppelin IV]", "Led Zeppelin", 451, 85)
    val song5 = Song("Houses of the Holy", "Led Zeppelin", 332, 94)
    val song6 = Song("Physical Graffiti", "Led Zeppelin", 174, 100)
    val song7 = Song("Presence", "Led Zeppelin", 80, 67)
    val song8 = Song("In Through the Out Door", "Led Zeppelin", 270, 93)
    val song9 = Song("Coda", "Led Zeppelin", 130, 91)
    val song10 = Song("Taylor Swift", "Taylor Swift", 165, 87)
    val song11 = Song("Fearless", "Taylor Swift", 75, 67)
    val song12 = Song("Speak Now", "Taylor Swift", 157, 53)
    val song13 = Song("Red", "Taylor Swift", 170, 85)
    val song14 = Song("1989", "Taylor Swift", 230, 96)
    val song15 = Song("reputation", "Taylor Swift", 90, 68)
    val song16 = Song("One of the Boys", "Katy Perry", 170, 67)
    val song17 = Song("Teenage Dream", "Katy Perry", 155, 87)
    val song18 = Song("PRISM", "Katy Perry", 93, 99)
    val song19 = Song("Witness", "Katy Perry", 162, 48)
    val song20 = Song("Ur So Gay", "Katy Perry", 113, 59)
 
    println("Die Bewerung von: ${song1.titel} by ${song1.interpret} (Bewertung: ${song1.bewertung} Punkte)")
    song1.change(50)
    println("Die Bewerung von: ${song1.titel} by ${song1.interpret} (Bewertung: ${song1.bewertung} Punkte)")
    song1.change(90)
 
    verwaltung.hinzufuegen(song1)
    verwaltung.hinzufuegen(song2)
    verwaltung.hinzufuegen(song3)
    verwaltung.hinzufuegen(song4)
    verwaltung.hinzufuegen(song5)
    verwaltung.hinzufuegen(song6)
    verwaltung.hinzufuegen(song7)
    verwaltung.hinzufuegen(song8)
    verwaltung.hinzufuegen(song9)
    verwaltung.hinzufuegen(song10)
    verwaltung.hinzufuegen(song11)
    verwaltung.hinzufuegen(song12)
    verwaltung.hinzufuegen(song13)
    verwaltung.hinzufuegen(song14)
    verwaltung.hinzufuegen(song15)
    verwaltung.hinzufuegen(song16)
    verwaltung.hinzufuegen(song17)
    verwaltung.hinzufuegen(song18)
    verwaltung.hinzufuegen(song19)
    verwaltung.hinzufuegen(song20)
 
    val newPlaylist = Playlist(mutableListOf<Song>(song1, song2, song3))
    val zufallPlaylist: Playlist = verwaltung.zufallPlaylist()
 
    newPlaylist.alleAbspielen()
    println("Spieldauer von newPlaylist: ${newPlaylist.gesamteSpieldauer()} Sekunden, ${String.format("%.2f",newPlaylist.gesamteSpieldauer() / 60.0)} Minuten")
 
    println("-----SEPERATOR-----")
 
    zufallPlaylist.alleAbspielen()
    println("Spieldauer von zufallPlaylist: ${zufallPlaylist.gesamteSpieldauer()} Sekunden, ${String.format("%.2f",zufallPlaylist.gesamteSpieldauer() / 60.0)} Minuten")
 
    val besterSong: Song = verwaltung.besterSong()
 
    println("Der beste Song in der Datenbank ist: ${besterSong.titel}, ${besterSong.interpret}, ${besterSong.spieldauer / 60.0}, ${besterSong.bewertung}")
 
    do{
 
        println("Bitte geben Sie einen Suchbegriff ein!")
        val eingabe = readLine().toString()
 
        if(eingabe != "stopp") {
            val seekSong = verwaltung.suchen(eingabe)
            seekSong.abspielen()
        }
    }
    while(eingabe != "stopp")
 
}
 
class Song(val titel: String, val interpret: String, val spieldauer: Int, _bewertung: Int) {
 
    var bewertung = _bewertung
        set(value) {
            if (value < 0)
                field = 0
            else if (value > 100)
                field = 100
            else
                field = value
        }
 
    fun abspielen() {
 
        var decke: Int
 
        if (spieldauer % 60 == 0)
            decke = spieldauer / 60
        else
            decke = spieldauer / 60 + 1
 
        for (i in 1..decke)
            println("Spiele: $titel von $interpret (Bewertung: $bewertung Punkte)")
    }
 
    fun change(newValue: Int) {
 
        if (newValue < 0)
            bewertung = 0
        else if (newValue > 100)
            bewertung = 100
        else
            bewertung = newValue
    }
 
    fun search(searchString: String): Boolean {
 
        var istDrin = false
 
        if (titel.contains(searchString))
            istDrin = true
 
        if (interpret.contains(searchString))
            istDrin = true
 
        return istDrin
    }
 
}
 
class Musikverwaltung() {
 
    val songliste: MutableList<Song> = mutableListOf<Song>()
 
    fun hinzufuegen(song: Song) {
 
        songliste.add(song)
    }
 
    fun suchen(begriff: String): Song{
 
        for(s in songliste){
 
            if(s.titel.contains(begriff) || s.interpret.contains(begriff)) {
 
                return s
            }
        }
 
        return songliste.get(0)
    }
 
    fun besterSong(): Song{
 
        var tmp = songliste.get(0)
 
        for(s in songliste){
 
            if(s.bewertung > tmp.bewertung)
                tmp = s
        }
 
        return tmp
    }
 
    fun zufallPlaylist(): Playlist{
 
        val zufallGen = Random()
 
        val newSongliste: MutableList<Song> = mutableListOf<Song>()
        var laengePlaylist = zufallGen.nextInt(songliste.size)
 
        var istVorhanden = false
 
        for(i in 0..laengePlaylist){
 
            for (s in newSongliste){
 
                if(songliste.get(zufallGen.nextInt(songliste.size)) == s)
                    istVorhanden = true
            }
 
            if(!istVorhanden)
                newSongliste.add(songliste.get(zufallGen.nextInt(songliste.size)))
            else
                laengePlaylist++
        }
 
        return Playlist(newSongliste)
    }
}
 
class Playlist(val songliste: MutableList<Song>) {
 
    fun gesamteSpieldauer(): Int {
 
        var temp = 0
 
        for (s in songliste) {
 
            temp += s.spieldauer
        }
 
        return temp
    }
 
    fun alleAbspielen() {
 
        for (s in songliste) {
 
            s.abspielen()
        }
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎