Initial commit
This commit is contained in:
87
src/main/scala/TopSongs/TopSongs.scala
Normal file
87
src/main/scala/TopSongs/TopSongs.scala
Normal file
@ -0,0 +1,87 @@
|
||||
package TopSongs
|
||||
import scala.io.Source
|
||||
import java.io.File
|
||||
import com.github.tototoshi.csv._
|
||||
|
||||
trait Person:
|
||||
val name: String
|
||||
|
||||
case class Artist(name: String) extends Person
|
||||
case class Writer(name: String) extends Person
|
||||
case class Producer(name: String) extends Person
|
||||
class Album(title: String, label: String)
|
||||
class Streak(val s: String) {
|
||||
val streak: Option[Int] = {
|
||||
val regex = "\\d+".r
|
||||
if(s.equalsIgnoreCase("Did not chart")) {
|
||||
Some(0)
|
||||
} else {
|
||||
regex.findFirstIn(s) match {
|
||||
case Some(value) => Some(Integer.parseInt(value))
|
||||
case None => None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
class Position(val p: String) {
|
||||
val position: Option[Int] = {
|
||||
val regex = "\\d+".r
|
||||
regex.findFirstIn(p) match {
|
||||
case Some(value) => Some(Integer.parseInt(value))
|
||||
case None => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Song(
|
||||
val title: String,
|
||||
val description: Option[String],
|
||||
val artist: List[Artist],
|
||||
val writer: List[Writer],
|
||||
val producer: List[Producer],
|
||||
val album: Option[Album],
|
||||
val streak: Streak,
|
||||
val position: Position
|
||||
)
|
||||
|
||||
|
||||
object TopSongs {
|
||||
var songs: List[Song] = List()
|
||||
def addSong(song: Song): Unit = {
|
||||
songs = song :: songs
|
||||
}
|
||||
def printSongs(): Unit = {
|
||||
songs.foreach(song => println(
|
||||
s"${song.title} by ${song.artist.map(_.name).mkString(", ")} " +
|
||||
s"produced by ${song.producer.map(_.name).mkString(", ")} " +
|
||||
s"spent ${song.streak.streak.getOrElse("no")} weeks " +
|
||||
s"on the charts on Pos. ${song.position.position.getOrElse("NA")}"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@main def main(): Unit =
|
||||
// create a new TopSongs object
|
||||
val topSongs = TopSongs
|
||||
|
||||
val reader = CSVReader.open(new File("src/main/resources/songs.csv"))
|
||||
val allRows = reader.allWithHeaders()
|
||||
for (row <- allRows) {
|
||||
val s = Song(
|
||||
title = row("title"),
|
||||
description = Some(row("description")),
|
||||
artist = List(Artist(row("artist"))),
|
||||
writer = List(Writer(row("writers"))),
|
||||
producer = List(Producer(row("producer"))),
|
||||
album = Some(Album(row("appears on"), "NA")),
|
||||
streak = Streak(row("streak")),
|
||||
position = Position(row("position"))
|
||||
)
|
||||
|
||||
// add the song to the TopSongs object
|
||||
topSongs.addSong(s)
|
||||
}
|
||||
reader.close()
|
||||
|
||||
// print the songs
|
||||
topSongs.printSongs()
|
Reference in New Issue
Block a user