Updated Movie class for bonus, completed MovieService class and Main class + Bonus.

This commit is contained in:
2026-04-30 20:21:01 +03:30
parent c56b99c494
commit d885803a90
3 changed files with 196 additions and 14 deletions
+63 -13
View File
@@ -103,10 +103,36 @@ 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
String jsonResponse = null;
try {
// Your code here
//Getting the JSON
jsonResponse = getMoviesByGenreList(genreId, page);
//Parsing the JSON
JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
JsonArray moviesArray = rootObject.getAsJsonArray("movies");
//Getting the number of movies
int total = rootObject.get("total").getAsInt();
//Getting the number of pages
int totalPages = rootObject.get("total_pages").getAsInt();
System.out.println("Total movies: " + total);
System.out.println("Total pages: " + totalPages);
for (JsonElement element : moviesArray) {
//Getting each element as a movie to read features
JsonObject movie = element.getAsJsonObject();
//Setting features
long id = movie.get("id").getAsLong();
String title = movie.get("title").getAsString();
int year = movie.get("year").getAsInt();
System.out.printf("ID: %d | %s (%d)%n", id, title, year);
}
} catch (Exception e) {
System.err.println("ERROR: " + e.getMessage());
@@ -120,13 +146,19 @@ 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();
//Building the URL
String url = MOVIE_BY_ID_URL + movieId;
//Building the request
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
//Sending request and getting response
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//Checking the response
if (response.statusCode() == 200) {return response.body();}
return null; //else
return null;
} catch (Exception e) {
System.out.println("!!Exception : " + e.getMessage());
}
@@ -180,11 +212,29 @@ public class MovieService {
System.out.println("Failed to get movie details for ID: " + movieId);
return;
}
try {
// TODO: Parse jsonResponse to JsonObject
// HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject()
// TODO: use Gson to parse the json
// Your code here:
//TODO: Parse jsonResponse to JsonObject
JsonObject jsonObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
//TODO: use Gson to parse the json
Gson gson = new Gson();
Movie movie = gson.fromJson(jsonResponse, Movie.class);
//Printing data:
System.out.println("\n===== Movie Details =====");
System.out.println("Title: " + movie.getTitle() + "(" + movie.getYear() + ")");
System.out.println("=========================");
System.out.println("Rating: " + movie.getImdb_rating() + " | Votes: " + movie.getImdb_votes());
System.out.println("Director: " + movie.getDirector() + " | Writer: " + movie.getWriter());
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());
System.out.println("=========================");
} catch (Exception e) {
System.err.println("ERROR parsing movie details: " + e.getMessage());
}