I'm originally a Java developer taking a stab at some Scala development. One of the main reasons I'm posting is because I know that in Scala you can write your code in a Java (OOP) way and it will run but that makes it bad Scala code.
I was hoping I could get a review of my code and maybe somebody could point out how I could be doing things a bit more in a functional way.
A method to get a set of JSON objects from a directory:
def createJsonSessionSet(filePaths: Set[String]) : Set[JSONObject] = {
val sessions: Set[JSONObject] = Set()
val parser: JSONParser = new JSONParser
for (filePath <- filePaths) {
val jsonObject: JSONObject = parser.parse(new FileReader(filePath)).asInstanceOf[JSONObject]
sessions.add(jsonObject)
}
return sessions
}
A method that lists directories from a file path:
def getListOfJsonFilePaths(file: File): Set[String] = {
val filePaths: Set[String] = Set()
for (file <- file.listFiles) {
filePaths.add(file.getAbsolutePath)
}
return filePaths
}
A method that takes a set of json objects and converts the time:
def convertTimeZone(sessions: Set[JSONObject]): Set[JSONObject] = {
val convertedSessions: Set[JSONObject] = Set()
val parser: JSONParser = new JSONParser
val utcFormat: DateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
val newFormat: DateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")
newFormat.setTimeZone(TimeZone.getTimeZone("Etc/GMT+7"))
for (session <- sessions) {
val date: Date = utcFormat.parse(session.get("UTCStartTime").toString)
implicit val formats = Serialization.formats(NoTypeHints)
val convertedSession = Map(
"SessionId" -> session.get("SessionId").toString,
"UserId" -> session.get("UserId").toString,
"Score" -> session.get("Score").toString,
"ETCStartTime" -> newFormat.format(date)
)
val jsonObject: JSONObject = parser.parse(write(convertedSession)).asInstanceOf[JSONObject]
convertedSessions.add(jsonObject)
}
return convertedSessions
}
A method that returns a set of JSON objects based on a set of JSON objects:
def getOutputData(convertedSessions: Set[JSONObject]): Set[JSONObject] = {
val outputData: Set[JSONObject] = Set()
val distinctUserIds: Set[String] = Set()
val distinctDays: Set[String] = Set()
for (session <- convertedSessions) {
distinctUserIds.add(session.get("UserId").toString)
distinctDays.add(session.get("ETCStartTime").toString.split("T")(0))
}
for (day <- distinctDays) {
for (userId <- distinctUserIds) {
implicit val formats = Serialization.formats(NoTypeHints)
val data = Map(
"UserId" -> userId,
"Day" -> day,
"AverageScore" -> 10
)
val parser: JSONParser = new JSONParser
val jsonObject: JSONObject = parser.parse(write(data)).asInstanceOf[JSONObject]
if (!jsonObject.get("AverageScore").toString.contains("NaN"))
outputData.add(jsonObject)
}
}
return outputData
}
I went about this mini project by first writing it in Java which was easy enough. I had trouble with the next method that returned fine in Java, but not so well in Scala. I think it has something to do with the fact that in Java I used ArrayList
s but Scala not supporting that I used sets.
def getUserDailyAverageScore(userId: String, day: String, sessions: Set[JSONObject]): Double = {
var sum: Double = 0
var sessionSize: Int = 0
for (session <- sessions) {
println("test")
if (session.get("UserId").toString.equals(userId) && session.get("ETCStartTime").toString.startsWith(day)) {
sum += session.get("Score").asInstanceOf[Double]
sessionSize += 1
}
}
return sum / sessionSize
}
From debugging, the code never steps into the if
statement whilst in Java it does.