Skip to content

Commit

Permalink
Implemented countries in region with limit paramater. Unit test passed
Browse files Browse the repository at this point in the history
  • Loading branch information
benhasselgren committed Mar 14, 2020
1 parent 23d4109 commit 9ef56bf
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/main/java/com/napier/sem/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,56 @@ public ArrayList<Country> getAllCountriesInRegion(String region) {
}
}

/**
* getAllCountriesInRegion.
* @param region The region in which the countries are chosen
* @param limit N
* @return A list of N countries in a region in descending order where N is provided by the user
*/
public ArrayList<Country> getAllCountriesInRegion(String region, int limit) {
try
{
// Create an SQL statement
Statement stmt = con.createStatement();
// Create string for SQL statement
String strSelect =
"SELECT country.Code, country.Name, country.Continent, country.Region, country.Population, country.Capital "
+ "FROM country "
+ "WHERE country.Region = '" + region + "' "
+ "ORDER BY country.population DESC "
+ "LIMIT " + limit + " ";

// Execute SQL statement
ResultSet rset = stmt.executeQuery(strSelect);

ArrayList<Country> countries = new ArrayList<Country>();

while(rset.next())
{
// Extract country information
Country country = new Country();
country.setCode(rset.getString("country.Code"));
country.setName(rset.getString("country.Name"));
country.setContinent(rset.getString("country.Continent"));
country.setRegion(rset.getString("country.Region"));
country.setPopulation(rset.getInt("country.Population"));
//Get the city by calling getCity() and passing the city id.
country.setCapitalCity(getCity(rset.getInt("country.Capital")));

//Add country to list
countries.add(country);
}
//return the countries
return countries;
}
catch (Exception e)
{
System.out.println(e.getMessage());
System.out.println("Failed to get countries.");
return null;
}
}

//------------------------------------- City queries -------------------------------------
/**
* Returns a city.
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/napier/sem/AppIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,14 @@ void testGetAllCountriesInRegion()
//Check to see that the number of rows in query is correct
assertEquals(7, countries.size());
}

@Test
void testGetAllCountriesInRegionLimit()
{
//Get list of countries in Nordic Countries region
ArrayList<Country> countries = app.getAllCountriesInRegion("Nordic Countries", 2);

//Check to see that the number of rows in query is correct
assertEquals(2, countries.size());
}
}

0 comments on commit 9ef56bf

Please sign in to comment.