diff --git a/Help/SortedByRating.txt b/Help/SortedByRating.txt index a26369a..e69de29 100644 --- a/Help/SortedByRating.txt +++ b/Help/SortedByRating.txt @@ -1,15 +0,0 @@ -1. Movie: The Shawshank Redemption, Rating: 9.3, Year: 1994 -2. Movie: The Godfather, Rating: 9.2, Year: 1972 -3. Movie: The Dark Knight, Rating: 9.0, Year: 2008 -4. Movie: 12 Angry Men, Rating: 9.0, Year: 1957 -5. Movie: Schindler's List, Rating: 9.0, Year: 1993 -6. Movie: The Lord of the Rings: The Return of the King, Rating: 9.0, Year: 2003 -7. Movie: Pulp Fiction, Rating: 8.9, Year: 1994 -8. Movie: The Lord of the Rings: The Fellowship of the Ring, Rating: 8.8, Year: 2001 -9. Movie: Forrest Gump, Rating: 8.8, Year: 1994 -10. Movie: Fight Club, Rating: 8.8, Year: 1999 -11. Movie: Inception, Rating: 8.8, Year: 2010 -12. Movie: Star Wars: Episode V - The Empire Strikes Back, Rating: 8.7, Year: 1980 -13. Movie: The Matrix, Rating: 8.7, Year: 1999 -14. Movie: Goodfellas, Rating: 8.7, Year: 1990 -15. Movie: Interstellar, Rating: 8.6, Year: 2014 \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 473dd4d..98f3bb2 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -12,6 +12,20 @@ public class Main { // 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 ==="); + for (Movie movie : parser.sortByTitle()) { + System.out.println(movie); + } + + System.out.println("\n=== Sorted By Rating ==="); + for (Movie movie : parser.sortByRating()) { + System.out.println(movie); + } + + System.out.println("\n=== Sorted By Year ==="); + for (Movie movie : parser.sortByYear()) { + System.out.println(movie); + } System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fc257fe..97817d3 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 "return \"Movie: \" + title + \", Rating: \" + rating + \", Year: \" + year;"; } @Override diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 516cf14..46806f8 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -18,6 +18,7 @@ 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 +26,9 @@ 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,21 +36,46 @@ 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.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 document = 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 = document.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") + .text(); + + String ratingText = movieElement + .select("span.movie-rating") + .text() + .replace("/10", ""); + + double rating = Double.parseDouble(ratingText); + + int year = Integer.parseInt( + movieElement + .select("span.movie-year") + .text() + ); + + movies.add(new Movie(title, rating, year)); + } } }