2 Commits
Author SHA1 Message Date
HadiSharifi 7391dc595b complete Parser class 2026-05-13 15:37:07 +03:30
HadiSharifi 963153c0b3 complete Movie class 2026-05-13 15:29:45 +03:30
3 changed files with 22 additions and 31 deletions
+5 -7
View File
@@ -2,17 +2,15 @@ import java.io.IOException;
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.
for (Movie movie: parser.sortByTitle()){
System.out.println(movie);
}
System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size());
} catch (IOException e) {
+5 -5
View File
@@ -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,8 @@ 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 "";
String result = "Title: " + title + ", Rating: " + rating + ", Year: " + year;
return result;
}
@Override
+12 -19
View File
@@ -15,38 +15,31 @@ public class Parser {
public List<Movie> sortByTitle() {
List<Movie> 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<Movie> sortByRating() {
List<Movie> 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.comparing(Movie::getRating).reversed());
return sortedByRating;
}
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).
// Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed().
sortedByYear.sort(Comparator.comparing(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.
Document document = Jsoup.parse(new File(filePath), "utf-8");
Elements elements = document.getElementsByClass("movie");
for (Element element : elements) {
String title = element.getElementsByClass("movie-title").text();
int year = Integer.parseInt(element.getElementsByClass("movie-year").text());
double rating = Double.parseDouble(element.getElementsByClass("movie-rating")
.text().replace("/10",""));
movies.add(new Movie(title,rating,year));
}
}
}