1.4-Software-Development-Principles

Advanced streams using json to load the data

To test the streams API on real world data we have downloaded and parsed the following JSON file:
https://data.overheid.nl/dataset/kabels-json
into a shorter version of the file: parsed json file.

It is a dataset from the Dutch government which contains all electricity cables in the Hague. Note: The supplied JSON contains only a part of the whole dataset. If you would like the whole dataset, please download it from the URL above.

You should write code that reads the JSON objects into java objects. You can use this data to answer the following questions by using the Java Streams API

code you can use as a template to answer the questions:

package nl.saxion.exercise;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.stream.Collectors;

public class Streams {

    private DB db;

    public static void main(String[] args) {
        Streams s = new Streams();
        s.loadDatabase();
        s.query();
    }

    private void query() {
        System.out.println("Print the ObjectIds of all features");
        //TODO
//        db.features.stream()

        System.out.println("How many cables have crossection \"3 x 10\"?");
        //TODO
//      long nrCablesWithCrossSection3x10 =
//                db.features.stream()
//
//        System.out.println(nrCablesWithCrossSection3x10);

        System.out.println("What is the total length of all cables with status \"In bedrijf\"");
        //TODO
//        double totalLengthInBedrijf = db.features.stream()
//
//        System.out.println(totalLengthInBedrijf);

        System.out.println("Distribution cables cost 145eur per kilometer. What is the total cost of all cables " +
                "with function 'Distributie'");
        //TODO
//        double costEuro = db.features.stream()
//
//        System.out.println("cost: " + costEuro);

        System.out.println("Print all classes of cables which are modified after 01-07-2016");
        //TODO


        System.out.println("Print per material the amount of features made of this material");
        /**
         * Expected result:
         *      Onbekend 6383
         *      Aluminium 1
         *      Koper 13591
         */
        //TODO

        System.out.println("Determine and print the number of different crossections");
        //TODO

        System.out.println("How many cables belong to the category cables with the largest crosssection");
        //TODO

    }

    private void loadDatabase() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);

        try {
            //DB taken from https://data.overheid.nl/dataset/kabels-json
            //Licence https://creativecommons.org/licenses/by/4.0/deed.nl
            db = mapper.readValue(new File("kabels-json.json"), DB.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


Reading the JSON file

This is done by mapping the JSON data onto a java Object of the type DB. It is your job to implement this class and all other classes needed to map the JSON data structure to java data structures


package nl.saxion.exercise;

import com.fasterxml.jackson.annotation.JsonIgnore;

import java.util.List;

public class DB {

    /* TODO: add properties to map the data in the json file */

    @Override
    public String toString() {
        return "DB";
    }
}