Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor to fileReader class with method #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions src/main/scala/csvreader/CSVReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
27 changes: 27 additions & 0 deletions src/main/scala/csvreader/fileReader.scala
Original file line number Diff line number Diff line change
@@ -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
}
}