From ff4f6e0f839bd6830985f2d74088d393d417688d Mon Sep 17 00:00:00 2001 From: Arefe Talebi Date: Thu, 14 May 2026 20:57:03 +0330 Subject: [PATCH] HW5 --- src/main/java/Main.java | 17 ++++++++++++++--- src/main/java/Movie.java | 7 ++++--- src/main/java/Parser.java | 38 +++++++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 473dd4d..b1d0278 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -10,9 +10,20 @@ public class Main { 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("=== Sort By Title ==="); + for (Movie movie : parser.sortByTitle()) { + System.out.println(movie); + } + System.out.println(); + System.out.println("=== Sort By Rating ==="); + for (Movie movie : parser.sortByRating()) { + System.out.println(movie); + } + System.out.println(); + System.out.println("=== Sort By Year ==="); + for (Movie movie : parser.sortByYear()) { + System.out.println(movie);} + System.out.println(); System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); } catch (IOException e) { diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fc257fe..8f3783b 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -7,7 +7,9 @@ public class Movie { 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() { @@ -25,8 +27,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 diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 516cf14..af876b9 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -16,37 +16,49 @@ 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. - // 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. + File file = new File(filePath); + Document document = Jsoup.parse(file, "UTF-8"); + Elements movieElements = document.select(".movie"); + for (Element movieElement : movieElements) { + String title = movieElement + .select("h3.movie-title") + .text(); + String ratingText = movieElement + .select("span.movie-rating") + .text(); + ratingText = ratingText.replace("/10", ""); + + double rating = Double.parseDouble(ratingText); + + String yearText = movieElement + .select("span.movie-year") + .text(); + int year = Integer.parseInt(yearText); + Movie movie = new Movie(title, rating, year); + movies.add(movie); + } } -} +} \ No newline at end of file