Edit Bouns
This commit is contained in:
@@ -25,11 +25,8 @@ public class Main {
|
|||||||
|
|
||||||
break;
|
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()
|
// TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails()
|
||||||
|
|
||||||
|
|
||||||
@@ -61,8 +58,7 @@ public class Main {
|
|||||||
System.out.println("================================");
|
System.out.println("================================");
|
||||||
System.out.println("1. Get all genres");
|
System.out.println("1. Get all genres");
|
||||||
System.out.println("2. Get movies by genre ID");
|
System.out.println("2. Get movies by genre ID");
|
||||||
System.out.println("3. Get all movies");
|
System.out.println("3. Get movie details by ID");
|
||||||
System.out.println("4. Get movie details by ID");
|
|
||||||
System.out.println("0. Exit");
|
System.out.println("0. Exit");
|
||||||
System.out.println("================================");
|
System.out.println("================================");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,19 +11,13 @@ import java.net.http.HttpResponse;
|
|||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
public class MovieService {
|
public class MovieService {
|
||||||
|
|
||||||
private final static String GENRES_URL = "https://api.meshcomp.ir/api/v1/genres";
|
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 final static HttpClient client = HttpClient.newHttpClient();
|
||||||
|
|
||||||
private Map<Long, Movie> moviesCache = new HashMap<>();
|
|
||||||
private boolean isMoviesLoaded = false;
|
|
||||||
private int totalMovies = 0;
|
|
||||||
private int totalPages = 0;
|
|
||||||
|
|
||||||
private String getGenresList() {
|
private String getGenresList() {
|
||||||
try {
|
try {
|
||||||
HttpRequest request = HttpRequest.newBuilder()
|
HttpRequest request = HttpRequest.newBuilder()
|
||||||
@@ -67,6 +61,7 @@ public class MovieService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void displayGenres() {
|
public void displayGenres() {
|
||||||
String jsonResponse = getGenresList();
|
String jsonResponse = getGenresList();
|
||||||
|
|
||||||
@@ -92,7 +87,6 @@ public class MovieService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO 1: Display movies for a specific genre and page
|
* 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)
|
* BONUS: Fetch movie details from API using movie ID
|
||||||
*
|
* URL format: https://api.meshcomp.ir/api/v1/movies/{movieId}
|
||||||
* 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
|
|
||||||
*/
|
*/
|
||||||
private void loadAllMovies() {
|
private String getMovieById(Long movieId) {
|
||||||
// TODO: Write your code here
|
|
||||||
// All parsing code must be inside try-catch
|
|
||||||
try {
|
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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
|
// if (response.statusCode() == 200) return response.body();
|
||||||
|
|
||||||
|
return null;
|
||||||
} catch (Exception e) {
|
} 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:
|
* Expected JSON format from getMovieById():
|
||||||
* - Call loadAllMovies() first
|
* {
|
||||||
* - Calculate total pages: (totalMovies + 9) / 10
|
* "id": 1,
|
||||||
* - Validate page number (1 to totalPages)
|
* "title": "The Shawshank Redemption",
|
||||||
* - Calculate start and end index
|
* "year": "1994",
|
||||||
* - Convert moviesCache.values() to List
|
* "genres": ["Crime", "Drama"],
|
||||||
* - Loop from start to end and display: "ID: {id} | {title} ({year})"
|
* "poster": "https://moviesapi.ir/images/tt0111161_poster.jpg",
|
||||||
|
* "country": "USA",
|
||||||
|
* "imdb_rating": "9.3"
|
||||||
|
* }
|
||||||
*
|
*
|
||||||
* loadAllMovies() already has try-catch inside
|
* INSTRUCTIONS:
|
||||||
*/
|
* 1. Call getMovieById(movieId) to get the JSON response
|
||||||
public void displayAllMovies(int page) {
|
* 2. Check if response is null (if yes, print error and return)
|
||||||
// TODO: Write your code here
|
* 3. Parse the JSON string to JsonObject (inside try-catch)
|
||||||
// loadAllMovies() already has try-catch
|
* 4. Extract all fields: id, title, year, genres array, poster, country, imdb_rating
|
||||||
// Your display logic here
|
* 5. Convert genres array to comma-separated string
|
||||||
}
|
* 6. Print all movie details in a readable format
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* TODO 4 (BONUS): Display movie details by ID
|
|
||||||
*
|
*
|
||||||
* Steps:
|
* PUT ALL PARSING CODE INSIDE THE EXISTING try-catch
|
||||||
* - Call loadAllMovies() first
|
|
||||||
* - Get movie from moviesCache using movieId
|
|
||||||
* - If movie is null, print "Movie not found"
|
|
||||||
* - Otherwise print: ID, Title, Year, Genres, Poster
|
|
||||||
*
|
*
|
||||||
* loadAllMovies() already has try-catch inside
|
* @param movieId The ID of the movie to display
|
||||||
*/
|
*/
|
||||||
public void displayMovieDetails(Long movieId) {
|
public void displayMovieDetails(Long movieId) {
|
||||||
// TODO: Write your code here (BONUS)
|
// Step 1: Get JSON response from API
|
||||||
// loadAllMovies() already has try-catch
|
String jsonResponse = getMovieById(movieId);
|
||||||
// Your display logic here
|
|
||||||
|
// Step 2: Check if we got valid data
|
||||||
|
if (jsonResponse == null) {
|
||||||
|
System.out.println("Failed to get movie details for ID: " + movieId);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getGenresString(List<Genre> genres) {
|
// Step 3-6: Parse JSON and display details (PUT YOUR CODE INSIDE try-catch)
|
||||||
if (genres == null || genres.isEmpty()) {
|
try {
|
||||||
return "No genres";
|
// TODO: Parse jsonResponse to JsonObject
|
||||||
}
|
// TODO: Extract id, title, year, genres array, poster, country, imdb_rating
|
||||||
StringBuilder sb = new StringBuilder();
|
// TODO: Convert genres array to a readable string
|
||||||
for (int i = 0; i < genres.size(); i++) {
|
// TODO: Print all movie details
|
||||||
sb.append(genres.get(i).getName());
|
// HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject()
|
||||||
if (i < genres.size() - 1) {
|
// HINT: Use getAsJsonArray("genres") to get genres
|
||||||
sb.append(", ");
|
// HINT: For each genre, use getAsString()
|
||||||
}
|
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getTotalMovies() {
|
// Your code here:
|
||||||
return totalMovies;
|
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("ERROR parsing movie details: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user