Implement movie explorer API features #1

Merged
Aryan merged 1 commits from develop into main 2026-06-12 13:52:04 +00:00
5 changed files with 71 additions and 29 deletions
+1 -1
View File
@@ -8,7 +8,7 @@
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" project-jdk-name="25" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_25" project-jdk-name="ms-25" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
+14 -14
View File
@@ -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
+5 -5
View File
@@ -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:
+31 -6
View File
@@ -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<String> genres, List<String> 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<String> getGenres() {
return genres;
}
public String getYear() {
return year;
}
public String getImdbRating() {
return imdb_rating;
}
public String toString() {
return id + ": " + title + " (" + year + ")";
}
}
+20 -3
View File
@@ -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<ArrayList<Movie>>() {}.getType();
Gson gson = new Gson();
List<Movie> 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());
}
}