finishing Parser.java

This commit is contained in:
2026-07-17 21:12:31 +03:30
parent 3b6502ac9c
commit 64cb9d56bc
+14 -19
View File
@@ -15,38 +15,33 @@ public class Parser {
public List<Movie> sortByTitle() { public List<Movie> sortByTitle() {
List<Movie> sortedByTitle = new ArrayList<>(movies); List<Movie> sortedByTitle = new ArrayList<>(movies);
// TODO: Sort the 'sortedByTitle' list alphabetically by the movie's title. sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
// Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator.
// Example: Comparator.comparing(Movie::getTitle)
return sortedByTitle; return sortedByTitle;
} }
public List<Movie> sortByRating() { public List<Movie> sortByRating() {
List<Movie> sortedByRating = new ArrayList<>(movies); List<Movie> sortedByRating = new ArrayList<>(movies);
// TODO: Sort the 'sortedByRating' list by the movie's rating in descending order (highest to lowest). sortedByRating.sort(Comparator.comparingDouble(Movie::getRating).reversed());
// Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed().
return sortedByRating; return sortedByRating;
} }
public List<Movie> sortByYear() { public List<Movie> sortByYear() {
List<Movie> sortedByYear = new ArrayList<>(movies); List<Movie> sortedByYear = new ArrayList<>(movies);
// TODO: Sort the 'sortedByYear' list by the movie's release year in descending order (newest to oldest). sortedByYear.sort(Comparator.comparingInt(Movie::getYear).reversed());
// Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed().
return sortedByYear; return sortedByYear;
} }
private void setUp(String filePath) throws IOException { private void setUp(String filePath) throws IOException {
// TODO: Create a java.io.File object using the given 'filePath'. File file = new File(filePath);
// TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object. Document document = Jsoup.parse(file, "UTF-8");
Elements movieElements = document.getElementsByClass("movie");
// TODO: Extract the list of movie elements from the Document. for (Element element : movieElements) {
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie". String title = element.select("h3.movie-title").text();
String ratingStr = element.select("span.movie-rating").text().replace("/10", "").trim();
// TODO: Iterate through each movie Element. double rating = Double.parseDouble(ratingStr);
// For each movie element: String yearStr = element.select("span.movie-year").text().trim();
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text. int year = Integer.parseInt(yearStr);
// 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. movies.add(new Movie(title, rating, year));
// 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.
} }
} }