From 783d8faecabccd6eb6a73ca201b3cdc7b038e1a6 Mon Sep 17 00:00:00 2001 From: Amir Ali Amiri Date: Mon, 15 Jun 2026 13:07:04 +0330 Subject: [PATCH] Implement movie parser and sorting functionality using JSoup --- src/main/java/Main.java | 30 ++++++++++++++++-------- src/main/java/Movie.java | 13 ++++++----- src/main/java/Parser.java | 48 ++++++++++++++++++++++++--------------- 3 files changed, 57 insertions(+), 34 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 473dd4d..472dc89 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,21 +2,31 @@ import java.io.IOException; public class Main { public static void main(String[] args) { - // The relative path automatically points to the resources folder + // مسیر نسبی فایل HTML در پوشه ریسورس‌ها String filePath = "src/main/resources/Movies.html"; try { - // Instantiate the parser + // راه اندازی پارسر برای خواندن داده‌ها Parser parser = new Parser(filePath); - - // TODO: You can test your code here before you run the unit tests. - // Example: Call your sorting methods, iterate through the returned list, - // and print out the movies to see if they match the expected results in the Help folder. - - System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); - + + // چاپ فیلم‌ها بر اساس حروف الفبا + System.out.println("--- Movies Sorted by Title ---"); + parser.sortByTitle().forEach(System.out::println); + + // چاپ فیلم‌ها بر اساس بالاترین امتیاز + System.out.println("\n--- Movies Sorted by Rating (Descending) ---"); + parser.sortByRating().forEach(System.out::println); + + // چاپ فیلم‌ها بر اساس جدیدترین سال ساخت + System.out.println("\n--- Movies Sorted by Year (Newest First) ---"); + parser.sortByYear().forEach(System.out::println); + + // نمایش تعداد کل فیلم‌های پارس شده در انتهای کار + System.out.println("\nTotal movies parsed: " + parser.sortByTitle().size()); + } catch (IOException e) { + // مدیریت خطاهای احتمالی در زمان خواندن یا نبودن فایل System.err.println("Error reading or parsing the HTML file: " + e.getMessage()); } } -} +} \ No newline at end of file diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fc257fe..6f80dc4 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -5,9 +5,11 @@ public class Movie { private double rating; private int year; + // سازنده کلاس برای مقداردهی اولیه به متغیرهای فیلم public Movie(String title, double rating, int year) { - // TODO: Initialize the instance variables (this.title, this.rating, this.year) - // with the values passed as arguments to this constructor. + this.title = title; + this.rating = rating; + this.year = year; } public String getTitle() { @@ -22,11 +24,10 @@ public class Movie { return year; } + // تبدیل شیء فیلم به یک متن خوانا با فرمت درخواستی صورت پروژه @Override public String toString() { - // TODO: Return a string representation of the movie matching the exact required format. - // Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972" - return ""; + return "Movie: " + title + ", Rating: " + rating + ", Year: " + year; } @Override @@ -36,4 +37,4 @@ public class Movie { Movie movie = (Movie) o; return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title); } -} +} \ No newline at end of file diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 516cf14..ddc82c2 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -7,46 +7,58 @@ import java.io.IOException; import java.util.*; public class Parser { + // لیستی برای ذخیره کردن فیلم‌های استخراج شده از HTML private List movies = new ArrayList<>(); public Parser(String filePath) throws IOException { setUp(filePath); } + //مرتب‌سازی فیلم‌ها به صورت الفبایی بر اساس عنوان (A تا Z) 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; } + // متد اصلی بارگذاری فایل و استخراج اطلاعات فیلم‌ها با JSoup 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. + // باز کردن فایل HTML از روی مسیر داده شده و پارس کردن ساختار آن + File file = new File(filePath); + Document document = Jsoup.parse(file, "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". + // پیدا کردن تمام بلاک‌های مربوط به فیلم‌ها که کلاس "movie" دارند + Elements movieElements = document.getElementsByClass("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. + // پیمایش تک‌تک فیلم‌ها برای بیرون کشیدن جزئیات + for (Element element : movieElements) { + // استخراج عنوان فیلم از تگ h3 + String title = element.selectFirst("h3.movie-title").text(); + + // استخراج امتیاز و حذف بخش اضافی "/10" برای تبدیل راحت به عدد اعشاری + String ratingText = element.selectFirst("span.movie-rating").text(); + double rating = Double.parseDouble(ratingText.replace("/10", "").trim()); + + // استخراج سال ساخت و تبدیل متن به عدد صحیح (int) + String yearText = element.selectFirst("span.movie-year").text(); + int year = Integer.parseInt(yearText.trim()); + + // ساخت شیء فیلم جدید و اضافه کردن آن به لیست اصلی + movies.add(new Movie(title, rating, year)); + } } -} +} \ No newline at end of file