Guest User

Untitled

a guest
Mar 20th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.44 KB | None | 0 0
  1. package com.wausoft.availability.parser
  2.  
  3. import com.google.gson.Gson
  4. import scala.io.Source
  5. import java.io.PrintWriter
  6. import com.wausoft.availability.model._
  7.  
  8. object Program {
  9.  
  10.   def main(args: Array[String]): Unit = {
  11.     // define paths
  12.     val inPath = "C:\\Users\\Slench\\Desktop\\swissHouseData.json"
  13.     val outPath = "C:\\Users\\Slench\\Desktop\\swissTuples.txt"
  14.      
  15.     println("Getting data") // read data from file and make a string out of it
  16.     val data = Source.fromFile(inPath)("UTF-8").mkString
  17.    
  18.     println("Parsing data") // parse read data to a Response object
  19.     val parsed = new Gson().fromJson(data, classOf[Response])
  20.    
  21.     // filter and flatten from List[List[T]] to List[T]
  22.     val availabilityPeriods = parsed.result/*.filter(_.houseCode == "AT-1010-04")*/.map(_.availabilityPeriod).flatten
  23.    
  24.     // make tuples containing only the important data
  25.     val relevantData = availabilityPeriods.map(i => (i.arrivalDate, i.departureDate, i.onRequest))
  26.    
  27.     // group by first element in tuple and then sort accordingly
  28.     val groupedData = relevantData.groupBy(_._1).toList.sortBy(_._1)
  29.    
  30.     val writer = new PrintWriter(outPath)
  31.    
  32.     println("Writing data")
  33.     for(e <- groupedData) {
  34.       val common  = e._1
  35.       val content = e._2.map(i => (i._2, i._3)).mkString(", ")
  36.       writer.print(s"$common -> {$content}\n")
  37.     }
  38.     println("Finished writing")
  39.     writer.close
  40.   }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment