From 677d68b6765fcca82f91c993ba7ad1eeac6eaca2 Mon Sep 17 00:00:00 2001 From: SaraZoveydavian Date: Sat, 2 May 2026 00:43:34 +0330 Subject: [PATCH] completing task1 & task2 --- README.md | 4 +-- src/main/java/Rational/Rational.java | 27 +++++++++++---- src/main/java/movie/apis/Main.java | 12 +++++-- src/main/java/movie/apis/Movie.java | 40 ++++++++++++++++++++-- src/main/java/movie/apis/MovieService.java | 29 +++++++++++++--- 5 files changed, 93 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index b2b0ac9..ef798b9 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ Only complete the TODO methods. 2. Write a method that receives the JSON string returned by `getMoviesByGenreList()` and converts it into a `List`. -3. Write another method that displays the list of movies (for the selected genre) in a clean and readable format. +3. Write another method that displays the list of movies (for the selected genre) in a clean and readable format.////////////////////////check! 4. Implement a console menu in `Main` that allows the user to: - View the list of genres @@ -201,5 +201,5 @@ Jackson Tutorial: --- ## Submission ⌛ -Deadline: May 1st (11 Ordibehesht) +Deadline: May 1st (11 Ordibehesht diff --git a/src/main/java/Rational/Rational.java b/src/main/java/Rational/Rational.java index 7bd840e..9f532b2 100644 --- a/src/main/java/Rational/Rational.java +++ b/src/main/java/Rational/Rational.java @@ -55,41 +55,56 @@ public class Rational { // 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 newNumerator = this.numerator * other.denominator - other.numerator * this.denominator; + int newDenominator = this.denominator * other.denominator; + return new Rational(newNumerator, newDenominator); //Done! } // 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); //Done! + } // 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 newNumerator = this.numerator * other.numerator; + int newDenominator = this.denominator * other.denominator; + return new Rational(newNumerator, newDenominator); //Done! + } // 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); + //Done! } // TODO: Instance method - this / other // Formula: (a/b) ÷ (c/d) = (a*d) / (b*c) public Rational divide(Rational other) { // YOUR CODE HERE + if (other.numerator == 0) { + throw new IllegalArgumentException("other Numerator cannot be zero"); // other Numerator????? + } + + int newNumerator = this.numerator * other.denominator; + int newDenominator = other.numerator * this.denominator; + return new Rational(newNumerator, newDenominator); // Done! // HINT: Division is multiplication by reciprocal // Don't forget to check if other.numerator is zero! - return null; // Remove this line when implemented + } // 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..d31a1cf 100644 --- a/src/main/java/movie/apis/Main.java +++ b/src/main/java/movie/apis/Main.java @@ -8,6 +8,7 @@ public class Main { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { + System.out.println("\n=== MOVIE API CLIENT ===\n"); boolean running = true; @@ -23,15 +24,20 @@ public class Main { case 2: // TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre() + Long ID = getLongInput("please enter your ID genre: "); + Long page = getLongInput("and page number you want to visit: "); + + movieService.displayMoviesByGenre(ID, page); + break; - case 3: + /*case 3: // TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails() break; - + */ case 0: running = false; System.out.println("Goodbye!"); @@ -58,7 +64,7 @@ public class Main { System.out.println("================================"); System.out.println("1. Get all genres"); System.out.println("2. Get movies by genre ID"); - System.out.println("3. Get movie details by ID"); +// System.out.println("3. Get movie details by ID"); System.out.println("0. Exit"); System.out.println("================================"); } diff --git a/src/main/java/movie/apis/Movie.java b/src/main/java/movie/apis/Movie.java index c8cc4e2..e59f89f 100644 --- a/src/main/java/movie/apis/Movie.java +++ b/src/main/java/movie/apis/Movie.java @@ -10,15 +10,49 @@ public class Movie { private Long id; private String title; - private List genres; - private String images; + private List genres; + private List images; private String year; - private String imdbRating; + 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 imdbRating, String country) { + this.id = id; + this.title = title; + this.genres = genres; + this.images = images; + this.year = year; + this.imdb_Rating = imdbRating; + 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 Long getId() {return id;} + public void setId(Long id) {this.id = id;} + + public String getTitle() {return title;} + public void setTitle(String title) {this.title = title;} + + public List getGenres() {return genres;} + public void setGenres(List genres) {this.genres = genres;} + + public List getImages() {return images;} + public void setImages(List images) {this.images = images;} + + public String getYear() {return year;} + public void setYear(String year) {this.year = year;} + + public String getImdbRating() {return imdb_Rating;} + public void setImdbRating(String imdbRating) {this.imdb_Rating = imdbRating;} + + public String getCountry() {return country;} + public void setCountry(String country) {this.country = country;} } \ 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 cbbe70b..cf69c91 100644 --- a/src/main/java/movie/apis/MovieService.java +++ b/src/main/java/movie/apis/MovieService.java @@ -103,9 +103,28 @@ public class MovieService { */ public void displayMoviesByGenre(Long genreId, Long page) { // TODO: Write your code here + String jsonResponse = getMoviesByGenreList(genreId, page); + int i = 0; // All parsing code must be inside try-catch try { // Your code here + JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject(); + JsonArray moviesArray = rootObject.getAsJsonArray("movies"); + Type movieListType = new TypeToken>(){}.getType(); + Gson gson = new Gson(); + List movies = gson.fromJson(moviesArray, movieListType); + int total_pages = rootObject.get("total_pages").getAsInt(); + + System.out.println("\nALL MOVIES:"); + for (Movie movie : movies) { + System.out.println("\"ID: {" + movie.getId() + "} | {" + movie.getTitle() + "} ({" + movie.getYear() + "})\""); + i++; // total + } + System.out.println(); + System.out.println("total: "+ i); + System.out.println("total pages of this genre: "+ total_pages); + System.out.println(); + } catch (Exception e) { System.err.println("ERROR: " + e.getMessage()); @@ -120,12 +139,12 @@ 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; + return null; } catch (Exception e) { System.out.println("!!Exception : " + e.getMessage()); } -- 2.54.0