Skip to content

Commit

Permalink
Merge pull request #48 from glugg23/develop
Browse files Browse the repository at this point in the history
User stories complete, request to merge into master.
  • Loading branch information
wrestlerdude authored May 1, 2019
2 parents 653fd85 + 808853c commit 95fc6d4
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker_rm.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/bin/bash
# Delete all containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
# Delete all images
docker rmi $(docker images -q)
1 change: 1 addition & 0 deletions src/main/java/com/napier/group20/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ public static void main(String[] args) {

PopulationReport continentReport = app.continentPopulationReport("Europe");
System.out.println(continentReport.toString());

}
}
100 changes: 100 additions & 0 deletions src/main/java/com/napier/group20/utils/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,26 @@ public ArrayList<Country> countriesInContinent(String continentName) {
return countries;
}

/**
* Countries in a region organised by largest population to smallest
*
* @param regionName The name of the region to get populated countries from
* @return List of countries sorted by most populated
*/
public ArrayList<Country> countriesInRegion(String regionName) {
ArrayList<Country> countries = new ArrayList<>();
for (Continent continent : world.getContinents()) {
for (Region region : continent.getRegions()) {
if (region.getName().equals(regionName)) {
countries.addAll(region.getCountries());
break;
}
}
}
countries.sort(Comparator.comparingLong(Country::getPopulation).reversed());
return countries;
}

/**
* Top N populated countries in a continent where N is provided by user
*
Expand Down Expand Up @@ -485,6 +505,21 @@ public PopulationReport regionPopulationReport(String regionName) {
return null;
}

/**
* Create report for number of people living in cities and non-cities in a given country
*
* @param countryName The name of the country for the population report
* @return An object representing the city and non-city population for that country
*/
public PopulationReport countryPopulationReport(String countryName) {
for (Country country : this.countriesInWorld()) {
if (country.getName().equals(countryName)) {
return new PopulationReport(countryName, country.getCitiesPopulation(), country.getPopulation());
}
}
return null;
}

/**
* Loads the contents of the database into the world member variable
*/
Expand All @@ -493,6 +528,71 @@ public void loadDatabase() {
world.setContinents(loadContinents());
}

/**
* The top N populated cities in the world where N is provided by the user
*
* @param limit Number of cities to display
* @return An object ArrayList with the most populated cities
*/
public ArrayList<City> mostPopulatedCities(int limit) {
return new ArrayList<>(citiesInWorld().subList(0, limit));
}

/**
* The top N populated capital cities in the world where N is provided by the user
*
* @param limit Number of capitals to display
* @return An object ArrayList with the most populated capitals
*/
public ArrayList<City> mostPopulatedCapitals(int limit) {
return new ArrayList<>(capitalCitiesInWorld().subList(0, limit));
}

/**
* The top N populated capital cities in a region where N is provided by the user
*
* @param limit Number of capitals to display
* @param region What region the capital resides in
* @return An object ArrayList with the most populated capitals in a given region
*/
public ArrayList<City> mostPopulatedCapitalsRegion(int limit, String region) {
ArrayList<City> output = new ArrayList<>();
int counter = 0;
for (City c : citiesInRegion(region)) {
if (c.isCapital()) {
output.add(c);
counter++;
}
if (counter >= limit) {
break;
}
}

return output;
}

/**
* The top N populated countries in a region where N is provided by the user
*
* @param limit Number of countries to display
* @param region What region the country resides in
* @return An object ArrayList with the most populated countries in a given region
*/
public ArrayList<Country> mostPopulatedCountryRegion(int limit, String region) {
return new ArrayList<>((countriesInRegion(region).subList(0, limit)));
}

/**
* The top N populated countries in a region where N is provided by the user
*
* @param limit Number of cities to display
* @param continent What continent the city resides in
* @return An object ArrayList with the most populated cities in a given continent
*/
public ArrayList<City> mostPopulatedCityContinent(int limit, String continent) {
return new ArrayList<>(citiesInContinent(continent).subList(0, limit));
}

