Actualiser src/main/scala/TopSongs/TopSongs.scala
This commit is contained in:
parent
046c984634
commit
9446c9d3c1
@ -3,6 +3,8 @@ import scala.io.Source
|
|||||||
import java.io.File
|
import java.io.File
|
||||||
import com.github.tototoshi.csv._
|
import com.github.tototoshi.csv._
|
||||||
|
|
||||||
|
//jpc: seems all ok but can you add more interesting things to make the class structure more interesting?
|
||||||
|
//jpc perhaps adding enum for the Genre or type of band or person, or the sub-genre: hard rock, etc. If you don't have enough data you can add some data yourself just for fun
|
||||||
trait Person:
|
trait Person:
|
||||||
val name: String
|
val name: String
|
||||||
|
|
||||||
@ -10,28 +12,21 @@ case class Artist(name: String) extends Person
|
|||||||
case class Writer(name: String) extends Person
|
case class Writer(name: String) extends Person
|
||||||
case class Producer(name: String) extends Person
|
case class Producer(name: String) extends Person
|
||||||
class Album(title: String, label: String)
|
class Album(title: String, label: String)
|
||||||
class Streak(val s: String) {
|
class Streak(val s: String) :
|
||||||
val streak: Option[Int] = {
|
val streak: Option[Int] =
|
||||||
val regex = "\\d+".r
|
val regex = "\\d+".r
|
||||||
if(s.equalsIgnoreCase("Did not chart")) {
|
if(s.equalsIgnoreCase("Did not chart")) then Some(0)
|
||||||
Some(0)
|
else regex.findFirstIn(s) match
|
||||||
} else {
|
|
||||||
regex.findFirstIn(s) match {
|
|
||||||
case Some(value) => Some(Integer.parseInt(value))
|
case Some(value) => Some(Integer.parseInt(value))
|
||||||
case None => None
|
case None => None
|
||||||
}
|
|
||||||
}
|
class Position(val p: String) :
|
||||||
}
|
val position: Option[Int] =
|
||||||
}
|
|
||||||
class Position(val p: String) {
|
|
||||||
val position: Option[Int] = {
|
|
||||||
val regex = "\\d+".r
|
val regex = "\\d+".r
|
||||||
regex.findFirstIn(p) match {
|
regex.findFirstIn(p) match
|
||||||
case Some(value) => Some(Integer.parseInt(value))
|
case Some(value) => Some(Integer.parseInt(value))
|
||||||
case None => None
|
case None => None
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class Song(
|
class Song(
|
||||||
val title: String,
|
val title: String,
|
||||||
@ -44,21 +39,19 @@ class Song(
|
|||||||
val position: Position
|
val position: Position
|
||||||
)
|
)
|
||||||
|
|
||||||
|
object TopSongs :
|
||||||
object TopSongs {
|
var songs: List[Song] = List() //jpc: consider not using mutable variables
|
||||||
var songs: List[Song] = List()
|
def addSong(song: Song): Unit =
|
||||||
def addSong(song: Song): Unit = {
|
|
||||||
songs = song :: songs
|
songs = song :: songs
|
||||||
}
|
|
||||||
def printSongs(): Unit = {
|
def printSongs(): Unit =
|
||||||
songs.foreach(song => println(
|
songs.foreach(song => println(
|
||||||
s"${song.title} by ${song.artist.map(_.name).mkString(", ")} " +
|
s"${song.title} by ${song.artist.map(_.name).mkString(", ")} " +
|
||||||
s"produced by ${song.producer.map(_.name).mkString(", ")} " +
|
s"produced by ${song.producer.map(_.name).mkString(", ")} " +
|
||||||
s"spent ${song.streak.streak.getOrElse("no")} weeks " +
|
s"spent ${song.streak.streak.getOrElse("no")} weeks " +
|
||||||
s"on the charts on Pos. ${song.position.position.getOrElse("NA")}"
|
s"on the charts on Pos. ${song.position.position.getOrElse("NA")}"
|
||||||
))
|
))
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@main def main(): Unit =
|
@main def main(): Unit =
|
||||||
// create a new TopSongs object
|
// create a new TopSongs object
|
||||||
@ -66,7 +59,7 @@ object TopSongs {
|
|||||||
|
|
||||||
val reader = CSVReader.open(new File("src/main/resources/songs.csv"))
|
val reader = CSVReader.open(new File("src/main/resources/songs.csv"))
|
||||||
val allRows = reader.allWithHeaders()
|
val allRows = reader.allWithHeaders()
|
||||||
for (row <- allRows) {
|
for (row <- allRows) do
|
||||||
val s = Song(
|
val s = Song(
|
||||||
title = row("title"),
|
title = row("title"),
|
||||||
description = Some(row("description")),
|
description = Some(row("description")),
|
||||||
@ -80,7 +73,7 @@ object TopSongs {
|
|||||||
|
|
||||||
// add the song to the TopSongs object
|
// add the song to the TopSongs object
|
||||||
topSongs.addSong(s)
|
topSongs.addSong(s)
|
||||||
}
|
|
||||||
reader.close()
|
reader.close()
|
||||||
|
|
||||||
// print the songs
|
// print the songs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user