Implement displayMoviesByGenre method
This commit is contained in:
@@ -103,11 +103,26 @@ public class MovieService {
|
||||
* PUT ALL PARSING CODE INSIDE try-catch
|
||||
*/
|
||||
public void displayMoviesByGenre(Long genreId, Long page) {
|
||||
|
||||
// TODO: Write your code here
|
||||
// All parsing code must be inside try-catch
|
||||
try {
|
||||
// Your code here
|
||||
String jsonResponse = getMoviesByGenreList(genreId, page);
|
||||
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
||||
JsonArray jsonMovies = jsonObject.getAsJsonArray("movies");
|
||||
System.out.println("\nMovies for genre " + genreId + " (Page " + page + "):");
|
||||
for (JsonElement element: jsonMovies) {
|
||||
JsonObject movie = element.getAsJsonObject();
|
||||
long id = movie.get("id").getAsLong();
|
||||
String title = movie.get("title").getAsString();
|
||||
String year = movie.get("year").getAsString();
|
||||
|
||||
System.out.println("ID: " + id + " | " + title + " (" + year + ")");
|
||||
}
|
||||
long total = jsonObject.get("total").getAsLong();
|
||||
long totalPages = jsonObject.get("total_pages").getAsLong();
|
||||
System.out.println("Total: " + total +"\n" + "Total pages: " + totalPages);
|
||||
// Your code here
|
||||
} catch (Exception e) {
|
||||
System.err.println("ERROR: " + e.getMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user