diff --git a/src/main/scala/csvreader/CSVReader.scala b/src/main/scala/csvreader/CSVReader.scala index e2d4d4b..cf6fcb5 100644 --- a/src/main/scala/csvreader/CSVReader.scala +++ b/src/main/scala/csvreader/CSVReader.scala @@ -10,19 +10,6 @@ import scala.util.{Failure, Success} * */ object CSVReader extends App { - val file = io.Source.fromFile("people.csv") - var m = Map[String, Int]() - for (line <- file.getLines) { - var state = line.split(",")(2) - if (m.contains(state)) { - m(state) += 1 - } else { - m(state) = 1 - } - } - m.foreach(println) - - val future = Future { - m.foreach(println) - } + val stateMap = new fileReader("people.csv") + stateMap.countStates() } \ No newline at end of file diff --git a/src/main/scala/csvreader/fileReader.scala b/src/main/scala/csvreader/fileReader.scala new file mode 100644 index 0000000..fda1b3c --- /dev/null +++ b/src/main/scala/csvreader/fileReader.scala @@ -0,0 +1,27 @@ +package csvreader + +import scala.collection.mutable.Map +import scala.concurrent.Future +import scala.concurrent.ExecutionContext.Implicits.global +import scala.util.{Failure, Success} + +class fileReader(fileName: String) { + + /** Simple method that parses the input file and counts the number of times each state appears + * @return a map with the state name as the key and the counts as the value + */ + def countStates(): Map[String, Int] = { + val file = io.Source.fromFile(fileName) + var m = Map[String, Int]() + for (line <- file.getLines) { + var state = line.split(",")(2) + if (m.contains(state)) { + m(state) += 1 + } else { + m(state) = 1 + } + } + val future = Future (for ((k, v) <- m) println(s"$k: $v")) + m + } +} \ No newline at end of file