fix: added implementations
This commit is contained in:
@@ -4,6 +4,7 @@ import com.google.gson.*;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
@@ -12,65 +13,74 @@ import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class MovieService {
|
||||
|
||||
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 String getGenresList() {
|
||||
try {
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(GENRES_URL))
|
||||
.build();
|
||||
private String getGenresList()
|
||||
{
|
||||
try
|
||||
{
|
||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(GENRES_URL)).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (response.statusCode() == 200) {
|
||||
if (response.statusCode() == 200)
|
||||
{
|
||||
System.out.println("Genres List received successfully");
|
||||
return response.body();
|
||||
} else {
|
||||
throw new IOException("HTTP error code: " + response.statusCode());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
else
|
||||
throw new IOException("HTTP error code: " + response.statusCode());
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("!!Exception : " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getMoviesByGenreList(Long genreId, Long page) {
|
||||
try {
|
||||
private String getMoviesByGenreList(Long genreId, Long page)
|
||||
{
|
||||
try
|
||||
{
|
||||
String url = "https://api.meshcomp.ir/api/v1/genres/" + genreId + "/movies?page=" + page;
|
||||
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create(url))
|
||||
.build();
|
||||
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(url)).build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (response.statusCode() == 200) {
|
||||
if (response.statusCode() == 200)
|
||||
return response.body();
|
||||
} else {
|
||||
else
|
||||
{
|
||||
System.out.println("HTTP error code: " + response.statusCode());
|
||||
return null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("!!Exception : " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void displayGenres() {
|
||||
public void displayGenres()
|
||||
{
|
||||
String jsonResponse = getGenresList();
|
||||
|
||||
if (jsonResponse == null) {
|
||||
if (jsonResponse == null)
|
||||
{
|
||||
System.out.println("Failed to get genres");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
try
|
||||
{
|
||||
JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
||||
JsonArray genresArray = rootObject.getAsJsonArray("genres");
|
||||
Type genreListType = new TypeToken<ArrayList<Genre>>(){}.getType();
|
||||
@@ -78,17 +88,19 @@ public class MovieService {
|
||||
List<Genre> genres = gson.fromJson(genresArray, genreListType);
|
||||
|
||||
System.out.println("\nALL GENRES:");
|
||||
for (Genre genre : genres) {
|
||||
for (Genre genre : genres)
|
||||
System.out.println(genre.getId() + ". " + genre.getName());
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("ERROR parsing genres: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 1: Display movies for a specific genre and page
|
||||
* Display movies for a specific genre and page
|
||||
*
|
||||
* JSON format: { "movies": [ { "id": 1, "title": "Movie", "year": "1994" } ], "total": 177, "total_pages": 18 }
|
||||
*
|
||||
@@ -101,13 +113,30 @@ public class MovieService {
|
||||
*
|
||||
* PUT ALL PARSING CODE INSIDE try-catch
|
||||
*/
|
||||
public void displayMoviesByGenre(Long genreId, Long page) {
|
||||
// TODO: Write your code here
|
||||
// All parsing code must be inside try-catch
|
||||
try {
|
||||
// Your code here
|
||||
public void displayMoviesByGenre(Long genreId, Long page)
|
||||
{
|
||||
try
|
||||
{
|
||||
String RawData = getMoviesByGenreList(genreId, page);
|
||||
if (RawData == null)
|
||||
throw new IllegalArgumentException("Invalid genreId or page number; no data found");
|
||||
|
||||
} catch (Exception e) {
|
||||
JsonObject JsonData = JsonParser.parseString(RawData).getAsJsonObject();
|
||||
JsonArray MoviesArray = JsonData.getAsJsonArray("movies");
|
||||
Type MovieListType = new TypeToken<ArrayList<Movie>>(){}.getType();
|
||||
Gson GsonHandle = new Gson();
|
||||
List<Movie> Movies = GsonHandle.fromJson(MoviesArray, MovieListType);
|
||||
|
||||
System.out.printf("\nMovies with %s genre:\n", JsonData.getAsJsonObject("genre").get("name").getAsString());
|
||||
for (Movie CurrentMovie : Movies)
|
||||
System.out.printf("ID: %d | %s (%s)\n", CurrentMovie.getId(), CurrentMovie.getTitle(), CurrentMovie.getYear());
|
||||
|
||||
System.out.printf("\nTotal: %s - Total pages: %s", JsonData.get("total").getAsString(), JsonData.get("total_pages").getAsString());
|
||||
|
||||
System.out.println();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("ERROR: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
@@ -117,18 +146,25 @@ public class MovieService {
|
||||
* BONUS: Fetch movie details from API using movie ID
|
||||
* URL format: https://api.meshcomp.ir/api/v1/movies/{movieId}
|
||||
*/
|
||||
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<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
// if (response.statusCode() == 200) return response.body();
|
||||
private String getMovieById(Long movieId)
|
||||
{
|
||||
try
|
||||
{
|
||||
final String URL = MOVIE_BY_ID_URL + movieId;
|
||||
|
||||
HttpRequest Request = HttpRequest.newBuilder().uri(URI.create(URL)).build();
|
||||
HttpResponse<String> Respone = client.send(Request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
if (Respone.statusCode() == HttpURLConnection.HTTP_OK)
|
||||
return Respone.body();
|
||||
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.out.println("!!Exception : " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -158,30 +194,29 @@ public class MovieService {
|
||||
*
|
||||
* @param movieId The ID of the movie to display
|
||||
*/
|
||||
public void displayMovieDetails(Long movieId) {
|
||||
// Step 1: Get JSON response from API
|
||||
public void displayMovieDetails(Long movieId)
|
||||
{
|
||||
String jsonResponse = getMovieById(movieId);
|
||||
|
||||
// Step 2: Check if we got valid data
|
||||
if (jsonResponse == null) {
|
||||
if (jsonResponse == null)
|
||||
{
|
||||
System.out.println("Failed to get movie details for ID: " + movieId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 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()
|
||||
try
|
||||
{
|
||||
JsonObject JsonData = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
||||
|
||||
// Your code here:
|
||||
Gson GsonHandle = new Gson();
|
||||
Type MovieType = new TypeToken<Movie>(){}.getType();
|
||||
Movie RequestedMovie = GsonHandle.fromJson(JsonData, MovieType);
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
System.out.println();
|
||||
System.out.println(RequestedMovie);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("ERROR parsing movie details: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user