task 1.2-3

This commit is contained in:
Louis Heredero 2024-05-01 13:07:56 +02:00
parent 931e9b43e6
commit b7c2adae27
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
6 changed files with 72 additions and 0 deletions

1
.gitignore vendored
View File

@ -5,3 +5,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
/res/songs/

View File

@ -3,7 +3,9 @@
<component name="NewModuleRootManager" inherit-compiler-output="true"> <component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output /> <exclude-output />
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/res" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/res/songs" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

View File

@ -4,4 +4,6 @@ import scala.collection.mutable.ArrayBuffer
class Album(val name: String) { class Album(val name: String) {
private var songs: ArrayBuffer[Song] = new ArrayBuffer() private var songs: ArrayBuffer[Song] = new ArrayBuffer()
def addSong(song: Song): Unit = songs.addOne(song)
def containsSong(song: Song): Boolean = songs.contains(song)
} }

View File

@ -4,4 +4,7 @@ import scala.collection.mutable.ArrayBuffer
class Artist(val name: String) { class Artist(val name: String) {
private var albums: ArrayBuffer[Album] = new ArrayBuffer() private var albums: ArrayBuffer[Album] = new ArrayBuffer()
def addAlbum(album: Album): Unit = albums.addOne(album)
def hasAlbum(album: Album): Boolean = albums.contains(album)
} }

View File

@ -4,4 +4,6 @@ import scala.collection.mutable.ArrayBuffer
class DataBase { class DataBase {
private var artists: ArrayBuffer[Artist] = new ArrayBuffer() private var artists: ArrayBuffer[Artist] = new ArrayBuffer()
def addArtist(artist: Artist): Unit = artists.addOne(artist)
def containsArtist(artist: Artist): Boolean = artists.contains(artist)
} }

View File

@ -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")
}
}