/**
* Queries the database for all the continents and creates the
* relevant objects
Expand Down
92 changes: 92 additions & 0 deletions src/test/java/com/napier/group20/utils/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ void citiesInDistrictLimit() {
assertEquals("0O6su4YbZ6DCs81cWaTeqDywRLY=", sha1);
}

@Test
void countriesInRegion() {
ArrayList<Country> actual = app.countriesInRegion("Southern and Central Asia");

String sha1 = listToSHA1(actual);
assertEquals("Gs2T7HdU10YC4+d0RZVlNP5iw4w=", sha1);
}

@Test
void populationOfWorld() {
//Arrange
Expand Down Expand Up @@ -314,6 +322,26 @@ void topNPopulatedCountriesInContinent() {

}

@Test
void topPopulatedCitiesInContinent() {
String expected = "City{name='Moscow', population=8389200, country='Russian Federation', district='Moscow (City)'}";

ArrayList<City> citiesInContinent = app.citiesInContinent("Europe");

assertEquals(expected, citiesInContinent.get(0).toString());
}

@Test
void topNPopulatedCitiesInContinent() {
String expectedFirstItem = "City{name='Moscow', population=8389200, country='Russian Federation', district='Moscow (City)'}";
final int expectedNumberOfElements = 5;

ArrayList<City> topCitiesInContinent = app.topPopulatedCitiesInContinent("Europe", 5);

assertEquals(expectedFirstItem, topCitiesInContinent.get(0).toString());
assertEquals(expectedNumberOfElements, topCitiesInContinent.size());
}

@Test
void regionPopulationReport() {
//Arrange
Expand All @@ -325,4 +353,68 @@ void regionPopulationReport() {
//Assert
assertEquals(expected, actual.toString());
}

@Test
void countryPopulationReport() {
String expected = "PopulationReport{name='Spain', totalPopulation=39441700, cityPopulation=16669189 (42.26%), nonCityPopulation=22772511 (57.74%)}";

PopulationReport actual = app.countryPopulationReport("Spain");

assertEquals(expected, actual.toString());
}

@Test
void mostPopulatedCities() {
//Call
ArrayList<City> actual = app.mostPopulatedCities(10);

String sha1 = listToSHA1(actual);
//Assert
assertEquals(10, actual.size());
assertEquals("kTPvpCEigC+aDe5n0bD6DdTf32Q=", sha1);
}

@Test
void mostPopulatedCapitals() {
//Call
ArrayList<City> actual = app.mostPopulatedCapitals(25);

String sha1 = listToSHA1(actual);
//Assert
assertEquals(25, actual.size());
assertEquals("juClLL3/xGT7F8JvmJe1zTxk5Og=", sha1);
}

@Test
void mostPopulatedCapitalsRegion() {
//Call
ArrayList<City> actual = app.mostPopulatedCapitalsRegion(10, "Middle East");

String sha1 = listToSHA1(actual);
//Assert
assertEquals(10, actual.size());
assertEquals("zbyha9k1J0HI9e3l2V9SC0SGjSQ=", sha1);
}

@Test
void mostPopulatedCountryRegion() {
//Call
ArrayList<Country> actual = app.mostPopulatedCountryRegion(10, "Middle East");

String sha1 = listToSHA1(actual);
//Assert
assertEquals(10, actual.size());
assertEquals("iFzU5BzqX1d/TXyAe+i1jTTWsac=", sha1);
}

@Test
void mostPopulatedCityContinent() {
//Call
ArrayList<City> actual = app.mostPopulatedCityContinent(10, "Europe");

String sha1 = listToSHA1(actual);
//Assert
assertEquals(10, actual.size());
assertEquals("CPLXM4e8UT7xWR53Cylo9ZLbGHo=", sha1);
}
}

0 comments on commit 95fc6d4

Please sign in to comment.