completing task1 & task2 #1

Open
Sarazpr wants to merge 1 commits from develop into main
5 changed files with 93 additions and 19 deletions
+2 -2
View File
@@ -56,7 +56,7 @@ Only complete the TODO methods.
2. Write a method that receives the JSON string returned by `getMoviesByGenreList()` and converts it into a `List<Movie>`.
3. Write another method that displays the list of movies (for the selected genre) in a clean and readable format.
3. Write another method that displays the list of movies (for the selected genre) in a clean and readable format.////////////////////////check!
4. Implement a console menu in `Main` that allows the user to:
- View the list of genres
@@ -201,5 +201,5 @@ Jackson Tutorial:
---
## Submission ⌛
Deadline: May 1st (11 Ordibehesht)
Deadline: May 1st (11 Ordibehesht
+21 -6
View File
@@ -55,41 +55,56 @@ public class Rational {
// 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); //Done!
}
// 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); //Done!
}
// 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); //Done!
}
// 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);
//Done!
}
// TODO: Instance method - this / other
// Formula: (a/b) ÷ (c/d) = (a*d) / (b*c)
public Rational divide(Rational other) {
// YOUR CODE HERE
if (other.numerator == 0) {
throw new IllegalArgumentException("other Numerator cannot be zero"); // other Numerator?????
}
int newNumerator = this.numerator * other.denominator;
int newDenominator = other.numerator * this.denominator;
return new Rational(newNumerator, newDenominator); // Done!
// HINT: Division is multiplication by reciprocal
// Don't forget to check if other.numerator is zero!
return null; // Remove this line when implemented
}
// 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
+9 -3
View File
@@ -8,6 +8,7 @@ public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("\n=== MOVIE API CLIENT ===\n");
boolean running = true;
@@ -23,15 +24,20 @@ public class Main {
case 2:
// TODO: Get genre ID and page from user, then call movieService.displayMoviesByGenre()
Long ID = getLongInput("please enter your ID genre: ");
Long page = getLongInput("and page number you want to visit: ");
movieService.displayMoviesByGenre(ID, page);
break;
case 3:
/*case 3:
// TODO (BONUS): Get movie ID from user, then call movieService.displayMovieDetails()
break;
*/
case 0:
running = false;
System.out.println("Goodbye!");
@@ -58,7 +64,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 movie details by ID");
// System.out.println("3. Get movie details by ID");
System.out.println("0. Exit");
System.out.println("================================");
}
+37 -3
View File
@@ -10,15 +10,49 @@ public class Movie {
private Long id;
private String title;
private List<Genre> genres;
private String images;
private List<String> genres;
private List<String> images;
private String year;
private String imdbRating;
private String imdb_Rating;
private String country;
// TODO: Create a constructor with all fields
public Movie(Long id, String title, List<String> genres, List<String> images, String year, String imdbRating, String country) {
this.id = id;
this.title = title;
this.genres = genres;
this.images = images;
this.year = year;
this.imdb_Rating = imdbRating;
this.country = country;
}
// TODO: Create a simple constructor with id and title only
public Movie(Long id, String title) {
//???
this.id = id;
this.title = title;
}
// TODO: Create getters and setters for all fields
public Long getId() {return id;}
public void setId(Long id) {this.id = id;}
public String getTitle() {return title;}
public void setTitle(String title) {this.title = title;}
public List<String> getGenres() {return genres;}
public void setGenres(List<String> genres) {this.genres = genres;}
public List<String> getImages() {return images;}
public void setImages(List<String> images) {this.images = images;}
public String getYear() {return year;}
public void setYear(String year) {this.year = year;}
public String getImdbRating() {return imdb_Rating;}
public void setImdbRating(String imdbRating) {this.imdb_Rating = imdbRating;}
public String getCountry() {return country;}
public void setCountry(String country) {this.country = country;}
}
+23 -4
View File
@@ -103,9 +103,28 @@ public class MovieService {
*/
public void displayMoviesByGenre(Long genreId, Long page) {
// TODO: Write your code here
String jsonResponse = getMoviesByGenreList(genreId, page);
int i = 0;
// All parsing code must be inside try-catch
try {
// Your code here
JsonObject rootObject = JsonParser.parseString(jsonResponse).getAsJsonObject();
JsonArray moviesArray = rootObject.getAsJsonArray("movies");
Type movieListType = new TypeToken<ArrayList<Movie>>(){}.getType();
Gson gson = new Gson();
List<Movie> movies = gson.fromJson(moviesArray, movieListType);
int total_pages = rootObject.get("total_pages").getAsInt();
System.out.println("\nALL MOVIES:");
for (Movie movie : movies) {
System.out.println("\"ID: {" + movie.getId() + "} | {" + movie.getTitle() + "} ({" + movie.getYear() + "})\"");
i++; // total
}
System.out.println();
System.out.println("total: "+ i);
System.out.println("total pages of this genre: "+ total_pages);
System.out.println();
} catch (Exception e) {
System.err.println("ERROR: " + e.getMessage());
@@ -120,10 +139,10 @@ public class MovieService {
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();
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) {