I have a function called as "readStock" which should return value of type [String,WeatherReading] whereas "WeatherReading" is object for the case class "WeatherReadingStub". I don't know how to declare and return object for the function "readStock".
Code:-
package HW9
object WeatherStub {
//Setting file name
val fileName = "weather.csv"
//Calling Main
def main(args: Array[String]): Unit = {
val weatherData: Map[String, WeatherReading] = readWeather(fileName)
printWeather(weatherData)
}
def readWeather(fn: String): Map[String,WeatherReading] = {
//Creating a mutable map to collect all the values
val weatherMuteMap = scala.collection.mutable.Map[String, WeatherReading]()
//Checking for empty or null values
def IsEmptyOrNull(s: String): Option[Float] = {
if ((s ne null) && s.trim.length > 0) Some(s.toFloat) else None
}
//Reading file
for (line <- io.Source.fromFile(fn).getLines()) {
val list1 = line.split(",", -1).map(_.trim).toList
//Setting up default values
val TotalPrecp: Option[Float] = IsEmptyOrNull(list1(1).toString) match { case Some(i) => Some(i) case _ => None}
val LowPrecp: Option[Float] = IsEmptyOrNull(list1(2).toString) match { case Some(i) => Some(i) case _ => None}
val HighPrecp: Option[Float] = IsEmptyOrNull(list1(3).toString) match { case Some(i) => Some(i) case _ => None}
//Creating object for the case class WeatherReadingStub
val WeatherReading = WeatherReadingStub(TotalPrecp,LowPrecp,HighPrecp)
//Adding elements to the mutable list
weatherMuteMap(list1(0).toString) = WeatherReading.toString
}
//Converting to Immutbale Map
weatherMuteMap.toMap
}
def printWeather(weatherMap: Map[String, String]): Unit = {
//Format of the message to be printed
println("(Date,{Precipitation,LowTemperature,HighTemperature})\tAverageTemperature")
//Printing the average temp values calculated in Case Class using object
weatherMap.toSeq.sortBy(_._1).foreach(wd => println(wd + "\t" + wd._2.averageTemperature.getOrElse("N/A")))
println
}
}
Case Class Code:-
package HW9
case class WeatherReadingStub(precipitation: Option[Float], lowTemperature: Option[Float], highTemperature: Option[Float]) {
def averageTemperature: Option[Float] = (lowTemperature, highTemperature) match {case (Some(i),Some(j)) => Some((i + j)/2) case _ => None}
override def toString: String = s"{${precipitation.getOrElse("MISSING")},${lowTemperature.getOrElse("MISSING")},${highTemperature.getOrElse("MISSING")}}"
}
check your class name WeatherReadingStub. In Object WeatherStub, the readWeather method builds a mutable Map with key the date (type String) and value is a WeatherReading object. Your class WeatherReadingStub has averageTemperature and you have refer WeatherReadingStub in your MAP[String , WeatherReadingStub]. If you replace WeatherReading with your defined object WeatherReadingStub, This program will run. Your definition of MAP should be val weatherData: Map[String, WeatherReadingStub] = readWeather(fileName) (replace all other occurrences.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.