diff --git a/src/main/java/movie/apis/Main.java b/src/main/java/movie/apis/Main.java index babf179..f0e713d 100644 --- a/src/main/java/movie/apis/Main.java +++ b/src/main/java/movie/apis/Main.java @@ -25,11 +25,8 @@ public class Main { break; - case 3: - // TODO: Get page number from user, then call movieService.displayAllMovies() - break; - case 4: + case 3: // TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails() @@ -61,8 +58,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 all movies"); - System.out.println("4. 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/MovieService.java b/src/main/java/movie/apis/MovieService.java index 7ceef15..cbbe70b 100644 --- a/src/main/java/movie/apis/MovieService.java +++ b/src/main/java/movie/apis/MovieService.java @@ -11,19 +11,13 @@ import java.net.http.HttpResponse; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.List; -import java.util.HashMap; -import java.util.Map; public class MovieService { private final static String GENRES_URL = "https://api.meshcomp.ir/api/v1/genres"; + private final static String MOVIE_BY_ID_URL = "https://api.meshcomp.ir/api/v1/movies/"; private final static HttpClient client = HttpClient.newHttpClient(); - private Map moviesCache = new HashMap<>(); - private boolean isMoviesLoaded = false; - private int totalMovies = 0; - private int totalPages = 0; - private String getGenresList() { try { HttpRequest request = HttpRequest.newBuilder() @@ -67,6 +61,7 @@ public class MovieService { } + public void displayGenres() { String jsonResponse = getGenresList(); @@ -92,7 +87,6 @@ public class MovieService { } } - /** * TODO 1: Display movies for a specific genre and page * @@ -120,85 +114,75 @@ public class MovieService { /** - * TODO 2: Load all movies into cache (called automatically when needed) - * - * Steps: - * - if (isMoviesLoaded) return; (already loaded) - * - Clear moviesCache - * - Get first page of genre 2 (Drama) to find total_pages - * - Loop through all pages (1 to total_pages) - * - For each page, get "movies" array - * - Extract: id, title, year, poster, genres array - * - For genres, loop and convert each string to Genre object - * - Store in moviesCache using id as key - * - Set isMoviesLoaded = true - * - * PUT ALL PARSING CODE INSIDE try-catch + * BONUS: Fetch movie details from API using movie ID + * URL format: https://api.meshcomp.ir/api/v1/movies/{movieId} */ - private void loadAllMovies() { - // TODO: Write your code here - // All parsing code must be inside try-catch + private String getMovieById(Long movieId) { try { - // Your code here + // 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(); + return null; } catch (Exception e) { - System.err.println("ERROR loading movies: " + e.getMessage()); + System.out.println("!!Exception : " + e.getMessage()); } + return null; } - - + /** - * TODO 3: Display all movies with pagination + * BONUS TODO: Display complete movie details for a given movie ID * - * Steps: - * - Call loadAllMovies() first - * - Calculate total pages: (totalMovies + 9) / 10 - * - Validate page number (1 to totalPages) - * - Calculate start and end index - * - Convert moviesCache.values() to List - * - Loop from start to end and display: "ID: {id} | {title} ({year})" + * Expected JSON format from getMovieById(): + * { + * "id": 1, + * "title": "The Shawshank Redemption", + * "year": "1994", + * "genres": ["Crime", "Drama"], + * "poster": "https://moviesapi.ir/images/tt0111161_poster.jpg", + * "country": "USA", + * "imdb_rating": "9.3" + * } * - * loadAllMovies() already has try-catch inside - */ - public void displayAllMovies(int page) { - // TODO: Write your code here - // loadAllMovies() already has try-catch - // Your display logic here - } - - - /** - * TODO 4 (BONUS): Display movie details by ID + * INSTRUCTIONS: + * 1. Call getMovieById(movieId) to get the JSON response + * 2. Check if response is null (if yes, print error and return) + * 3. Parse the JSON string to JsonObject (inside try-catch) + * 4. Extract all fields: id, title, year, genres array, poster, country, imdb_rating + * 5. Convert genres array to comma-separated string + * 6. Print all movie details in a readable format * - * Steps: - * - Call loadAllMovies() first - * - Get movie from moviesCache using movieId - * - If movie is null, print "Movie not found" - * - Otherwise print: ID, Title, Year, Genres, Poster + * PUT ALL PARSING CODE INSIDE THE EXISTING try-catch * - * loadAllMovies() already has try-catch inside + * @param movieId The ID of the movie to display */ public void displayMovieDetails(Long movieId) { - // TODO: Write your code here (BONUS) - // loadAllMovies() already has try-catch - // Your display logic here - } + // Step 1: Get JSON response from API + String jsonResponse = getMovieById(movieId); - private String getGenresString(List genres) { - if (genres == null || genres.isEmpty()) { - return "No genres"; + // Step 2: Check if we got valid data + if (jsonResponse == null) { + System.out.println("Failed to get movie details for ID: " + movieId); + return; } - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < genres.size(); i++) { - sb.append(genres.get(i).getName()); - if (i < genres.size() - 1) { - sb.append(", "); - } - } - return sb.toString(); - } - public int getTotalMovies() { - return totalMovies; + // Step 3-6: Parse JSON and display details (PUT YOUR CODE INSIDE try-catch) + try { + // TODO: Parse jsonResponse to JsonObject + // TODO: Extract id, title, year, genres array, poster, country, imdb_rating + // TODO: Convert genres array to a readable string + // TODO: Print all movie details + // HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject() + // HINT: Use getAsJsonArray("genres") to get genres + // HINT: For each genre, use getAsString() + + // Your code here: + + + } catch (Exception e) { + System.err.println("ERROR parsing movie details: " + e.getMessage()); + } } } \ No newline at end of file