Comple not bonus methods

This commit is contained in:
2026-05-03 23:17:53 +03:30
parent 737a521018
commit 6ad5db5311
2 changed files with 25 additions and 22 deletions
+3 -1
View File
@@ -22,7 +22,9 @@ public class Main {
case 2:
// TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre()
Long genreId = getLongInput("Enter genre ID: ");
Long page = getLongInput("Enter page number: ");
movieService.displayMoviesByGenre(genreId, page);
break;
+22 -21
View File
@@ -87,26 +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 jsonAsString = getMoviesByGenreList(genreId, page);
JsonObject jsonObj = JsonParser.parseString(jsonAsString).getAsJsonObject();
JsonArray moviesArray = jsonObj.getAsJsonArray("movies");
List<Movie> movies = new ArrayList<>();
for(JsonElement movie : moviesArray){
JsonObject movieAsObj = movie.getAsJsonObject();
System.out.printf("ID: %d | %s (%d)\n\n",
movieAsObj.get("id").getAsLong(), movieAsObj.get("title").getAsString(), movieAsObj.get("year").getAsLong());
movies.add(new Movie(movieAsObj.get("id").getAsLong(),
movieAsObj.get("title").getAsString()));
}
System.out.printf("Total: %d, total_pages: %d\n\n",
jsonObj.get("total").getAsLong(), jsonObj.get("total_pages").getAsLong());
} catch (Exception e) {
System.err.println("ERROR: " + e.getMessage());
@@ -121,10 +122,10 @@ public class MovieService {
private String getMovieById(Long movieId) {
try {
// TODO: Complete this method
// String url = MOVIE_BY_ID_URL + movieId;
// HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
// HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// if (response.statusCode() == 200) return response.body();
String url = MOVIE_BY_ID_URL + movieId;
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) return response.body();
return null;
} catch (Exception e) {