bajro
bajro

Reputation: 1250

Scala filter and print a class

So I have defined here a class:

class CsvEntry(val organisation: String, val yearAndQuartal : String, val medKF:Int, val trueOrFalse: Int, val name: String, val money:String){
  override def toString = s"$organisation, $yearAndQuartal, $medKF, $trueOrFalse, $name, $money"
}

Lets assume the value of "medKF" is wheter 2,4 or 31. How can I filter all CsvEntries to only give me an output for these where the "medKF" is 2? (with foreach println)?

Upvotes: 0

Views: 670

Answers (1)

marios
marios

Reputation: 8996

Creating two random entries:

val a = new CsvEntry("s", "11", 12, 0, "asdasd", "1")
val b = new CsvEntry("s", "11", 2, 0, "asdasd", "234,123.23")

Then filter:

List(a,b).withFilter(_.medKF == 2).foreach(println)

To sum those entries:

List(a,b).map(_.medKF).sum

To add the money:

 def moneyToCent(money: String): Long = (money.replace(",","").toDouble*100).toLong
 List(a,b).withFilter(_.medKF == 2).map(x => moneyToCent(x.money)).sum

Upvotes: 1

Related Questions