This commit is contained in:
Louis Heredero 2024-05-01 13:24:03 +02:00
parent b7c2adae27
commit 6d1d53e43c
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 45 additions and 17 deletions

View File

@ -2,8 +2,11 @@ package ch.hevs.isc.slopify_v2
import scala.collection.mutable.ArrayBuffer
class Album(val name: String) {
private var songs: ArrayBuffer[Song] = new ArrayBuffer()
def addSong(song: Song): Unit = songs.addOne(song)
def containsSong(song: Song): Boolean = songs.contains(song)
class Album(val name: String) extends Serializable {
private var _songs: ArrayBuffer[Song] = new ArrayBuffer()
def addSong(song: Song): Unit = _songs.addOne(song)
def containsSong(song: Song): Boolean = _songs.contains(song)
def getSongs(): Array[Song] = _songs.toArray
override def toString: String = s"<Album '$name': ${_songs.length} song(s)>"
}

View File

@ -2,9 +2,12 @@ package ch.hevs.isc.slopify_v2
import scala.collection.mutable.ArrayBuffer
class Artist(val name: String) {
private var albums: ArrayBuffer[Album] = new ArrayBuffer()
def addAlbum(album: Album): Unit = albums.addOne(album)
class Artist(val name: String) extends Serializable {
private var _albums: ArrayBuffer[Album] = new ArrayBuffer()
def addAlbum(album: Album): Unit = _albums.addOne(album)
def hasAlbum(album: Album): Boolean = albums.contains(album)
def hasAlbum(album: Album): Boolean = _albums.contains(album)
def getAlbums(): Array[Album] = _albums.toArray
override def toString: String = s"<Artist '$name': ${_albums.length} album(s)>"
}

View File

@ -2,8 +2,11 @@ package ch.hevs.isc.slopify_v2
import scala.collection.mutable.ArrayBuffer
class DataBase {
private var artists: ArrayBuffer[Artist] = new ArrayBuffer()
def addArtist(artist: Artist): Unit = artists.addOne(artist)
def containsArtist(artist: Artist): Boolean = artists.contains(artist)
class DataBase extends Serializable {
private var _artists: ArrayBuffer[Artist] = new ArrayBuffer()
def addArtist(artist: Artist): Unit = _artists.addOne(artist)
def containsArtist(artist: Artist): Boolean = _artists.contains(artist)
def getArtists(): Array[Artist] = _artists.toArray
override def toString: String = s"<Database: ${_artists.length} artist(s)>"
}

View File

@ -1,6 +1,6 @@
package ch.hevs.isc.slopify_v2
import java.io.File
import java.io.{File, FileInputStream, FileOutputStream, ObjectInput, ObjectInputStream, ObjectOutputStream}
object DataBaseHelper {
def create(directory:String) : DataBase = {
@ -52,11 +52,30 @@ object DataBaseHelper {
return db
}
def save(fileName:String, db:DataBase) : Unit = ???
def load(fileName:String) : DataBase = ???
def save(fileName: String, db: DataBase) : Unit = {
val fos: FileOutputStream = new FileOutputStream(fileName)
val oos: ObjectOutputStream = new ObjectOutputStream(fos)
oos.writeObject(db)
oos.close()
}
def load(fileName:String) : DataBase = {
val fis: FileInputStream = new FileInputStream(fileName)
val ois: ObjectInputStream = new ObjectInputStream(fis)
val db: DataBase = ois.readObject().asInstanceOf[DataBase]
ois.close()
return db
}
def main(args: Array[String]): Unit = {
val db: DataBase = create("res/songs")
println(db)
val artists: Array[Artist] = db.getArtists()
println(artists(0))
save("res/db.bin", db)
val db2: DataBase = load("res/db.bin")
println(db2)
val artists2: Array[Artist] = db2.getArtists()
println(artists2(0))
}
}

View File

@ -1,5 +1,5 @@
package ch.hevs.isc.slopify_v2
class Song(val title: String, val number: Int) {
class Song(val title: String, val number: Int) extends Serializable {
override def toString: String = s"<Song n° $number '$title'>"
}