task 2
This commit is contained in:
parent
b7c2adae27
commit
6d1d53e43c
@ -2,8 +2,11 @@ package ch.hevs.isc.slopify_v2
|
|||||||
|
|
||||||
import scala.collection.mutable.ArrayBuffer
|
import scala.collection.mutable.ArrayBuffer
|
||||||
|
|
||||||
class Album(val name: String) {
|
class Album(val name: String) extends Serializable {
|
||||||
private var songs: ArrayBuffer[Song] = new ArrayBuffer()
|
private var _songs: ArrayBuffer[Song] = new ArrayBuffer()
|
||||||
def addSong(song: Song): Unit = songs.addOne(song)
|
def addSong(song: Song): Unit = _songs.addOne(song)
|
||||||
def containsSong(song: Song): Boolean = songs.contains(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)>"
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,12 @@ package ch.hevs.isc.slopify_v2
|
|||||||
|
|
||||||
import scala.collection.mutable.ArrayBuffer
|
import scala.collection.mutable.ArrayBuffer
|
||||||
|
|
||||||
class Artist(val name: String) {
|
class Artist(val name: String) extends Serializable {
|
||||||
private var albums: ArrayBuffer[Album] = new ArrayBuffer()
|
private var _albums: ArrayBuffer[Album] = new ArrayBuffer()
|
||||||
def addAlbum(album: Album): Unit = albums.addOne(album)
|
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)>"
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,11 @@ package ch.hevs.isc.slopify_v2
|
|||||||
|
|
||||||
import scala.collection.mutable.ArrayBuffer
|
import scala.collection.mutable.ArrayBuffer
|
||||||
|
|
||||||
class DataBase {
|
class DataBase extends Serializable {
|
||||||
private var artists: ArrayBuffer[Artist] = new ArrayBuffer()
|
private var _artists: ArrayBuffer[Artist] = new ArrayBuffer()
|
||||||
def addArtist(artist: Artist): Unit = artists.addOne(artist)
|
def addArtist(artist: Artist): Unit = _artists.addOne(artist)
|
||||||
def containsArtist(artist: Artist): Boolean = artists.contains(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)>"
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package ch.hevs.isc.slopify_v2
|
package ch.hevs.isc.slopify_v2
|
||||||
|
|
||||||
import java.io.File
|
import java.io.{File, FileInputStream, FileOutputStream, ObjectInput, ObjectInputStream, ObjectOutputStream}
|
||||||
|
|
||||||
object DataBaseHelper {
|
object DataBaseHelper {
|
||||||
def create(directory:String) : DataBase = {
|
def create(directory:String) : DataBase = {
|
||||||
@ -52,11 +52,30 @@ object DataBaseHelper {
|
|||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
def save(fileName:String, db:DataBase) : Unit = ???
|
def save(fileName: String, db: DataBase) : Unit = {
|
||||||
def load(fileName:String) : DataBase = ???
|
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 = {
|
def main(args: Array[String]): Unit = {
|
||||||
val db: DataBase = create("res/songs")
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
package ch.hevs.isc.slopify_v2
|
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'>"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user