diff --git a/.idea/misc.xml b/.idea/misc.xml index 5274667..29fc1ba 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/src/main/java/Rational/Rational.java b/src/main/java/Rational/Rational.java index 7bd840e..922c94d 100644 --- a/src/main/java/Rational/Rational.java +++ b/src/main/java/Rational/Rational.java @@ -54,42 +54,42 @@ 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 newNumerator = this.numerator * other.denominator - other.numerator * this.denominator; + int newDenominator = this.denominator * other.denominator; + return new Rational(newNumerator, newDenominator); } // 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 newNumerator = this.numerator * other.numerator; + int newDenominator = this.denominator * other.denominator; + return new Rational(newNumerator, newDenominator); } // 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 // Formula: (a/b) รท (c/d) = (a*d) / (b*c) public Rational divide(Rational other) { - // 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("Cannot divide by zero"); + int newNumerator = this.numerator * other.denominator; + int newDenominator = this.denominator * other.numerator; + return new Rational(newNumerator, newDenominator); } // 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..af511c7 100644 --- a/src/main/java/movie/apis/Main.java +++ b/src/main/java/movie/apis/Main.java @@ -21,15 +21,15 @@ public class Main { break; 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; case 3: - // TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails() - - + Long movieId = getLongInput("Enter movie ID: "); + movieService.displayMovieDetails(movieId); break; case 0: diff --git a/src/main/java/movie/apis/Movie.java b/src/main/java/movie/apis/Movie.java index 969774a..2139111 100644 --- a/src/main/java/movie/apis/Movie.java +++ b/src/main/java/movie/apis/Movie.java @@ -15,10 +15,35 @@ public class Movie { private String year; private String imdb_rating; private String country; - - // TODO: Create a constructor with all fields - - // TODO: Create a simple constructor with id and title only - - // TODO: Create getters and setters for 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; + } + public Movie(Long id, String title) { + this.id = id; + this.title = title; + } + public Long getId() { + return id; + } + public String getTitle() { + return title; + } + public List getGenres() { + return genres; + } + public String getYear() { + return year; + } + public String getImdbRating() { + return imdb_rating; + } + public String toString() { + return id + ": " + title + " (" + 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..839c5ad 100644 --- a/src/main/java/movie/apis/MovieService.java +++ b/src/main/java/movie/apis/MovieService.java @@ -106,9 +106,26 @@ public class MovieService { // TODO: Write your code here // All parsing code must be inside try-catch try { - // Your code here - - } catch (Exception e) { + String jsonResponse = getMoviesByGenreList(genreId, page); + if (jsonResponse == null || jsonResponse.isEmpty()) { + System.out.println("No response received."); + return; + } + JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject(); + JsonArray moviesArray = rootObject.getAsJsonArray("movies"); + int total = rootObject.get("total").getAsInt(); + int totalPages = rootObject.get("total_pages").getAsInt(); + Type movieListType = new TypeToken>() {}.getType(); + Gson gson = new Gson(); + List movies = gson.fromJson(moviesArray, movieListType); + System.out.println("Movies:"); + for (Movie movie : movies) { + System.out.println("ID: " + movie.getId() + " | " + movie.getTitle() + " (" + movie.getYear() + ")"); + } + System.out.println("Total movies: " + total); + System.out.println("Total pages: " + totalPages); + } + catch (Exception e) { System.err.println("ERROR: " + e.getMessage()); } }