From 64cb9d56bc3e4b107dad2d23fb90b7815eb021bc Mon Sep 17 00:00:00 2001 From: ShayanEdalatjoo Date: Fri, 17 Jul 2026 21:12:31 +0330 Subject: [PATCH] finishing Parser.java --- src/main/java/Parser.java | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 516cf14..5c0d2b4 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -15,38 +15,33 @@ public class Parser { public List sortByTitle() { List sortedByTitle = new ArrayList<>(movies); - // 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; } public List sortByRating() { List 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; } public List sortByYear() { List 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.comparingInt(Movie::getYear).reversed()); return sortedByYear; } private void setUp(String filePath) throws IOException { - // TODO: Create a java.io.File object using the given 'filePath'. - // TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object. - - // 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". - - // TODO: Iterate through each movie Element. - // For each movie element: - // 1. Find the title element (e.g., using selector "h3.movie-title") and get its text. - // 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. + File file = new File(filePath); + Document document = Jsoup.parse(file, "UTF-8"); + Elements movieElements = document.getElementsByClass("movie"); + for (Element element : movieElements) { + String title = element.select("h3.movie-title").text(); + String ratingStr = element.select("span.movie-rating").text().replace("/10", "").trim(); + double rating = Double.parseDouble(ratingStr); + String yearStr = element.select("span.movie-year").text().trim(); + int year = Integer.parseInt(yearStr); + movies.add(new Movie(title, rating, year)); + } } }