implement getMoviesByGenreId

This commit is contained in:
2026-04-30 17:11:40 +03:30
parent 190fedbf3a
commit e0e20cf256
2 changed files with 22 additions and 21 deletions
+3 -1
View File
@@ -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;
+19 -20
View File
@@ -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<Movie> movies = mapper.readValue(
moviesJson,
new TypeReference<List<Movie>>() {}
);
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());
}
}