implement final changes for displaying movie by ID inside movie service and main

This commit is contained in:
2026-04-30 15:28:33 +03:30
parent 68c1d8a5a0
commit 22b6e5fcdb
2 changed files with 24 additions and 4 deletions
+3
View File
@@ -34,6 +34,9 @@ public class Main {
case 3:
System.out.println("Please enter movie ID: ");
long movieID = scanner.nextLong();
movieService.displayMovieDetails(movieID);
// TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails()
+21 -4
View File
@@ -136,10 +136,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) {
@@ -196,6 +196,23 @@ public class MovieService {
return;
}
try {
JsonObject movieObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
Gson gson = new Gson();
Movie movie = gson.fromJson(jsonResponse, Movie.class);
System.out.println("________________________ " + movie.getTitle().toUpperCase()+ " (" + movie.getYear() +
") ________________________");
System.out.println("IMDB rating: " + movie.getImdb_rating() +
" | Votes: " + movie.getImdb_votes());
System.out.println("Director: " + movie.getDirector());
System.out.println("Cast: " + movie.getActors());
System.out.println("Genre: " + movie.getGenres());
System.out.println("Runtime: " + movie.getRuntime() +
" | Rated: " + movie.getRated());
System.out.println("Country: " + movie.getCountry() +
" | Released: " + movie.getReleased());
System.out.println("Plot: " + movie.getPlot());
System.out.println("Awards: " + movie.getAwards());
// TODO: Parse jsonResponse to JsonObject
// HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject()
// TODO: use Gson to parse the json