import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.File; import java.io.IOException; import java.util.*; public class Parser { private List movies = new ArrayList<>(); public Parser(String filePath) throws IOException { setUp(filePath); } public List sortByTitle() { List sortedByTitle = new ArrayList<>(movies); sortedByTitle.sort(Comparator.comparing(Movie::getTitle)); return sortedByTitle; } public List sortByRating() { List sortedByRating = new ArrayList<>(movies); sortedByRating.sort(Comparator.comparingDouble(Movie::getRating).reversed()); return sortedByRating; } public List sortByYear() { List sortedByYear = new ArrayList<>(movies); sortedByYear.sort(Comparator.comparingInt(Movie::getYear).reversed()); return sortedByYear; } private void setUp(String filePath) throws IOException { File file = new File(filePath); Document document = Jsoup.parse(file, "UTF-8"); Elements movieElements = document.select("div.movie"); for (Element movieElement : movieElements) { String title = movieElement.select("h3.movie-title").text(); String ratingText = movieElement.select("span.movie-rating").text(); ratingText = ratingText.replace("/10", ""); double rating = Double.parseDouble(ratingText); String yearText = movieElement.select("span.movie-year").text(); int year = Integer.parseInt(yearText); Movie movie = new Movie(title, rating, year); movies.add(movie); } } }