1 Commits
Author SHA1 Message Date
2025mohseni e17cbb319a All is well 2026-06-08 21:38:30 +03:30
3 changed files with 60 additions and 49 deletions
+11 -13
View File
@@ -1,22 +1,20 @@
import java.io.IOException; import java.io.IOException;
import java.util.List;
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
String filePath = "src/main/resources/Movies.html";
try { try {
// Instantiate the parser // مسیر فایل HTML بر اساس ساختار استاندارد پروژه‌های میون
Parser parser = new Parser(filePath); Parser parser = new Parser("src/main/resources/Movies.html");
// TODO: You can test your code here before you run the unit tests. System.out.println("--- Movies sorted by Year (Descending) ---");
// Example: Call your sorting methods, iterate through the returned list, List<Movie> sortedByYear = parser.sortByYear();
// and print out the movies to see if they match the expected results in the Help folder. for (Movie movie : sortedByYear) {
System.out.println(movie);
System.out.println("Movies parsed successfully! Total movies: " + 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 the HTML file: " + e.getMessage());
} }
} }
} }
+12 -6
View File
@@ -6,8 +6,9 @@ public class Movie {
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() {
@@ -24,9 +25,7 @@ public class Movie {
@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
@@ -34,6 +33,13 @@ public class Movie {
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
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 &&
year == movie.year &&
Objects.equals(title, movie.title);
}
@Override
public int hashCode() {
return Objects.hash(title, rating, year);
} }
} }
+37 -30
View File
@@ -2,51 +2,58 @@ import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.io.File; import java.io.File;
import java.io.IOException; 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 { public class Parser {
private List<Movie> movies = new ArrayList<>(); private List<Movie> movies;
public Parser(String filePath) throws IOException { public Parser(String filePath) throws IOException {
movies = new ArrayList<>();
setUp(filePath); 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<Movie> sortByTitle() { public List<Movie> sortByTitle() {
List<Movie> sortedByTitle = new ArrayList<>(movies); return movies.stream()
// TODO: Sort the 'sortedByTitle' list alphabetically by the movie's title. .sorted(Comparator.comparing(Movie::getTitle))
// Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator. .collect(Collectors.toList());
// Example: Comparator.comparing(Movie::getTitle)
return sortedByTitle;
} }
public List<Movie> sortByRating() { public List<Movie> sortByRating() {
List<Movie> sortedByRating = new ArrayList<>(movies); // مرتب‌سازی نزولی بر اساس امتیاز
// TODO: Sort the 'sortedByRating' list by the movie's rating in descending order (highest to lowest). return movies.stream()
// Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed(). .sorted(Comparator.comparing(Movie::getRating).reversed())
return sortedByRating; .collect(Collectors.toList());
} }
public List<Movie> sortByYear() { public List<Movie> sortByYear() {
List<Movie> sortedByYear = new ArrayList<>(movies); // مرتب‌سازی نزولی بر اساس سال
// TODO: Sort the 'sortedByYear' list by the movie's release year in descending order (newest to oldest). return movies.stream()
// Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed(). .sorted(Comparator.comparing(Movie::getYear).reversed())
return sortedByYear; .collect(Collectors.toList());
}
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.
} }
} }