Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/main/java/io/codelex/assignments/restcountries/Country.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package io.codelex.assignments.restcountries;

import java.util.HashSet;
import java.util.Set;

public class Country {
private final String name;
private String capital;
private int population;
private double area;
private Set<Currency> currencies = new HashSet<>();


public Country(String name) {
this.name = name;
}

public String getName() {
return name;
}

public String getCapital() {
return capital;
}

public void setCapital(String capital) {
this.capital = capital;
}

public int getPopulation() {
return population;
}

public void setPopulation(int population) {
this.population = population;
}

public double getArea() {
return area;
}

public void setArea(double area) {
this.area = area;
}

public Set<Currency> getCurrencies() {
return currencies;
}

public void addCurrency(Currency currency) {
currencies.add(currency);
}

public double getDensity() {
if (getPopulation() != 0 && getArea() != 0) {
return (getPopulation() / getArea());
}
return 0.0;
}

@Override
public String toString() {
return "Country{" +
"name='" + name + '\'' +
", capital='" + capital + '\'' +
", population=" + population +
", area=" + area +
", currencies=" + currencies +
'}';
}

}
61 changes: 61 additions & 0 deletions src/main/java/io/codelex/assignments/restcountries/Currency.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package io.codelex.assignments.restcountries;

import java.util.Objects;

public class Currency {
private final String code;
private final String name;
private final String symbol;

public Currency(String code, String name, String symbol) {
this.code = code;
this.name = name;
this.symbol = symbol;
}

public String getCode() {
return code;
}

public String getName() {
return name;
}

public String getSymbol() {
return symbol;
}

@Override
public String toString() {
return code + " \"" + name + "\" " + symbol;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Currency currency = (Currency) o;

if (!Objects.equals(code, currency.code)) {
return false;
}
if (!Objects.equals(name, currency.name)) {
return false;
}
return Objects.equals(symbol, currency.symbol);
}

@Override
public int hashCode() {
int result = code != null ? code.hashCode() : 0;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (symbol != null ? symbol.hashCode() : 0);
return result;
}

}
24 changes: 24 additions & 0 deletions src/main/java/io/codelex/assignments/restcountries/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Task
---
<br>
<p>1. Using <a href="https://restcountries.eu">restcountries.eu</a> create a text data file in JSON format, containing only the
following data about countries in the European Union: name, capital, currencies, population,
area.</p>
<br>
<p>2. Read and process this file with your java program to get the following results:<br>
<ol type="a">
<li>top 10 countries with the biggest population</li>
<li>top 10 countries with the biggest area</li>
<li>top 10 countries with the biggest population density (people / square km)</li></ol></p><br>
<p>3a. Add the ability to consume the data about countries directly from the <a href="https://restcountries.eu">restcountries.eu</a> and
process it (keep the code that processes data from the text file as well!)<br>
3b. Convert your application to Spring Boot application and expose results via REST API</p>
4. Add unit tests<br><br>
<b>Additional info:</b><br>
1. and 2. are mandatory.<br>
3a. and 3b. are very good to have, can be done in any order<br>
4. is optional, but nice to have. do not be shy to try :)<br><br>
<b>Deliverables:</b><br>
1. source code<br>
2. description how to run your program - i.e. how do you run it yourself. it might be using IDE, or
a using command line, whatever works
104 changes: 104 additions & 0 deletions src/main/java/io/codelex/assignments/restcountries/RestCountries.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.codelex.assignments.restcountries;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class RestCountries {
public static void main(String[] args) throws IOException {
String jsonData = getRestCountriesJsonData();
List<Country> countries = parseRestCountriesJsonData(jsonData);

System.out.println("Top 10 by population:");
List<Country> top10pop = countries.stream()
.sorted(Comparator.comparing(Country::getPopulation).reversed())
.limit(10L)
.toList();
top10pop.forEach(System.out::println);

System.out.println();
System.out.println("Top 10 by area:");
List<Country> top10area = countries.stream()
.filter(country -> country.getArea() != 0)
.sorted(Comparator.comparing(Country::getArea).reversed())
.limit(10L)
.toList();
top10area.forEach(System.out::println);

System.out.println();
System.out.println("Top 10 by density");
List<Country> top10density = countries.stream()
.filter(country -> country.getDensity() != 0.0)
.sorted(Comparator.comparing(Country::getDensity).reversed())
.limit(10L)
.toList();
top10density.forEach(System.out::println);

System.out.println();
System.out.println("Listing all Countries");
countries.forEach(System.out::println);
}

private static String getLocalRestCountriesJsonData() throws IOException {
Path fileName = Path.of(Country.class.getResource("eu-countries.json").getPath());
return Files.readString(fileName);
}

private static String getRestCountriesJsonData() {
try {
URL url = new URL("https://restcountries.com/v2/regionalbloc/eu?fields=name,capital,currencies,population,area");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode != 200) {
throw new RuntimeException("HttpResponseCode: " + responseCode);
} else {
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = reader.readLine()) != null) {
builder.append(inputLine);
}
return builder.toString();
}
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}

private static List<Country> parseRestCountriesJsonData(String jsonData) {
List<Country> countries = new ArrayList<>();
JSONArray jsonArr = new JSONArray(jsonData);
for (Object countryObj : jsonArr) {
JSONObject countryJson = (JSONObject) countryObj;
Country country = new Country(countryJson.getString("name"));
country.setCapital(countryJson.getString("capital"));
country.setPopulation(countryJson.getInt("population"));
if (countryJson.has("area")) {
country.setArea(countryJson.getDouble("area"));
}
for (Object currencyObj : countryJson.getJSONArray("currencies")) {
JSONObject currencyJson = (JSONObject) currencyObj;
String code = currencyJson.getString("code");
String name = currencyJson.getString("name");
String symbol = currencyJson.getString("symbol");
Currency currency = new Currency(code, name, symbol);
country.addCurrency(currency);
}
countries.add(country);
}
return countries;
}

}
Loading