diff --git a/README.md b/README.md index 6697ac3..b4293d7 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,16 @@ +# SET08103 Group 20 + [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) [![Release](https://img.shields.io/github/release/glugg23/SET08103-G20.svg)](#) -![](https://img.shields.io/github/languages/code-size/glugg23/SET08103-G20.svg?style=flat) -# SET08103 Group 20 -#### Development Branch -[![Build Status](https://travis-ci.org/glugg23/SET08103-G20.svg?branch=develop)](https://travis-ci.org/glugg23/SET08103-G20) +[![codecov](https://codecov.io/gh/glugg23/SET08103-G20/branch/master/graph/badge.svg)](https://codecov.io/gh/glugg23/SET08103-G20) +![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/glugg23/SET08103-G20.svg) + #### Master Branch [![Build Status](https://travis-ci.org/glugg23/SET08103-G20.svg?branch=master)](https://travis-ci.org/glugg23/SET08103-G20) +#### Development Branch +[![Build Status](https://travis-ci.org/glugg23/SET08103-G20.svg?branch=develop)](https://travis-ci.org/glugg23/SET08103-G20) + ## Introduction This is group 20's coursework submission for the SET08103 course at Edinburgh Napier University @@ -16,15 +20,60 @@ This is group 20's coursework submission for the SET08103 course at Edinburgh Na - Maven 4.0.0 - JDK 11.0.2 - ## Build from source ``` -mvn install -docker-compose up --build --abort-on-container-exit +mvn install -DskipTests=true ``` ## Run ``` -java -jar target/SET08103-G20-X-X-X-jar-with-dependencies.jar +java -jar target/SET08103-G20.jar {ip address} ``` -where `X-X-X` is the version number + +Where {ip address} is the ip address of the SQL server you wish to connect to. + +## Features + +| ID | Name | Met | Screenshot | +|-----|------|-----|------------| +| 1 | All the countries in the world organised by largest population to smallest. | Yes | [Link](img/1.png) | +| 2 | All the countries in a continent organised by largest population to smallest. | Yes | [Link](img/2.png) | +| 3 | All the countries in a region organised by largest population to smallest. | Yes | [Link](img/3.png) | +| 4 | The top `N` populated countries in the world where `N` is provided by the user. | Yes | [Link](img/4.png) | +| 5 | The top `N` populated countries in a continent where `N` is provided by the user. | Yes | [Link](img/5.png) | +| 6 | The top `N` populated countries in a region where `N` is provided by the user. | Yes | [Link](img/6.png) | +| 7 | All the cities in the world organised by largest population to smallest. | Yes | [Link](img/7.png) | +| 8 | All the cities in a continent organised by largest population to smallest. | Yes | [Link](img/8.png) | +| 9 | All the cities in a region organised by largest population to smallest. | Yes | [Link](img/9.png) | +| 10 | All the cities in a country organised by largest population to smallest. | Yes | [Link](img/10.png) | +| 11 | All the cities in a district organised by largest population to smallest. | Yes | [Link](img/11.png) | +| 12 | The top `N` populated cities in the world where `N` is provided by the user. | Yes | [Link](img/12.png) | +| 13 | The top `N` populated cities in a continent where `N` is provided by the user. | Yes | [Link](img/13.png) | +| 14 | The top `N` populated cities in a region where `N` is provided by the user. | Yes | [Link](img/14.png) | +| 15 | The top `N` populated cities in a country where `N` is provided by the user. | Yes | [Link](img/15.png) | +| 16 | The top `N` populated cities in a district where `N` is provided by the user. | Yes | [Link](img/16.png) | +| 17 | All the capital cities in the world organised by largest population to smallest. | Yes | [Link](img/17.png) | +| 18 | All the capital cities in a continent organised by largest population to smallest. | Yes | [Link](img/18.png) | +| 19 | All the capital cities in a region organised by largest to smallest. | Yes | [Link](img/19.png) |Contribution +| 20 | The top `N` populated capital cities in the world where `N` is provided by the user. | Yes | [Link](img/20.png) | +| 21 | The top `N` populated capital cities in a continent where `N` is provided by the user. | Yes | [Link](img/21.png) | +| 22 | The top `N` populated capital cities in a region where `N` is provided by the user. | Yes | [Link](img/22.png) | +| 23 | The population of people, people living in cities, and people not living in cities in each continent. | Yes | [Link](img/23.png) | +| 24 | The population of people, people living in cities, and people not living in cities in each region. | Yes | [Link](img/24.png) | +| 25 | The population of people, people living in cities, and people not living in cities in each country. | Yes | [Link](img/25.png) | +| 26 | The population of the world. | Yes | [Link](img/26.png) | +| 27 | The population of a continent. | Yes | [Link](img/27.png) | +| 28 | The population of a region. | Yes | [Link](img/28.png) | +| 29 | The population of a country. | Yes | [Link](img/29.png) | +| 30 | The population of a district. | Yes | [Link](img/30.png) | +| 31 | The population of a city. | Yes | [Link](img/31.png) | +| 32 | The number of people who speak Chinese, English, Hindi, Spanish or Arabic in the world. | Yes | [Link](img/32.png) | + +## Contributions + +| | #1 | #2 | #3 | #4 | Final | Avg | +|------------------|-----|-----|-----|-----|-------|-----------| +| Johann Leonhardt | 43% | 23% | 35% | 33% | 27% | **32.2%** | +| Jacob Barrow | 20% | 31% | 25% | 22% | 17% | **23.0%** | +| Toby Cook | 15% | 26% | 25% | 20% | 33% | **23.8%** | +| Raish Allan | 22% | 20% | 15% | 25% | 23% | **21.0%** | diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 0000000..4a5190b --- /dev/null +++ b/codecov.yml @@ -0,0 +1,2 @@ +ignore: + - "src/main/java/com/napier/group20/Main.java" diff --git a/img/1.png b/img/1.png new file mode 100644 index 0000000..4ceeb5a Binary files /dev/null and b/img/1.png differ diff --git a/img/10.png b/img/10.png new file mode 100644 index 0000000..69eca6c Binary files /dev/null and b/img/10.png differ diff --git a/img/11.png b/img/11.png new file mode 100644 index 0000000..6455dba Binary files /dev/null and b/img/11.png differ diff --git a/img/12.png b/img/12.png new file mode 100644 index 0000000..f63023b Binary files /dev/null and b/img/12.png differ diff --git a/img/13.png b/img/13.png new file mode 100644 index 0000000..c6db1cf Binary files /dev/null and b/img/13.png differ diff --git a/img/14.png b/img/14.png new file mode 100644 index 0000000..87c37e6 Binary files /dev/null and b/img/14.png differ diff --git a/img/15.png b/img/15.png new file mode 100644 index 0000000..8d3ed85 Binary files /dev/null and b/img/15.png differ diff --git a/img/16.png b/img/16.png new file mode 100644 index 0000000..62e0ee9 Binary files /dev/null and b/img/16.png differ diff --git a/img/17.png b/img/17.png new file mode 100644 index 0000000..80f7648 Binary files /dev/null and b/img/17.png differ diff --git a/img/18.png b/img/18.png new file mode 100644 index 0000000..98ce2a9 Binary files /dev/null and b/img/18.png differ diff --git a/img/19.png b/img/19.png new file mode 100644 index 0000000..02a6b0a Binary files /dev/null and b/img/19.png differ diff --git a/img/2.png b/img/2.png new file mode 100644 index 0000000..db25586 Binary files /dev/null and b/img/2.png differ diff --git a/img/20.png b/img/20.png new file mode 100644 index 0000000..4e39e09 Binary files /dev/null and b/img/20.png differ diff --git a/img/21.png b/img/21.png new file mode 100644 index 0000000..9e0586d Binary files /dev/null and b/img/21.png differ diff --git a/img/22.png b/img/22.png new file mode 100644 index 0000000..688b296 Binary files /dev/null and b/img/22.png differ diff --git a/img/23.png b/img/23.png new file mode 100644 index 0000000..3a9a369 Binary files /dev/null and b/img/23.png differ diff --git a/img/24.png b/img/24.png new file mode 100644 index 0000000..cc6652e Binary files /dev/null and b/img/24.png differ diff --git a/img/25.png b/img/25.png new file mode 100644 index 0000000..a8a055a Binary files /dev/null and b/img/25.png differ diff --git a/img/26.png b/img/26.png new file mode 100644 index 0000000..efb4260 Binary files /dev/null and b/img/26.png differ diff --git a/img/27.png b/img/27.png new file mode 100644 index 0000000..5411e72 Binary files /dev/null and b/img/27.png differ diff --git a/img/28.png b/img/28.png new file mode 100644 index 0000000..15cb886 Binary files /dev/null and b/img/28.png differ diff --git a/img/29.png b/img/29.png new file mode 100644 index 0000000..c5ab941 Binary files /dev/null and b/img/29.png differ diff --git a/img/3.png b/img/3.png new file mode 100644 index 0000000..5614a5f Binary files /dev/null and b/img/3.png differ diff --git a/img/30.png b/img/30.png new file mode 100644 index 0000000..0d4c5c2 Binary files /dev/null and b/img/30.png differ diff --git a/img/31.png b/img/31.png new file mode 100644 index 0000000..24f5a38 Binary files /dev/null and b/img/31.png differ diff --git a/img/32.png b/img/32.png new file mode 100644 index 0000000..fe9a3aa Binary files /dev/null and b/img/32.png differ diff --git a/img/4.png b/img/4.png new file mode 100644 index 0000000..7de5701 Binary files /dev/null and b/img/4.png differ diff --git a/img/5.png b/img/5.png new file mode 100644 index 0000000..9b613ca Binary files /dev/null and b/img/5.png differ diff --git a/img/6.png b/img/6.png new file mode 100644 index 0000000..4491299 Binary files /dev/null and b/img/6.png differ diff --git a/img/7.png b/img/7.png new file mode 100644 index 0000000..ffae34f Binary files /dev/null and b/img/7.png differ diff --git a/img/8.png b/img/8.png new file mode 100644 index 0000000..fe3ef74 Binary files /dev/null and b/img/8.png differ diff --git a/img/9.png b/img/9.png new file mode 100644 index 0000000..db4bfac Binary files /dev/null and b/img/9.png differ diff --git a/src/main/java/com/napier/group20/Main.java b/src/main/java/com/napier/group20/Main.java index d7e53de..8b7e842 100644 --- a/src/main/java/com/napier/group20/Main.java +++ b/src/main/java/com/napier/group20/Main.java @@ -4,43 +4,597 @@ import com.napier.group20.places.Country; import com.napier.group20.utils.App; import com.napier.group20.utils.LanguageReport; -import com.napier.group20.utils.PopulationReport; import java.util.ArrayList; +import java.util.InputMismatchException; +import java.util.Scanner; public class Main { public static void main(String[] args) { + if(args.length < 1) { + System.err.println("Usage: SET08103-G20 {ip address}"); + return; + } + + System.out.println("Loading database, this may take a while."); + + String connectionString = String.format("%s:3306", args[0]); App app = new App(); - app.connect("db:3306", 10); + app.connect(connectionString, 10); app.loadDatabase(); + System.out.println("Finished loading database."); + app.disconnect(); - ArrayList topNCountries = app.topPopulatedCountriesInContinent("Europe", 5); - for (Country country : topNCountries) { - System.out.println(country.toString()); - } + Scanner menuIn = new Scanner(System.in); + Scanner answerIn = new Scanner(System.in); + int input = 0; - ArrayList languagesOfWorld = app.languagesOfWorld(); - for (LanguageReport report : languagesOfWorld) { - System.out.println(report.toString()); - } + do { + System.out.print("Question ID [1-32, 0 to quit]: "); + + try { + input = menuIn.nextInt(); + } catch(InputMismatchException e) { + input = -1; + } + + switch(input) { + case 0: + break; + + case 1: { + System.out.println("All the countries in the world organised by largest population to smallest."); + ArrayList countries = app.countriesInWorld(); + + for(Country c : countries) { + System.out.println(c); + } + + break; + } + + case 2: { + System.out.println("All the countries in a continent organised by largest population to smallest."); + System.out.print("Continent: "); + String search = answerIn.nextLine(); + + ArrayList countries = app.countriesInContinent(search); + for(Country c : countries) { + System.out.println(c); + } + + break; + } + + case 3: { + System.out.println("All the countries in a region organised by largest population to smallest."); + System.out.print("Region: "); + String search = answerIn.nextLine(); + + ArrayList countries = app.countriesInRegion(search); + for(Country c : countries) { + System.out.println(c); + } + + break; + } + + case 4: { + System.out.println("The top N populated countries in the world where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + ArrayList countries = app.countriesInWorldLimit(limit); + for(Country c : countries) { + System.out.println(c); + } + + break; + } + + case 5: { + System.out.println("The top N populated countries in a continent where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("Continent: "); + String search = answerIn.nextLine(); + + ArrayList countries = app.topPopulatedCountriesInContinent(search, limit); + for(Country c : countries) { + System.out.println(c); + } + + break; + } + + case 6: { + System.out.println("The top N populated countries in a region where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("Region: "); + String search = answerIn.nextLine(); + + ArrayList countries = app.mostPopulatedCountryRegion(limit, search); + for(Country c : countries) { + System.out.println(c); + } + + break; + } + + case 7: { + System.out.println("All the cities in the world organised by largest population to smallest."); + ArrayList cities = app.citiesInWorld(); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 8: { + System.out.println("All the cities in a continent organised by largest population to smallest."); + System.out.print("Continent: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.citiesInContinent(search); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 9: { + System.out.println("All the cities in a region organised by largest population to smallest."); + System.out.print("Region: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.citiesInRegion(search); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 10: { + System.out.println("All the cities in a country organised by largest population to smallest."); + System.out.print("Country: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.citiesInCountry(search); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 11: { + System.out.println("All the cities in a district organised by largest population to smallest."); + System.out.print("District: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.citiesInDistrict(search); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 12: { + System.out.println("The top N populated cities in the world where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + ArrayList cities = app.mostPopulatedCities(limit); + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 13: { + System.out.println("The top N populated cities in a continent where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("Continent: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.topPopulatedCitiesInContinent(search, limit); + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 14: { + System.out.println("The top N populated cities in a region where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("Region: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.citiesInRegionLimit(search, limit); + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 15: { + System.out.println("The top N populated cities in a country where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("Country: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.citiesInCountryLimit(search, limit); + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 16: { + System.out.println("The top N populated cities in a district where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("District: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.citiesInDistrictLimit(search, limit); + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 17: { + System.out.println("All the capital cities in the world organised by largest population to smallest."); + ArrayList cities = app.capitalCitiesInWorld(); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 18: { + System.out.println("All the capital cities in a continent organised by largest population to smallest."); + System.out.print("Continent: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.capitalCitiesInContinent(search); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 19: { + System.out.println("All the capital cities in a region organised by largest population to smallest."); + System.out.print("Region: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.capitalCitiesInRegion(search); + + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 20: { + System.out.println("The top N populated capital cities in the world where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + ArrayList cities = app.mostPopulatedCapitals(limit); + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 21: { + System.out.println("The top N populated capital cities in a continent where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("Continent: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.mostPopulatedCapitalsContinent(limit, search); + for(City c : cities) { + System.out.println(c); + } + + break; + } + + case 22: { + System.out.println("The top N populated capital cities in a region where N is provided by the user."); + System.out.print("N: "); + + int limit; + try { + limit = answerIn.nextInt(); + } catch(InputMismatchException e) { + limit = 0; + answerIn.nextLine(); + } + + if(limit < 0) { + break; + } + + answerIn.nextLine(); //Clear buffer + + System.out.print("Region: "); + String search = answerIn.nextLine(); + + ArrayList cities = app.mostPopulatedCapitalsRegion(limit, search); + for(City c : cities) { + System.out.println(c); + } + + break; + } + case 23: { + System.out.println("The population of people, people living in cities, and people not living in cities in each continent."); + System.out.print("Continent: "); + String search = answerIn.nextLine(); + + System.out.println(app.continentPopulationReport(search)); + + break; + } + + case 24: { + System.out.println("The population of people, people living in cities, and people not living in cities in each region."); + System.out.print("Region: "); + String search = answerIn.nextLine(); + + System.out.println(app.regionPopulationReport(search)); + + break; + } + + case 25: { + System.out.println("The population of people, people living in cities, and people not living in cities in each country."); + System.out.print("Country: "); + String search = answerIn.nextLine(); + + System.out.println(app.countryPopulationReport(search)); + + break; + } + + case 26: { + System.out.println("The population of the world."); + System.out.println(app.populationOfWorld()); + break; + } + + case 27: { + System.out.println("The population of a continent."); + System.out.print("Continent: "); + + String search = answerIn.nextLine(); + + System.out.println(app.continentPopulationReport(search)); + break; + } + + case 28: { + System.out.println("The population of a region."); + System.out.print("Region: "); + + String search = answerIn.nextLine(); + + System.out.println(app.regionPopulationReport(search)); + break; + } + + case 29: { + System.out.println("The population of a country."); + System.out.print("Country: "); + + String search = answerIn.nextLine(); + + System.out.println(app.countryPopulationReport(search)); + break; + } + + case 30: { + System.out.println("The population of a district."); + System.out.print("District: "); + + String search = answerIn.nextLine(); + + System.out.println(app.populationOfDistrict(search)); + break; + } + + case 31: { + System.out.println("The population of a city."); + System.out.print("City: "); + + String search = answerIn.nextLine(); + + System.out.println(app.populationOfCity(search)); + + break; + } - long populationOfWorld = app.populationOfWorld(); - System.out.println(populationOfWorld); + case 32: { + System.out.println("The number of people who speak Chinese, English, Hindi, Spanish or Arabic in the world."); - long populationOfCity = app.populationOfCity("Edinburgh"); - System.out.println(populationOfCity); + ArrayList languageReports = app.languagesOfWorld(); + for(LanguageReport report : languageReports) { + System.out.println(report); + } - long populationOfRegion = app.populationOfRegion("Asia", "Southern and Central Asia"); - System.out.println(populationOfRegion); - long populationOfDistrict = app.populationOfDistrict("Argentina", "Buenos Aires"); - System.out.println(populationOfDistrict); + break; + } - PopulationReport continentReport = app.continentPopulationReport("Europe"); - System.out.println(continentReport.toString()); + default: + System.err.println("ERROR: Unknown question ID"); + menuIn.nextLine(); //Clear input buffer + } + } while(input != 0); } } diff --git a/src/main/java/com/napier/group20/utils/App.java b/src/main/java/com/napier/group20/utils/App.java index 9cd5e49..870cea6 100644 --- a/src/main/java/com/napier/group20/utils/App.java +++ b/src/main/java/com/napier/group20/utils/App.java @@ -407,18 +407,15 @@ public long populationOfRegion(String continentName, String regionName) { /** * Implements feature to return the population of a given district * - * @param countryName The country name of where the district is * @param districtName The district name to return the population of * * @return Population of given district if found, otherwise return -1 */ - public long populationOfDistrict(String countryName, String districtName) { + public long populationOfDistrict(String districtName) { for (Country country : countriesInWorld()) { - if (country.getName().equals(countryName)) { - for (District district : country.getDistricts()) { - if (district.getName().equals(districtName)) { - return district.getPopulation(); - } + for (District district : country.getDistricts()) { + if (district.getName().equals(districtName)) { + return district.getPopulation(); } } } @@ -548,6 +545,32 @@ public ArrayList mostPopulatedCapitals(int limit) { return new ArrayList<>(capitalCitiesInWorld().subList(0, limit)); } + /** + * All the capital cities in a region organised by largest to smallest. + * + * @param regionName The region to search for + * @return A list of capitals for a region + */ + public ArrayList capitalCitiesInRegion(String regionName) { + ArrayList cities = new ArrayList<>(); + for (Continent continent : world.getContinents()) { + for (Region region : continent.getRegions()) { + if (region.getName().equals(regionName)) { + for(Country country : region.getCountries()) { + if(country.getCapital() != null) { + cities.add(country.getCapital()); + } + } + + break; + } + } + } + + cities.sort(Comparator.comparingLong(City::getPopulation).reversed()); + return cities; + } + /** * The top N populated capital cities in a region where N is provided by the user * @@ -556,19 +579,7 @@ public ArrayList mostPopulatedCapitals(int limit) { * @return An object ArrayList with the most populated capitals in a given region */ public ArrayList mostPopulatedCapitalsRegion(int limit, String region) { - ArrayList output = new ArrayList<>(); - int counter = 0; - for (City c : citiesInRegion(region)) { - if (c.isCapital()) { - output.add(c); - counter++; - } - if (counter >= limit) { - break; - } - } - - return output; + return new ArrayList<>(capitalCitiesInRegion(region).subList(0, limit)); } /** @@ -583,14 +594,40 @@ public ArrayList mostPopulatedCountryRegion(int limit, String region) { } /** - * The top N populated countries in a region where N is provided by the user + * All the capital cities in a continent organised by largest population to smallest. * - * @param limit Number of cities to display + * @param continentName The continent to get the capitals from + * @return A list of capitals for a continent + */ + public ArrayList capitalCitiesInContinent(String continentName) { + ArrayList cities = new ArrayList<>(); + for (Continent continent : world.getContinents()) { + if(continent.getName().equals(continentName)) { + for(Region region : continent.getRegions()) { + for(Country country : region.getCountries()) { + if(country.getCapital() != null) { + cities.add(country.getCapital()); + } + } + } + + break; + } + } + + cities.sort(Comparator.comparingLong(City::getPopulation).reversed()); + return cities; + } + + /** + * The top N populated capitals in a continent where N is provided by the user + * + * @param limit Number of capitals to display * @param continent What continent the city resides in - * @return An object ArrayList with the most populated cities in a given continent + * @return An object ArrayList with the most populated capitals in a given continent */ - public ArrayList mostPopulatedCityContinent(int limit, String continent) { - return new ArrayList<>(citiesInContinent(continent).subList(0, limit)); + public ArrayList mostPopulatedCapitalsContinent(int limit, String continent) { + return new ArrayList<>(capitalCitiesInContinent(continent).subList(0, limit)); } /** diff --git a/src/test/java/com/napier/group20/utils/AppTest.java b/src/test/java/com/napier/group20/utils/AppTest.java index 2d36124..03b4006 100644 --- a/src/test/java/com/napier/group20/utils/AppTest.java +++ b/src/test/java/com/napier/group20/utils/AppTest.java @@ -250,7 +250,7 @@ void populationOfDistrict() { long expected = 10530136; //Act - long actual = app.populationOfDistrict("Argentina", "Buenos Aires"); + long actual = app.populationOfDistrict("Buenos Aires"); //Assert assertEquals(expected, actual); @@ -262,7 +262,7 @@ void populationOfDistrictNotFound() { long expected = -1; //Act - long actual = app.populationOfDistrict("Does not exist", "Does not exist"); + long actual = app.populationOfDistrict("Does not exist"); //Assert assertEquals(expected, actual); @@ -408,13 +408,35 @@ void mostPopulatedCountryRegion() { } @Test - void mostPopulatedCityContinent() { + void mostPopulatedCapitalsContinent() { //Call - ArrayList actual = app.mostPopulatedCityContinent(10, "Europe"); + ArrayList actual = app.mostPopulatedCapitalsContinent(10, "Europe"); String sha1 = listToSHA1(actual); //Assert assertEquals(10, actual.size()); - assertEquals("CPLXM4e8UT7xWR53Cylo9ZLbGHo=", sha1); + assertEquals("oleozjqTk1Hv4E1wJJDwZ7Hrl3c=", sha1); + } + + @Test + void capitalCitiesInRegion() { + //Act + ArrayList actual = app.capitalCitiesInRegion("Nordic Countries"); + + //Assert + String sha1 = listToSHA1(actual); + assertEquals(7, actual.size()); + assertEquals("UR11HuUf6k9zpAsQmx1hjOPzlHQ=", sha1); + } + + @Test + void capitalCitiesInContinent() { + //Act + ArrayList actual = app.capitalCitiesInContinent("South America"); + + //Assert + String sha1 = listToSHA1(actual); + assertEquals(14, actual.size()); + assertEquals("qetEAS40evowOvjQvTfd4uWr4bY=", sha1); } }