From 34d18f15ce2d4242bc1d2af34524fca0281582fb Mon Sep 17 00:00:00 2001 From: "amirM.t" Date: Wed, 13 May 2026 18:52:09 +0330 Subject: [PATCH] feat : implement movie parser + sorting methods --- src/main/java/Main.java | 35 ++++++++++---- src/main/java/Movie.java | 21 +++++---- src/main/java/Parser.java | 98 ++++++++++++++++++++++++++++++--------- 3 files changed, 112 insertions(+), 42 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 473dd4d..7c96cf5 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,21 +1,36 @@ import java.io.IOException; -public class Main { - public static void main(String[] args) { +public class Main +{ + public static void main(String[] args) + { // The relative path automatically points to the resources folder String filePath = "src/main/resources/Movies.html"; - try { + 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. - System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); - - } catch (IOException e) { + + var sortedMoviesByTitle = parser.sortByTitle(); + var sortedMoviesByRating = parser.sortByRating(); + var sortedMoviesByYear = parser.sortByYear(); + + System.out.println("------- Movies sorted by title -------"); + + for (Movie movie : sortedMoviesByTitle) System.out.println(movie.toString()); + + System.out.println("------- Movies sorted by rating -------"); + + for (Movie movie : sortedMoviesByRating) System.out.println(movie.toString()); + + System.out.println("------- Movies sorted by year -------"); + + for (Movie movie : sortedMoviesByYear) System.out.println(movie.toString()); + } + catch (IOException e) + { System.err.println("Error reading or parsing the HTML file: " + e.getMessage()); } } diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fc257fe..52ce565 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -1,13 +1,16 @@ import java.util.Objects; -public class Movie { +public class Movie +{ private String title; private double rating; 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. + public Movie(String title, double rating, int year) + { + this.title = title; + this.rating = rating; + this.year = year; } public String getTitle() { @@ -23,14 +26,14 @@ 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 ""; + public String toString() + { + return String.format("Movie: %s, Rating: %.1f, Year: %d", title, rating, year); } @Override - public boolean equals(Object o) { + public boolean equals(Object o) + { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Movie movie = (Movie) o; diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index 516cf14..08919ba 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -6,47 +6,99 @@ import java.io.File; import java.io.IOException; import java.util.*; -public class Parser { +public class Parser +{ private List movies = new ArrayList<>(); - public Parser(String filePath) throws IOException { + public Parser(String filePath) throws IOException + { setUp(filePath); } - public List sortByTitle() { + 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) + + for (int i = 0; i < sortedByTitle.size() - 1; i++) + { + for (int j = 0; j < sortedByTitle.size() - i - 1; j++) + { + Movie m1 = sortedByTitle.get(j); + Movie m2 = sortedByTitle.get(j + 1); + + if (m1.getTitle().compareTo(m2.getTitle()) > 0) + { + sortedByTitle.set(j, m2); + sortedByTitle.set(j + 1, m1); + } + } + } + return sortedByTitle; } - public List sortByRating() { + 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(). + + for (int i = 0; i < sortedByRating.size() - 1; i++) + { + for (int j = 0; j < sortedByRating.size() - i - 1; j++) + { + Movie m1 = sortedByRating.get(j); + Movie m2 = sortedByRating.get(j + 1); + + if (m1.getRating() < m2.getRating()) + { + sortedByRating.set(j, m2); + sortedByRating.set(j + 1, m1); + } + } + } + return sortedByRating; } - public List sortByYear() { + 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(). + + for (int i = 0; i < sortedByYear.size() - 1; i++) + { + for (int j = 0; j < sortedByYear.size() - i - 1; j++) + { + Movie m1 = sortedByYear.get(j); + Movie m2 = sortedByYear.get(j + 1); + + if (m1.getYear() < m2.getYear()) + { + sortedByYear.set(j, m2); + sortedByYear.set(j + 1, m1); + } + } + } + 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. + private void setUp(String filePath) throws IOException + { + 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("div.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(); + double rating = Double.parseDouble(ratingText.replace("/10", "").trim()); + + String yearText = movieElement.select("span.movie-year").text(); + int year = Integer.parseInt(yearText.trim()); + + movies.add(new Movie(title, rating, year)); + } } } -- 2.54.0