View difference between Paste ID: WxLHk5AL and GChh5zuh
SHOW: | | - or go back to the newest paste.
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-
  
9+
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-
    val availabilityPeriods = parsed.result/*.filter(_.houseCode == "AT-1010-04")*/.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
}