From e17cbb319ada7d14c7eccecad8e62f8bbdf61f2c Mon Sep 17 00:00:00 2001 From: 2025mohseni Date: Mon, 8 Jun 2026 21:38:30 +0330 Subject: [PATCH] All is well --- src/main/java/Main.java | 24 +++++++------- src/main/java/Movie.java | 18 +++++++---- src/main/java/Parser.java | 67 +++++++++++++++++++++------------------ 3 files changed, 60 insertions(+), 49 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 473dd4d..1c7a60e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,22 +1,20 @@ import java.io.IOException; +import java.util.List; public class Main { public static void main(String[] args) { - // The relative path automatically points to the resources folder - 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()); - + // مسیر فایل HTML بر اساس ساختار استاندارد پروژه‌های میون + Parser parser = new Parser("src/main/resources/Movies.html"); + + System.out.println("--- Movies sorted by Year (Descending) ---"); + List sortedByYear = parser.sortByYear(); + for (Movie movie : sortedByYear) { + System.out.println(movie); + } + } catch (IOException e) { - System.err.println("Error reading or parsing the HTML file: " + e.getMessage()); + System.err.println("Error reading the HTML file: " + e.getMessage()); } } } diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fc257fe..9d0001d 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -6,8 +6,9 @@ public class Movie { 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() { @@ -24,9 +25,7 @@ public class Movie { @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 @@ -34,6 +33,13 @@ public class Movie { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; 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 && + year == movie.year && + Objects.equals(title, movie.title); + } + + @Override + public int hashCode() { + return Objects.hash(title, rating, year); } } diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 516cf14..e5ba98d 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -2,51 +2,58 @@ 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.*; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; public class Parser { - private List movies = new ArrayList<>(); + private List movies; public Parser(String filePath) throws IOException { + movies = new ArrayList<>(); setUp(filePath); } + private void setUp(String filePath) throws IOException { + File input = new File(filePath); + Document doc = Jsoup.parse(input, "UTF-8"); + + Elements movieElements = doc.select(".movie"); + + for (Element movieElement : movieElements) { + String title = movieElement.select("h3.movie-title").text(); + + String ratingText = movieElement.select("span.movie-rating").text().replace("/10", "").trim(); + double rating = Double.parseDouble(ratingText); + + String yearRaw = movieElement.select("span.movie-year").text(); + int year = Integer.parseInt(yearRaw.replaceAll("[()\\s]", "")); + + movies.add(new Movie(title, rating, year)); + } + } + 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) - return sortedByTitle; + return movies.stream() + .sorted(Comparator.comparing(Movie::getTitle)) + .collect(Collectors.toList()); } 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(). - return sortedByRating; + // مرتب‌سازی نزولی بر اساس امتیاز + return movies.stream() + .sorted(Comparator.comparing(Movie::getRating).reversed()) + .collect(Collectors.toList()); } 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(). - 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. + // مرتب‌سازی نزولی بر اساس سال + return movies.stream() + .sorted(Comparator.comparing(Movie::getYear).reversed()) + .collect(Collectors.toList()); } } -- 2.54.0