From d25e0a1779d38ade66512f62feeeca6ce4a74dbe Mon Sep 17 00:00:00 2001 From: emad Date: Fri, 1 May 2026 23:08:13 +0430 Subject: [PATCH 1/2] implement all TODOs exept bonus --- src/main/java/Rational/Rational.java | 28 ++++---- src/main/java/movie/apis/Main.java | 4 +- src/main/java/movie/apis/Movie.java | 75 +++++++++++++++++++++- src/main/java/movie/apis/MovieService.java | 20 +++++- 4 files changed, 113 insertions(+), 14 deletions(-) diff --git a/src/main/java/Rational/Rational.java b/src/main/java/Rational/Rational.java index 7bd840e..8d33a8f 100644 --- a/src/main/java/Rational/Rational.java +++ b/src/main/java/Rational/Rational.java @@ -54,27 +54,27 @@ public class Rational { // TODO: Instance method - this - other // Formula: a/b - c/d = (a*d - c*b) / (b*d) public Rational subtract(Rational other) { - // YOUR CODE HERE - return null; // Remove this line when implemented + int num = this.numerator * other.denominator - this.denominator * other.numerator ; + int den = this.denominator * other.denominator; + return new Rational(num, den); } // TODO: Static method - r1 - r2 public static Rational subtract(Rational r1, Rational r2) { - // YOUR CODE HERE - return null; // Remove this line when implemented + return r1.subtract(r2); } // TODO: Instance method - this * other // Formula: (a/b) * (c/d) = (a*c) / (b*d) public Rational multiply(Rational other) { - // YOUR CODE HERE - return null; // Remove this line when implemented + int num = this.numerator * other.numerator; + int den = this.denominator * other.denominator; + return new Rational(num, den); } // TODO: Static method - r1 * r2 public static Rational multiply(Rational r1, Rational r2) { - // YOUR CODE HERE - return null; // Remove this line when implemented + return r1.multiply(r2); } // TODO: Instance method - this / other @@ -83,13 +83,19 @@ public class Rational { // YOUR CODE HERE // HINT: Division is multiplication by reciprocal // Don't forget to check if other.numerator is zero! - return null; // Remove this line when implemented + if (other.numerator == 0) + { + throw new ArithmeticException("The divisor must not be zero"); + } + int num = this.numerator * other.denominator; + int den = this.denominator * other.numerator; + return new Rational(num, den); + } // TODO: Static method - r1 / r2 public static Rational divide(Rational r1, Rational r2) { - // YOUR CODE HERE - return null; // Remove this line when implemented + return r1.divide(r2); } // PROVIDED - String representation diff --git a/src/main/java/movie/apis/Main.java b/src/main/java/movie/apis/Main.java index f0e713d..c412ca7 100644 --- a/src/main/java/movie/apis/Main.java +++ b/src/main/java/movie/apis/Main.java @@ -22,7 +22,9 @@ public class Main { case 2: // TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre() - + long generId = getLongInput("Enter the ID: "); + long page = getLongInput("Enter the page: "); + movieService.displayMoviesByGenre(generId, page); break; diff --git a/src/main/java/movie/apis/Movie.java b/src/main/java/movie/apis/Movie.java index 969774a..8770bf2 100644 --- a/src/main/java/movie/apis/Movie.java +++ b/src/main/java/movie/apis/Movie.java @@ -16,9 +16,82 @@ public class Movie { private String imdb_rating; private String country; + // TODO: Create a constructor with all fields + public Movie(Long id, String title, List genres, List images, String year, String imdb_rating, String country) { + this.id = id; + this.title = title; + this.genres = genres; + this.images = images; + this.year = year; + this.imdb_rating = imdb_rating; + this.country = country; + } // TODO: Create a simple constructor with id and title only - + public Movie(long id, String title) + { + this.id = id; + this.title = title; + } // TODO: Create getters and setters for all fields + + public List getGenres() { + return genres; + } + + public void setGenres(List genres) { + this.genres = genres; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getImdb_rating() { + return imdb_rating; + } + + public void setImdb_rating(String imdb_rating) { + this.imdb_rating = imdb_rating; + } + + public List getImages() { + return images; + } + + public void setImages(List images) { + this.images = images; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public String getYear() { + return year; + } + + public void setYear(String year) { + this.year = year; + } + + + } \ No newline at end of file diff --git a/src/main/java/movie/apis/MovieService.java b/src/main/java/movie/apis/MovieService.java index 3f053f5..d95e55e 100644 --- a/src/main/java/movie/apis/MovieService.java +++ b/src/main/java/movie/apis/MovieService.java @@ -106,7 +106,23 @@ public class MovieService { // TODO: Write your code here // All parsing code must be inside try-catch try { - // Your code here + String json = getMoviesByGenreList(genreId, page); + JsonElement j = JsonParser.parseString(json); + JsonObject obj = j.getAsJsonObject(); + JsonArray movies = obj.getAsJsonArray("movies"); + for(int i = 0; i < movies.size(); i ++) + { + JsonObject movie = movies.get(i).getAsJsonObject(); + 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); + } + System.out.println(); + int total = obj.get("total").getAsInt(); + int totalPages = obj.get("total_pages").getAsInt(); + System.out.printf("Total: %d\n", total); + System.out.printf("Total pages: %d", totalPages); } catch (Exception e) { System.err.println("ERROR: " + e.getMessage()); @@ -189,4 +205,6 @@ public class MovieService { System.err.println("ERROR parsing movie details: " + e.getMessage()); } } + + } \ No newline at end of file -- 2.54.0 From cd88bc70969b104ae6e25e3f4f59ce8c820d60f8 Mon Sep 17 00:00:00 2001 From: emad Date: Sun, 3 May 2026 00:42:39 +0430 Subject: [PATCH 2/2] implement all TODO tasks --- src/main/java/movie/apis/Main.java | 7 +- src/main/java/movie/apis/Movie.java | 129 ++++++++++++++++++++- src/main/java/movie/apis/MovieService.java | 46 +++++++- 3 files changed, 173 insertions(+), 9 deletions(-) diff --git a/src/main/java/movie/apis/Main.java b/src/main/java/movie/apis/Main.java index c412ca7..25490dd 100644 --- a/src/main/java/movie/apis/Main.java +++ b/src/main/java/movie/apis/Main.java @@ -22,15 +22,16 @@ public class Main { case 2: // TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre() - long generId = getLongInput("Enter the ID: "); + long genreId = getLongInput("Enter the genre ID: "); long page = getLongInput("Enter the page: "); - movieService.displayMoviesByGenre(generId, page); + movieService.displayMoviesByGenre(genreId, page); break; case 3: // TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails() - + long movieId = getLongInput("Enter the movie ID: "); + movieService.displayMovieDetails(movieId); break; diff --git a/src/main/java/movie/apis/Movie.java b/src/main/java/movie/apis/Movie.java index 8770bf2..e12ae6b 100644 --- a/src/main/java/movie/apis/Movie.java +++ b/src/main/java/movie/apis/Movie.java @@ -15,10 +15,30 @@ public class Movie { private String year; private String imdb_rating; private String country; + private String actors; + private String awards; + private String director; + private String imdb_id; + private String plot; + private String released; + private String runtime; + private String type; + private String writer; + + private String rated; + + private String imdb_votes; + private String metascore; + + + // TODO: Create a constructor with all fields - public Movie(Long id, String title, List genres, List images, String year, String imdb_rating, String country) { + public Movie(Long id, String title, List genres, List images, String year, + String imdb_rating, String country, String actors, String awards, + String director, String imdb_id, String plot, String released, + String runtime, String type, String writer, String rated, String imdb_votes, String metascore) { this.id = id; this.title = title; this.genres = genres; @@ -26,6 +46,18 @@ public class Movie { this.year = year; this.imdb_rating = imdb_rating; this.country = country; + this.actors = actors; + this.awards = awards; + this.director = director; + this.imdb_id = imdb_id; + this.plot = plot; + this.released = released; + this.runtime = runtime; + this.type = type; + this.writer = writer; + this.rated = rated; + this.imdb_votes = imdb_votes; + this.metascore = metascore; } // TODO: Create a simple constructor with id and title only @@ -34,6 +66,8 @@ public class Movie { this.id = id; this.title = title; } + + // TODO: Create getters and setters for all fields public List getGenres() { @@ -92,6 +126,99 @@ public class Movie { this.year = year; } + public String getActors() { + return actors; + } + public void setActors(String actors) { + this.actors = actors; + } + public String getAwards() { + return awards; + } + + public void setAwards(String awards) { + this.awards = awards; + } + + public String getDirector() { + return director; + } + + public void setDirector(String director) { + this.director = director; + } + + public String getImdb_id() { + return imdb_id; + } + + public void setImdb_id(String imdb_id) { + this.imdb_id = imdb_id; + } + + public String getImdb_votes() { + return imdb_votes; + } + + public void setImdb_votes(String imdb_votes) { + this.imdb_votes = imdb_votes; + } + + public String getMetascore() { + return metascore; + } + + public void setMetascore(String metascore) { + this.metascore = metascore; + } + + public String getPlot() { + return plot; + } + + public void setPlot(String plot) { + this.plot = plot; + } + + public String getRated() { + return rated; + } + + public void setRated(String rated) { + this.rated = rated; + } + + public String getReleased() { + return released; + } + + public void setReleased(String released) { + this.released = released; + } + + public String getRuntime() { + return runtime; + } + + public void setRuntime(String runtime) { + this.runtime = runtime; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getWriter() { + return writer; + } + + public void setWriter(String writer) { + this.writer = writer; + } } \ No newline at end of file diff --git a/src/main/java/movie/apis/MovieService.java b/src/main/java/movie/apis/MovieService.java index d95e55e..2df86e3 100644 --- a/src/main/java/movie/apis/MovieService.java +++ b/src/main/java/movie/apis/MovieService.java @@ -10,6 +10,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.lang.reflect.Type; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class MovieService { @@ -137,10 +138,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 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 response = client.send(request, HttpResponse.BodyHandlers.ofString()); + if (response.statusCode() == 200) return response.body(); return null; } catch (Exception e) { @@ -198,13 +199,48 @@ public class MovieService { } try { // TODO: Parse jsonResponse to JsonObject - // HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject() + //HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject() + JsonObject obj = JsonParser.parseString(jsonResponse).getAsJsonObject(); // TODO: use Gson to parse the json // Your code here: + + Gson gson = new Gson(); + Movie movie = gson.fromJson(obj, Movie.class); + StringBuilder genresString = new StringBuilder(); + for (int i = 0; i < movie.getGenres().size(); i++) { + if(i == movie.getGenres().size() - 1) + { + genresString.append(movie.getGenres().get(i)); + break; + } + genresString.append(movie.getGenres().get(i)).append(", "); + + } + System.out.println("======================================================================="); + int mid = (71 - movie.getTitle().length())/ 2; + System.out.print(" ".repeat(mid) + movie.getTitle()+"\n"); + System.out.println("======================================================================="); + System.out.printf(""" + Rating: %s | Votes: %s + Director: %s + Cast: %s + Genre: %s + Runtime: %s | Rated: %s + Country: %s | Released: %s + Plot: %s + Awards: %s + """, + movie.getImdb_rating(), movie.getImdb_votes(), movie.getDirector(), movie.getActors(), + genresString.toString(),movie.getRuntime(), movie.getRated(), movie.getCountry(), movie.getReleased(), + movie.getPlot(), movie.getAwards()); + System.out.println("======================================================================="); + + } catch (Exception e) { System.err.println("ERROR parsing movie details: " + e.getMessage()); } } + } \ No newline at end of file -- 2.54.0