|
|
|
@@ -1,7 +1,8 @@
|
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
import org.jsoup.nodes.Document;
|
|
|
|
|
import org.jsoup.nodes.Element;
|
|
|
|
|
import org.jsoup.select.Elements;
|
|
|
|
|
import org.jsoup.Jsoup;
|
|
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.*;
|
|
|
|
@@ -18,6 +19,8 @@ public class Parser {
|
|
|
|
|
// TODO: Sort the 'sortedByTitle' list alphabetically by the movie's title.
|
|
|
|
|
// Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator.
|
|
|
|
|
// Example: Comparator.comparing(Movie::getTitle)
|
|
|
|
|
sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
|
|
|
|
|
|
|
|
|
|
return sortedByTitle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -25,6 +28,7 @@ public class Parser {
|
|
|
|
|
List<Movie> sortedByRating = new ArrayList<>(movies);
|
|
|
|
|
// TODO: Sort the 'sortedByRating' list by the movie's rating in descending order (highest to lowest).
|
|
|
|
|
// Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed().
|
|
|
|
|
sortedByRating.sort(Comparator.comparingDouble(Movie::getRating).reversed());
|
|
|
|
|
return sortedByRating;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -32,15 +36,19 @@ public class Parser {
|
|
|
|
|
List<Movie> sortedByYear = new ArrayList<>(movies);
|
|
|
|
|
// TODO: Sort the 'sortedByYear' list by the movie's release year in descending order (newest to oldest).
|
|
|
|
|
// Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed().
|
|
|
|
|
sortedByYear.sort(Comparator.comparingDouble(Movie::getYear).reversed());
|
|
|
|
|
return sortedByYear;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setUp(String filePath) throws IOException {
|
|
|
|
|
// TODO: Create a java.io.File object using the given 'filePath'.
|
|
|
|
|
File in=new File(filePath);
|
|
|
|
|
// TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object.
|
|
|
|
|
Document doc = Jsoup.parse(in, "UTF-8");
|
|
|
|
|
|
|
|
|
|
// TODO: Extract the list of movie elements from the Document.
|
|
|
|
|
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie".
|
|
|
|
|
Elements elements= doc.getElementsByClass("movie");
|
|
|
|
|
|
|
|
|
|
// TODO: Iterate through each movie Element.
|
|
|
|
|
// For each movie element:
|
|
|
|
@@ -48,5 +56,15 @@ public class Parser {
|
|
|
|
|
// 2. Find the rating element (e.g., using selector "span.movie-rating"). Extract the text, remove the "/10" part, and parse it to a double.
|
|
|
|
|
// 3. Find the year element (e.g., using selector "span.movie-year"). Extract the text and parse it to an int.
|
|
|
|
|
// 4. Create a new Movie object with these values and add it to the 'movies' list.
|
|
|
|
|
for (Element element : elements) {
|
|
|
|
|
String title = element.select("h3.movie-title").text();
|
|
|
|
|
String rating = element.select("span.movie-rating").text();
|
|
|
|
|
rating = rating.replace("/10", "");
|
|
|
|
|
double numOfRating = Double.parseDouble(rating);
|
|
|
|
|
String year = element.select("span.movie-year").text();
|
|
|
|
|
int numOfYear = Integer.parseInt(year);
|
|
|
|
|
|
|
|
|
|
movies.add(new Movie(title, numOfRating, numOfYear));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|