This course has already ended.

⌛⌛ Country data (JSON)

The submission consists of a Maven project. Place your answer into files pom.xml, src/main/java/fi/tuni/prog3/round7/jsoncountries/Country.java and src/main/java/fi/tuni/prog3/round7/jsoncountries/CountryData.java in the directory Round7/jsoncountries. Remember to pull task material from student_template_project.

In this task you will try reading and writing JSON data using Google’s GSON library: https://github.com/google/gson/blob/master/UserGuide.md. Information about what kind of Maven dependency is needed to use the library can be found behind the link.

The task is almost indentical with the task about handling country statistics in XML form: the only difference is that the data is now in JSON format instead of XML. The data is in fact exactly the same; it has just been converted from XML to JSON with an automatic tool. Due to this the structure of the data is somewhat clumsy: the conversion tool has aimed to follow the form of the original XML verbatim.

You should implement the classes Country and CountryData that have at least the following features. You can decide the rest of the details yourself.

  • Class Country stores the name of the country (string), area (double), population (long) and gross domestic product (double).

    • Defined in the package fi.tuni.prog3.round7.jsoncountries (i.e. set the line package fi.tuni.prog3.round7.jsoncountries; into the very beginning of the class).

    • Implements the interface Comparable<Country> so that the comparison is based on the name of the country according to the natural order of the String class.

    • A public member function String toString() that returns a String representation of the country in the form shown in the example outputs: first the name of the country, and after it the area, population and gross national product, each on separate lines and indented with two spaces. Double values with the precision of one decimal.

    • Public member functions String getName(), double getArea(), long getPopulation() and double getGdp() that return the data their names suggest.

  • Class CountryData offers two public static member functions for reading and writing country data in JSON format.

    • Defined in the package fi.tuni.prog3.round7.jsoncountries.

    • List<Country> readFromJsons(String areaFile, String populationFile, String gdpFile) reads country data from the JSON files named by the parameters.

      • The files contain information about the area, population and gross domestic product of the countries as suggested by the file names. Note how the data is in three separate files.

      • Deduct the structure of the files by investigating the example input files. The structure of each file is identical: information for one country is in field objects inside a record object. Each field object has under the key “attributes” a key “name” whose value describes which piece of information the field object contains.

      • The function returns some kind of a list implementing the interface List<Country> that contains the Country objects depicting the read data. You must hence combine the data read from the three JSON files into Country objects.

    • void writeToJson(List<Country> countries, String countryFile) writes the information depicted by Country objects in the list countries in JSON format into the file named by the parameter countryFile.

      • A JSON array that contains one JSON object for each country. Each JSON object depicts the data of one country under the keys name, area, population and gdp.

      • Check the exact output format from the example outputs. They have been created using the setPrettyPrinting() feature of the GSON library.

The automatic tests (and the ones given below) assume that you make the following definitions in your pom.xml project file:

  • The value of artifactId is jsoncountries.

  • The value of version element is 1.0.

  • A onejar plugin definition where the value of mainClass is CountryTest.

    • CountryTest is the name of the given test class (described below).

Testing

There are three test sets available for the task. Below, the character X refers to the number of the test and is 1, 2 or 3.

You can test your classes with the test program given in the file CountryTest.java, the test files named in the form areaX.json, populationX.json and gdpX.json, the example outputs named in the form outputX.txt, and the example result files named in the form resultX.json.

Set CountryTest.java into the root of the src/main/java subdirectory of your Maven project, and the other files into the root directory of your Maven project (where the pom.xml is). Note that CountryTest.java does not include a package definition and therefore is not placed into a deeper subdirectory.

After this you can compile the program with mvn package and run the test X as java -jar target/jsoncountries-1.0.one-jar.jar areaX.json populationX.json gdpX.json countriesX.json. The test number X should produce the output depicted in the corresponding file outputX.txt and create a file countriesX.json whose contents are identical with the file resultX.json.

A+ presents the exercise submission form here.

Posting submission...