diff --git a/pom.xml b/pom.xml
index c187af2..0c29e21 100644
--- a/pom.xml
+++ b/pom.xml
@@ -26,11 +26,17 @@
5.8.1
test
+
+
+
+
+
- org.jsoup
- jsoup
- 1.14.3
+ org.jsoup
+ jsoup
+ 1.17.2
+
diff --git a/src/main/java/Main.java b/src/main/java/Main.java
index 473dd4d..f985ac1 100644
--- a/src/main/java/Main.java
+++ b/src/main/java/Main.java
@@ -8,7 +8,18 @@ public class Main {
try {
// Instantiate the parser
Parser parser = new Parser(filePath);
-
+ System.out.println("Top 3 movies based on title: ");
+ for (int i = 0; i < 3; i ++) {
+ System.out.println(i+1 + ". "+ parser.sortByTitle().get(i));
+ }
+ System.out.println("\nTop 3 movies based on year: ");
+ for (int i = 0; i < 3; i ++) {
+ System.out.println(i+1 + ". "+ parser.sortByYear().get(i));
+ }
+ System.out.println("\nTop 3 movies based on rating: ");
+ for (int i = 0; i < 3; i ++) {
+ System.out.println(i+1 + ". "+ parser.sortByRating().get(i));
+ }
// 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/Movie.java b/src/main/java/Movie.java
index fc257fe..1bee00b 100644
--- a/src/main/java/Movie.java
+++ b/src/main/java/Movie.java
@@ -6,6 +6,9 @@ public class Movie {
private int year;
public Movie(String title, double rating, int year) {
+ this.title = title;
+ this.rating = rating;
+ this.year = year;
// TODO: Initialize the instance variables (this.title, this.rating, this.year)
// with the values passed as arguments to this constructor.
}
@@ -24,9 +27,9 @@ public class Movie {
@Override
public String toString() {
+ return "Movie: " + this.title + ", Rating: " + this.rating + ", Year: " + this.year;
// TODO: Return a string representation of the movie matching the exact required format.
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
- return "";
}
@Override
diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java
index 516cf14..aa6ddf5 100644
--- a/src/main/java/Parser.java
+++ b/src/main/java/Parser.java
@@ -15,6 +15,7 @@ public class Parser {
public List sortByTitle() {
List sortedByTitle = new ArrayList<>(movies);
+ sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
// 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)
@@ -23,6 +24,7 @@ public class Parser {
public List sortByRating() {
List sortedByRating = new ArrayList<>(movies);
+ sortedByRating.sort(Comparator.comparing(Movie::getRating).reversed());
// 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().
return sortedByRating;
@@ -30,18 +32,43 @@ public class Parser {
public List sortByYear() {
List sortedByYear = new ArrayList<>(movies);
+ sortedByYear.sort(Comparator.comparing(Movie::getYear).reversed());
// 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().
return sortedByYear;
}
private void setUp(String filePath) throws IOException {
+ File input = new File(filePath);
+ Document doc = Jsoup.parse(input, "UTF-8");
+ Elements movieElements = doc.select(".movie");
+
// 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".
+ for (Element movie: movieElements) {
+ double rating = 0;
+ int year = 0;
+ String title = "";
+ Element titleElement = movie.selectFirst("h3.movie-title");
+ if (titleElement != null) {
+ title = titleElement.text();
+ }
+ Element yearElement = movie.selectFirst("span.movie-year");
+ if (yearElement != null) {
+ year = Integer.parseInt(yearElement.text());
+ }
+ Element ratingElement = movie.selectFirst("span.movie-rating");
+ if (ratingElement != null) {
+ String ratingString = ratingElement.text().replace("/10", "");
+ rating = Double.parseDouble(ratingString);
+ }
+ Movie movieToAdd = new Movie(title, rating, year);
+ movies.add(movieToAdd);
+ }
// 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.