Advertisement
Slench255

PrettyPrinter for @Leisure Availability data

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