diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fc257fe..f44a8ef 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -8,6 +8,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() { @@ -26,7 +29,7 @@ public class Movie { 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..33d3379 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -1,7 +1,8 @@ -import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import org.jsoup.Jsoup; + import java.io.File; import java.io.IOException; import java.util.*; @@ -18,6 +19,8 @@ public class Parser { // 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; } @@ -25,6 +28,7 @@ public class Parser { 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; } @@ -32,15 +36,19 @@ public class Parser { 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.comparingDouble(Movie::getYear).reversed()); return sortedByYear; } private void setUp(String filePath) throws IOException { // TODO: Create a java.io.File object using the given 'filePath'. + File in=new File(filePath); // TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object. + Document doc = Jsoup.parse(in, "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 elements= doc.getElementsByClass("movie"); // TODO: Iterate through each movie Element. // For each movie element: @@ -48,5 +56,15 @@ public class Parser { // 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 element : elements) { + String title = element.select("h3.movie-title").text(); + String rating = element.select("span.movie-rating").text(); + rating = rating.replace("/10", ""); + double numOfRating = Double.parseDouble(rating); + String year = element.select("span.movie-year").text(); + int numOfYear = Integer.parseInt(year); + + movies.add(new Movie(title, numOfRating, numOfYear)); + } } }