From b7c2adae2795bd205520d9c502fa2f559f1c553f Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Wed, 1 May 2024 13:07:56 +0200 Subject: [PATCH] task 1.2-3 --- .gitignore | 1 + Lab18.iml | 2 + src/ch/hevs/isc/slopify_v2/Album.scala | 2 + src/ch/hevs/isc/slopify_v2/Artist.scala | 3 + src/ch/hevs/isc/slopify_v2/DataBase.scala | 2 + .../hevs/isc/slopify_v2/DataBaseHelper.scala | 62 +++++++++++++++++++ 6 files changed, 72 insertions(+) create mode 100644 src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala diff --git a/.gitignore b/.gitignore index 561b953..9ff5fba 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +/res/songs/ diff --git a/Lab18.iml b/Lab18.iml index 1f50b08..6815354 100644 --- a/Lab18.iml +++ b/Lab18.iml @@ -3,7 +3,9 @@ + + diff --git a/src/ch/hevs/isc/slopify_v2/Album.scala b/src/ch/hevs/isc/slopify_v2/Album.scala index 1ba3b5f..8c2a76f 100644 --- a/src/ch/hevs/isc/slopify_v2/Album.scala +++ b/src/ch/hevs/isc/slopify_v2/Album.scala @@ -4,4 +4,6 @@ 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) } diff --git a/src/ch/hevs/isc/slopify_v2/Artist.scala b/src/ch/hevs/isc/slopify_v2/Artist.scala index eeedd64..60325af 100644 --- a/src/ch/hevs/isc/slopify_v2/Artist.scala +++ b/src/ch/hevs/isc/slopify_v2/Artist.scala @@ -4,4 +4,7 @@ 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) + + def hasAlbum(album: Album): Boolean = albums.contains(album) } diff --git a/src/ch/hevs/isc/slopify_v2/DataBase.scala b/src/ch/hevs/isc/slopify_v2/DataBase.scala index c1214a1..c71f47b 100644 --- a/src/ch/hevs/isc/slopify_v2/DataBase.scala +++ b/src/ch/hevs/isc/slopify_v2/DataBase.scala @@ -4,4 +4,6 @@ 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) } diff --git a/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala b/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala new file mode 100644 index 0000000..3a71708 --- /dev/null +++ b/src/ch/hevs/isc/slopify_v2/DataBaseHelper.scala @@ -0,0 +1,62 @@ +package ch.hevs.isc.slopify_v2 + +import java.io.File + +object DataBaseHelper { + def create(directory:String) : DataBase = { + val db = new DataBase() + for (a <- new File(directory).listFiles() if a.isDirectory) { + val artistName = a.getName + println(s"found new artist : $artistName") + val artist: Artist = new Artist(artistName) + + for (b <- a.listFiles() if b.isDirectory ) { + val albumName = b.getName + println(s"found new album $albumName for artist : $artistName") + val album: Album = new Album(albumName) + + for (c <- b.listFiles() if c.isFile if c.getName.toLowerCase().endsWith(".mp3") ) { + val fileName = c.getName.substring(0, c.getName.length - 4) + val format1 = """(\d*) (.*)""".r + val format2 = """(\d*)-(\d*) (.*)""".r + val format3 = """(\d*)-(.*)""".r + var songName: String = "" + var songNumber: Int = 0 + + fileName match { + case format1(nr, name) => { + println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '${a.getName}'") + songName = name + songNumber = Integer.parseInt(nr) + } + case format2(cd, nr, name) => { + println(s"found song nr #$nr on cd#$cd name:'$name' in album '$albumName' for artist : '${a.getName}'") + songName = name + songNumber = Integer.parseInt(nr) + } + case format3(nr, name) => { + println(s"found song nr #$nr name:'$name' in album '$albumName' for artist : '$artistName'") + songName = name + songNumber = Integer.parseInt(nr) + } + } + + val song: Song = new Song(songName, songNumber) + album.addSong(song) + } + artist.addAlbum(album) + } + db.addArtist(artist) + } + + return db + } + + def save(fileName:String, db:DataBase) : Unit = ??? + def load(fileName:String) : DataBase = ??? + + def main(args: Array[String]): Unit = { + val db: DataBase = create("res/songs") + + } +} \ No newline at end of file