Skip to content

Commit 32a503a

Browse files
Amended the PrintCityValues.java
1 parent c4d6c8f commit 32a503a

3 files changed

Lines changed: 56 additions & 54 deletions

File tree

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM openjdk:latest
1+
FROM eclipse-temurin:17-jdk
22
COPY ./target/devops-0.1.0.3-jar-with-dependencies.jar /tmp
33
WORKDIR /tmp
44
ENTRYPOINT ["java", "-jar", "devops-0.1.0.3-jar-with-dependencies.jar"]

src/main/java/com/napier/sem/App.java

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -61,57 +61,7 @@ public void disconnect() {
6161
}
6262
}
6363

64-
/**
65-
* Gets all cities and sorts them by population in descending order
66-
* returns a list of cities sorted by population in descending order, or return null if thrown exception
67-
*/
68-
69-
public ArrayList<City> getAllCities() {
70-
try {
71-
// Create an SQL statement
72-
Statement stmt = con.createStatement();
73-
74-
// Modified SQL query to get all cities with their country and population
75-
String strSelect =
76-
"SELECT city.Name AS CityName, " +
77-
"country.Name AS Country, " +
78-
"city.Population AS Population " +
79-
"FROM city " +
80-
"JOIN country ON city.CountryCode = country.Code " +
81-
"ORDER BY city.Population DESC";
82-
83-
// Execute SQL statement
84-
ResultSet rset = stmt.executeQuery(strSelect);
85-
86-
// Extract city information
87-
ArrayList<City> cities = new ArrayList<City>();
88-
while (rset.next()) {
89-
City city = new City();
90-
city.name = rset.getString("CityName");
91-
city.country = rset.getString("Country");
92-
city.population = rset.getInt("Population");
93-
94-
cities.add(city);
95-
}
9664

97-
return cities;
98-
99-
} catch (Exception e) {
100-
System.out.println(e.getMessage());
101-
System.out.println("Failed to get the cities");
102-
return null;
103-
}
104-
105-
}
106-
public void Display(List<City> cities) {
107-
if (cities != null && !cities.isEmpty()) {
108-
for (City c : cities) {
109-
System.out.println("City: " + c.name + ", Country: " + c.country + ", Population: " + c.population);
110-
}
111-
} else {
112-
System.out.println("No capital cities found.");
113-
}
114-
}
11565
public static void main(String[] args)
11666
{
11767
// Create new Application
@@ -121,11 +71,11 @@ public static void main(String[] args)
12171
a.connect();
12272

12373
// Extract city information
124-
ArrayList<City> cityList = a.getAllCities();
125-
a.Display(cityList);
74+
PrintCityValues printCityValues = new PrintCityValues();
75+
printCityValues.getAllCities(a.con);
76+
12677

12778

128-
System.out.println(cityList.size());
12979

13080
// Disconnect from database
13181
a.disconnect();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.napier.sem;
2+
3+
import java.sql.Connection;
4+
import java.sql.ResultSet;
5+
import java.sql.Statement;
6+
import java.util.ArrayList;
7+
8+
public class PrintCityValues {
9+
10+
public void getAllCities(Connection con) {
11+
ArrayList<City> cities = new ArrayList<>();
12+
13+
try {
14+
Statement stmt = con.createStatement();
15+
16+
String strSelect =
17+
"SELECT city.name AS CityName, " +
18+
"country.name AS Country, " +
19+
"city.population AS Population " +
20+
"FROM city " +
21+
"JOIN country ON city.countryCode = country.code " +
22+
"ORDER BY city.population DESC";
23+
24+
ResultSet rset = stmt.executeQuery(strSelect);
25+
26+
while (rset.next()) {
27+
City city = new City();
28+
city.name = rset.getString("CityName");
29+
city.country = rset.getString("Country");
30+
city.population = rset.getInt("Population");
31+
cities.add(city);
32+
}
33+
34+
} catch (Exception e) {
35+
System.out.println(e.getMessage());
36+
System.out.println("Failed to get the cities");
37+
}
38+
39+
// Print table header
40+
System.out.printf("%-30s %-20s %-15s\n", "City", "Country", "Population");
41+
System.out.println("--------------------------------------------------------------");
42+
43+
// Print rows
44+
for (City city : cities) {
45+
System.out.printf("%-30s %-20s %,15d\n",
46+
city.name,
47+
city.country,
48+
city.population);
49+
}
50+
}
51+
}
52+

0 commit comments

Comments
 (0)