implement all TODO tasks
This commit is contained in:
@@ -22,15 +22,16 @@ public class Main {
|
|||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
// TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre()
|
// TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre()
|
||||||
long generId = getLongInput("Enter the ID: ");
|
long genreId = getLongInput("Enter the genre ID: ");
|
||||||
long page = getLongInput("Enter the page: ");
|
long page = getLongInput("Enter the page: ");
|
||||||
movieService.displayMoviesByGenre(generId, page);
|
movieService.displayMoviesByGenre(genreId, page);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
// TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails()
|
// TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails()
|
||||||
|
long movieId = getLongInput("Enter the movie ID: ");
|
||||||
|
movieService.displayMovieDetails(movieId);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -15,10 +15,30 @@ public class Movie {
|
|||||||
private String year;
|
private String year;
|
||||||
private String imdb_rating;
|
private String imdb_rating;
|
||||||
private String country;
|
private String country;
|
||||||
|
private String actors;
|
||||||
|
private String awards;
|
||||||
|
private String director;
|
||||||
|
private String imdb_id;
|
||||||
|
private String plot;
|
||||||
|
private String released;
|
||||||
|
private String runtime;
|
||||||
|
private String type;
|
||||||
|
private String writer;
|
||||||
|
|
||||||
|
private String rated;
|
||||||
|
|
||||||
|
private String imdb_votes;
|
||||||
|
private String metascore;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: Create a constructor with all fields
|
// TODO: Create a constructor with all fields
|
||||||
public Movie(Long id, String title, List<String> genres, List<String> images, String year, String imdb_rating, String country) {
|
public Movie(Long id, String title, List<String> genres, List<String> images, String year,
|
||||||
|
String imdb_rating, String country, String actors, String awards,
|
||||||
|
String director, String imdb_id, String plot, String released,
|
||||||
|
String runtime, String type, String writer, String rated, String imdb_votes, String metascore) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
this.genres = genres;
|
this.genres = genres;
|
||||||
@@ -26,6 +46,18 @@ public class Movie {
|
|||||||
this.year = year;
|
this.year = year;
|
||||||
this.imdb_rating = imdb_rating;
|
this.imdb_rating = imdb_rating;
|
||||||
this.country = country;
|
this.country = country;
|
||||||
|
this.actors = actors;
|
||||||
|
this.awards = awards;
|
||||||
|
this.director = director;
|
||||||
|
this.imdb_id = imdb_id;
|
||||||
|
this.plot = plot;
|
||||||
|
this.released = released;
|
||||||
|
this.runtime = runtime;
|
||||||
|
this.type = type;
|
||||||
|
this.writer = writer;
|
||||||
|
this.rated = rated;
|
||||||
|
this.imdb_votes = imdb_votes;
|
||||||
|
this.metascore = metascore;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Create a simple constructor with id and title only
|
// TODO: Create a simple constructor with id and title only
|
||||||
@@ -34,6 +66,8 @@ public class Movie {
|
|||||||
this.id = id;
|
this.id = id;
|
||||||
this.title = title;
|
this.title = title;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO: Create getters and setters for all fields
|
// TODO: Create getters and setters for all fields
|
||||||
|
|
||||||
public List<String> getGenres() {
|
public List<String> getGenres() {
|
||||||
@@ -92,6 +126,99 @@ public class Movie {
|
|||||||
this.year = year;
|
this.year = year;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getActors() {
|
||||||
|
return actors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setActors(String actors) {
|
||||||
|
this.actors = actors;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAwards() {
|
||||||
|
return awards;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAwards(String awards) {
|
||||||
|
this.awards = awards;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDirector() {
|
||||||
|
return director;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDirector(String director) {
|
||||||
|
this.director = director;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getImdb_id() {
|
||||||
|
return imdb_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImdb_id(String imdb_id) {
|
||||||
|
this.imdb_id = imdb_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getImdb_votes() {
|
||||||
|
return imdb_votes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setImdb_votes(String imdb_votes) {
|
||||||
|
this.imdb_votes = imdb_votes;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMetascore() {
|
||||||
|
return metascore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMetascore(String metascore) {
|
||||||
|
this.metascore = metascore;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPlot() {
|
||||||
|
return plot;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPlot(String plot) {
|
||||||
|
this.plot = plot;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRated() {
|
||||||
|
return rated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRated(String rated) {
|
||||||
|
this.rated = rated;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getReleased() {
|
||||||
|
return released;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setReleased(String released) {
|
||||||
|
this.released = released;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRuntime() {
|
||||||
|
return runtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRuntime(String runtime) {
|
||||||
|
this.runtime = runtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setType(String type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getWriter() {
|
||||||
|
return writer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setWriter(String writer) {
|
||||||
|
this.writer = writer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,6 +10,7 @@ import java.net.http.HttpRequest;
|
|||||||
import java.net.http.HttpResponse;
|
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.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MovieService {
|
public class MovieService {
|
||||||
@@ -137,10 +138,10 @@ public class MovieService {
|
|||||||
private String getMovieById(Long movieId) {
|
private String getMovieById(Long movieId) {
|
||||||
try {
|
try {
|
||||||
// TODO: Complete this method
|
// TODO: Complete this method
|
||||||
// String url = MOVIE_BY_ID_URL + movieId;
|
String url = MOVIE_BY_ID_URL + movieId;
|
||||||
// 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());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
// if (response.statusCode() == 200) return response.body();
|
if (response.statusCode() == 200) return response.body();
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -198,13 +199,48 @@ public class MovieService {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
// TODO: Parse jsonResponse to JsonObject
|
// TODO: Parse jsonResponse to JsonObject
|
||||||
// HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject()
|
//HINT: Use JsonParser.parseString(jsonResponse).getAsJsonObject()
|
||||||
|
JsonObject obj = JsonParser.parseString(jsonResponse).getAsJsonObject();
|
||||||
// TODO: use Gson to parse the json
|
// TODO: use Gson to parse the json
|
||||||
// Your code here:
|
// Your code here:
|
||||||
|
|
||||||
|
Gson gson = new Gson();
|
||||||
|
Movie movie = gson.fromJson(obj, Movie.class);
|
||||||
|
StringBuilder genresString = new StringBuilder();
|
||||||
|
for (int i = 0; i < movie.getGenres().size(); i++) {
|
||||||
|
if(i == movie.getGenres().size() - 1)
|
||||||
|
{
|
||||||
|
genresString.append(movie.getGenres().get(i));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
genresString.append(movie.getGenres().get(i)).append(", ");
|
||||||
|
|
||||||
|
}
|
||||||
|
System.out.println("=======================================================================");
|
||||||
|
int mid = (71 - movie.getTitle().length())/ 2;
|
||||||
|
System.out.print(" ".repeat(mid) + movie.getTitle()+"\n");
|
||||||
|
System.out.println("=======================================================================");
|
||||||
|
System.out.printf("""
|
||||||
|
Rating: %s | Votes: %s
|
||||||
|
Director: %s
|
||||||
|
Cast: %s
|
||||||
|
Genre: %s
|
||||||
|
Runtime: %s | Rated: %s
|
||||||
|
Country: %s | Released: %s
|
||||||
|
Plot: %s
|
||||||
|
Awards: %s
|
||||||
|
""",
|
||||||
|
movie.getImdb_rating(), movie.getImdb_votes(), movie.getDirector(), movie.getActors(),
|
||||||
|
genresString.toString(),movie.getRuntime(), movie.getRated(), movie.getCountry(), movie.getReleased(),
|
||||||
|
movie.getPlot(), movie.getAwards());
|
||||||
|
System.out.println("=======================================================================");
|
||||||
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("ERROR parsing movie details: " + e.getMessage());
|
System.err.println("ERROR parsing movie details: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user