@@ -62,8 +62,8 @@ public ArrayList<City> printCitiesByContinent(String continent) {
6262 */
6363 public ArrayList <City > printCitiesByRegion (String region ) {
6464 String sql = """
65- SELECT city.Name AS CityName, country.Name AS Country,city.District AS District, city.Population AS Population
66- FROM city JOIN country ON city.CountryCode = country.Code
65+ SELECT city.Name AS CityName, country.Name AS Country,city.District AS District, city.Population AS Population
66+ FROM city JOIN country ON city.CountryCode = country.Code
6767 WHERE country.Region = ?
6868 ORDER BY city.Population DESC
6969 """ ;
@@ -78,14 +78,56 @@ public ArrayList<City> printCitiesByRegion(String region) {
7878 */
7979 public ArrayList <City > printCitiesByDistrict (String district ) {
8080 String sql = """
81- SELECT city.Name AS CityName, country.Name AS Country,city.District AS District, city.Population AS Population
81+ SELECT city.Name AS CityName, country.Name AS Country,city.District AS District, city.Population AS Population
8282 FROM city JOIN country ON city.CountryCode = country.Code
8383 WHERE city.District = ?
8484 ORDER BY city.Population DESC
8585 """ ;
8686 return executeCapitalCityQuery (sql ,district );
8787 }
8888
89+ /**
90+ * Gets most populated cities in a district
91+ * @param district the name of the district
92+ * @return a list of the most populated cities in that district
93+ */
94+
95+ public ArrayList <City > CityPopulationDistrict (String district , int limit ) {
96+ String sql = """
97+ SELECT city.Name AS CityName,
98+ country.Name AS Country,
99+ city.District AS District,
100+ city.Population AS Population
101+ FROM city
102+ JOIN country ON city.CountryCode = country.Code
103+ WHERE city.District = ?
104+ ORDER BY city.Population DESC
105+ LIMIT ?
106+ """ ;
107+
108+ return executeCapitalCityQuery (sql , district , String .valueOf (limit ));
109+ }
110+
111+ /**
112+ * Retrieves the top N most populated cities in the world.
113+ *
114+ * @param limit the number of cities to return (top N).
115+ * @return A list of the top N cities globally.
116+ */
117+ public ArrayList <City > getTopNCitiesInWorld (int limit ) {
118+ String sql = """
119+ SELECT city.Name AS CityName,
120+ country.Name AS Country,
121+ city.District AS District,
122+ city.Population AS Population
123+ FROM city
124+ JOIN country ON city.CountryCode = country.Code
125+ ORDER BY city.Population DESC
126+ LIMIT ?
127+ """ ;
128+
129+ return executeCapitalCityQuery (sql , String .valueOf (limit ));
130+ }
89131
90132 /**
91133 * Executes SQL queries and maps results to City objects.
0 commit comments