From e0e20cf256a5b9dddf657c45981edbba6344770a Mon Sep 17 00:00:00 2001 From: Ramtin_Jafari Date: Thu, 30 Apr 2026 17:11:40 +0330 Subject: [PATCH] implement getMoviesByGenreId --- src/main/java/movie/apis/Main.java | 4 ++- src/main/java/movie/apis/MovieService.java | 39 +++++++++++----------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/src/main/java/movie/apis/Main.java b/src/main/java/movie/apis/Main.java index f0e713d..e5bf781 100644 --- a/src/main/java/movie/apis/Main.java +++ b/src/main/java/movie/apis/Main.java @@ -21,8 +21,10 @@ public class Main { break; case 2: - // TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre() + Long genreId = getLongInput("Please set your desired genre id: "); + Long page = getLongInput("Please set the number of the page: "); + movieService.displayMoviesByGenre(genreId, page); break; diff --git a/src/main/java/movie/apis/MovieService.java b/src/main/java/movie/apis/MovieService.java index 3f053f5..f9366bc 100644 --- a/src/main/java/movie/apis/MovieService.java +++ b/src/main/java/movie/apis/MovieService.java @@ -87,28 +87,27 @@ public class MovieService { } } - /** - * TODO 1: Display movies for a specific genre and page - * - * Open this link in your browser (and check Pretty-print checkbox) to view json format: - * https://api.meshcomp.ir/api/v1/genres/1/movies - * - * Steps: - * - Call getMoviesByGenreList(genreId, page) to get JSON - * - Parse JSON to JsonObject - * - Get "movies" array (NOT "data") - * - Loop through array and print: "ID: {id} | {title} ({year})" - * - Also print total and total_pages - * - * 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 jsonData = getMoviesByGenreList(genreId, page); - } catch (Exception e) { + try { + JsonObject jsonObject = JsonParser.parseString(jsonData).getAsJsonObject(); + + String moviesJson = jsonObject.get("movies").toString(); + + ObjectMapper mapper = new ObjectMapper(); + List movies = mapper.readValue( + moviesJson, + new TypeReference>() {} + ); + + for (Movie movie : movies) { + System.out.println("ID: " + movie.getId() + " | " + movie.getTitle() + " ( " + movie.getYear() + ")"); + } + + System.out.println("total: " + movies.size() + " | page: " + page + " | total pages: " + movies.size()/10); + } + catch (Exception e) { System.err.println("ERROR: " + e.getMessage()); } }