1 Commits
Author SHA1 Message Date
AmirAli_Amiri 783d8faeca Implement movie parser and sorting functionality using JSoup 2026-06-15 13:07:04 +03:30
3 changed files with 57 additions and 34 deletions
+20 -10
View File
@@ -2,21 +2,31 @@ import java.io.IOException;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
// The relative path automatically points to the resources folder // مسیر نسبی فایل HTML در پوشه ریسورس‌ها
String filePath = "src/main/resources/Movies.html"; String filePath = "src/main/resources/Movies.html";
try { try {
// Instantiate the parser // راه اندازی پارسر برای خواندن داده‌ها
Parser parser = new Parser(filePath); 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, System.out.println("--- Movies Sorted by Title ---");
// and print out the movies to see if they match the expected results in the Help folder. parser.sortByTitle().forEach(System.out::println);
System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); // چاپ فیلم‌ها بر اساس بالاترین امتیاز
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) { } catch (IOException e) {
// مدیریت خطاهای احتمالی در زمان خواندن یا نبودن فایل
System.err.println("Error reading or parsing the HTML file: " + e.getMessage()); System.err.println("Error reading or parsing the HTML file: " + e.getMessage());
} }
} }
} }
+7 -6
View File
@@ -5,9 +5,11 @@ public class Movie {
private double rating; private double rating;
private int year; private int year;
// سازنده کلاس برای مقداردهی اولیه به متغیرهای فیلم
public Movie(String title, double rating, int year) { public Movie(String title, double rating, int year) {
// TODO: Initialize the instance variables (this.title, this.rating, this.year) this.title = title;
// with the values passed as arguments to this constructor. this.rating = rating;
this.year = year;
} }
public String getTitle() { public String getTitle() {
@@ -22,11 +24,10 @@ public class Movie {
return year; return year;
} }
// تبدیل شیء فیلم به یک متن خوانا با فرمت درخواستی صورت پروژه
@Override @Override
public String toString() { public String toString() {
// TODO: Return a string representation of the movie matching the exact required format. return "Movie: " + title + ", Rating: " + rating + ", Year: " + year;
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
return "";
} }
@Override @Override
@@ -36,4 +37,4 @@ public class Movie {
Movie movie = (Movie) o; Movie movie = (Movie) o;
return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title); return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title);
} }
} }
+30 -18
View File
@@ -7,46 +7,58 @@ import java.io.IOException;
import java.util.*; import java.util.*;
public class Parser { public class Parser {
// لیستی برای ذخیره کردن فیلم‌های استخراج شده از HTML
private List<Movie> movies = new ArrayList<>(); private List<Movie> movies = new ArrayList<>();
public Parser(String filePath) throws IOException { public Parser(String filePath) throws IOException {
setUp(filePath); setUp(filePath);
} }
//مرتب‌سازی فیلم‌ها به صورت الفبایی بر اساس عنوان (A تا Z)
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;
} }
// متد اصلی بارگذاری فایل و استخراج اطلاعات فیلم‌ها با JSoup
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'. // باز کردن فایل HTML از روی مسیر داده شده و پارس کردن ساختار آن
// TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object. File file = new File(filePath);
Document document = Jsoup.parse(file, "UTF-8");
// TODO: Extract the list of movie elements from the Document. // پیدا کردن تمام بلاک‌های مربوط به فیلم‌ها که کلاس "movie" دارند
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie". Elements movieElements = document.getElementsByClass("movie");
// TODO: Iterate through each movie Element. // پیمایش تک‌تک فیلم‌ها برای بیرون کشیدن جزئیات
// For each movie element: for (Element element : movieElements) {
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text. // استخراج عنوان فیلم از تگ h3
// 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. String title = element.selectFirst("h3.movie-title").text();
// 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. // استخراج امتیاز و حذف بخش اضافی "/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));
}
} }
} }