From 733e0376fa287a09e2ea8022b41fe07330c394e6 Mon Sep 17 00:00:00 2001 From: Bita Date: Mon, 11 May 2026 13:19:43 +0330 Subject: [PATCH 1/3] Complete classs Movie --- src/main/java/Movie.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fc257fe..e01e9c8 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 : " + getTitle() + ", Rating : " + getRating() + ", Year : " + getYear(); } @Override -- 2.54.0 From c91e72a304274259fa8a9aa983c25f340603321a Mon Sep 17 00:00:00 2001 From: Bita Date: Mon, 11 May 2026 14:50:39 +0330 Subject: [PATCH 2/3] Complete the class Parser --- src/main/java/Main.java | 2 +- src/main/java/Parser.java | 34 +++++++++++++++++----------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 473dd4d..e292cb0 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -8,7 +8,7 @@ public class Main { 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. diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 516cf14..d74bbb8 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -15,38 +15,38 @@ public class Parser { 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; } 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. + File file = new File(filePath); + Document doc = 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". + Elements movieElements = doc.select("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 movieElement : movieElements) + { + String title = movieElement.select("h3.movie-title").toString(); + + Double rating = Double.parseDouble(movieElement.select("span.movie-rating").toString().replace("/10","")); + + int year = Integer.parseInt(movieElement.select("span.movie-year").toString()); + + Movie movie = new Movie(title, rating, year); + movies.add(movie); + } } } -- 2.54.0 From a802256569946035bf97998e97a6382806ea5c1f Mon Sep 17 00:00:00 2001 From: Bita Date: Tue, 12 May 2026 14:02:32 +0330 Subject: [PATCH 3/3] Complete class Main --- src/main/java/Main.java | 23 +++++++++++++++++++---- src/main/java/Parser.java | 8 ++++---- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index e292cb0..74f3538 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,4 +1,5 @@ import java.io.IOException; +import java.util.List; public class Main { public static void main(String[] args) { @@ -9,11 +10,25 @@ public class Main { // 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("====== Sorted by title ======"); + List sortedByTitle = parser.sortByTitle(); + for (int i = 1; i <= sortedByTitle.size(); i++) { + System.out.println(i + ". " + sortedByTitle.get(i - 1)); + } + + System.out.println("\n====== Sorted by rating ======"); + List sortedByRating = parser.sortByRating(); + for (int i = 1; i <= sortedByRating.size(); i++) { + System.out.println(i + ". " + sortedByRating.get(i - 1)); + } + + System.out.println("\n====== Sorted by year ======"); + List sortedByYear = parser.sortByYear(); + for (int i = 1; i <= sortedByYear.size(); i++) { + System.out.println(i + ". " + sortedByYear.get(i - 1)); + } - System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); + System.out.println("\nMovies parsed successfully! Total movies: " + parser.sortByTitle().size()); } catch (IOException e) { System.err.println("Error reading or parsing the HTML file: " + e.getMessage()); diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index d74bbb8..2ad5555 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -35,15 +35,15 @@ public class Parser { File file = new File(filePath); Document doc = Jsoup.parse(file, "UTF-8"); - Elements movieElements = doc.select("movie"); + Elements movieElements = doc.select("div.movie"); for (Element movieElement : movieElements) { - String title = movieElement.select("h3.movie-title").toString(); + String title = movieElement.select("h3.movie-title").text(); - Double rating = Double.parseDouble(movieElement.select("span.movie-rating").toString().replace("/10","")); + Double rating = Double.parseDouble(movieElement.select("span.movie-rating").text().replace("/10","")); - int year = Integer.parseInt(movieElement.select("span.movie-year").toString()); + int year = Integer.parseInt(movieElement.select("span.movie-year").text()); Movie movie = new Movie(title, rating, year); movies.add(movie); -- 2.54.0