官术网_书友最值得收藏!

Loading inflation data

Now that we can load some equity data, we need to load inflation data so that we're able to compute inflation-adjusted returns. It is very similar to the loading of equity data.

First, copy cpi_2017.tsv from https://github.com/PacktPublishing/Scala-Programming-Projects/blob/master/Chapter02/retirement-calculator/src/main/resources/cpi.tsv to src/test/resources. Then, create a new unit test called InflationDataSpec in the retcalc package:

package retcalc

import org.scalatest.{Matchers, WordSpec}

class InflationDataSpec extends WordSpec with Matchers {
"InflationData.fromResource" should {
"load CPI data from a tsv file" in {
val data = InflationData.fromResource("cpi_2017.tsv")
data should ===(Vector(
InflationData("2016.09", 241.428),
InflationData("2016.10", 241.729),
InflationData("2016.11", 241.353),
InflationData("2016.12", 241.432),
InflationData("2017.01", 242.839),
InflationData("2017.02", 243.603),
InflationData("2017.03", 243.801),
InflationData("2017.04", 244.524),
InflationData("2017.05", 244.733),
InflationData("2017.06", 244.955),
InflationData("2017.07", 244.786),
InflationData("2017.08", 245.519),
InflationData("2017.09", 246.819)
))
}
}
}

Then, create the corresponding InflationData class and companion object:

package retcalc

import scala.io.Source

case class InflationData(monthId: String, value: Double)

object InflationData {
def fromResource(resource: String): Vector[InflationData] =
Source.fromResource(resource).getLines().drop(1).map { line =>
val fields = line.split("\t")
InflationData(monthId = fields(0), value = fields(1).toDouble)
}.toVector
}
主站蜘蛛池模板: 新乡市| 彩票| 澄迈县| 卢湾区| 崇礼县| 宣化县| 湘阴县| 尖扎县| 山阳县| 贵德县| 莱西市| 博乐市| 全椒县| 合肥市| 道孚县| 富川| 秀山| 库伦旗| 梓潼县| 修文县| 基隆市| 民丰县| 贵南县| 潜江市| 长治市| 高碑店市| 泗水县| 玉山县| 吉隆县| 河池市| 绥芬河市| 额敏县| 金坛市| 岫岩| 安阳市| 石泉县| 辽宁省| 昌吉市| 桐柏县| 图木舒克市| 云安县|