From 6d1d53e43c21342b989b14ab232a5893ab6f8a2e Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 1 May 2024 13:24:03 +0200 Subject: [PATCH] task 2 --- src/ch/hevs/isc/slopify_v2/Album.scala | 11 +++++--- src/ch/hevs/isc/slopify_v2/Artist.scala | 11 +++++--- src/ch/hevs/isc/slopify_v2/DataBase.scala | 11 +++++--- .../hevs/isc/slopify_v2/DataBaseHelper.scala | 25 ++++++++++++++++--- src/ch/hevs/isc/slopify_v2/Song.scala | 4 +-- 5 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/ch/hevs/isc/slopify_v2/Album.scala b/src/ch/hevs/isc/slopify_v2/Album.scala index 8c2a76f..63cb5ed 100644 --- a/src/ch/hevs/isc/slopify_v2/Album.scala +++ b/src/ch/hevs/isc/slopify_v2/Album.scala @@ -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"" } diff --git a/src/ch/hevs/isc/slopify_v2/Artist.scala b/src/ch/hevs/isc/slopify_v2/Artist.scala index 60325af..5d24378 100644 --- a/src/ch/hevs/isc/slopify_v2/Artist.scala +++ b/src/ch/hevs/isc/slopify_v2/Artist.scala @@ -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"" } diff --git a/src/ch/hevs/isc/slopify_v2/DataBase.scala b/src/ch/hevs/isc/slopify_v2/DataBase.scala index c71f47b..8b630f1 100644 --- a/src/ch/hevs/isc/slopify_v2/DataBase.scala +++ b/src/ch/hevs/isc/slopify_v2/DataBase.scala @@ -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"" } diff --git a/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala b/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala index 3a71708..f0ccc62 100644 --- a/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala +++ b/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala @@ -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)) } } \ No newline at end of file diff --git a/src/ch/hevs/isc/slopify_v2/Song.scala b/src/ch/hevs/isc/slopify_v2/Song.scala index e16241a..ac11275 100644 --- a/src/ch/hevs/isc/slopify_v2/Song.scala +++ b/src/ch/hevs/isc/slopify_v2/Song.scala @@ -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"" }