feat : implement movie parser + sorting methods

This commit is contained in:
amirM.t
2026-05-13 18:52:09 +03:30
parent 7e0386ce16
commit 34d18f15ce
3 changed files with 112 additions and 42 deletions
+25 -10
View File
@@ -1,21 +1,36 @@
import java.io.IOException; import java.io.IOException;
public class Main { public class Main
public static void main(String[] args) { {
public static void main(String[] args)
{
// The relative path automatically points to the resources folder // The relative path automatically points to the resources folder
String filePath = "src/main/resources/Movies.html"; String filePath = "src/main/resources/Movies.html";
try { try
{
// Instantiate the parser // Instantiate the parser
Parser parser = new Parser(filePath); 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()); 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()); System.err.println("Error reading or parsing the HTML file: " + e.getMessage());
} }
} }
+12 -9
View File
@@ -1,13 +1,16 @@
import java.util.Objects; import java.util.Objects;
public class Movie { public class Movie
{
private String title; private String title;
private double rating; private double rating;
private int year; private int year;
public Movie(String title, double rating, 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. this.title = title;
this.rating = rating;
this.year = year;
} }
public String getTitle() { public String getTitle() {
@@ -23,14 +26,14 @@ public class Movie {
} }
@Override @Override
public String toString() { 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 String.format("Movie: %s, Rating: %.1f, Year: %d", title, rating, year);
return "";
} }
@Override @Override
public boolean equals(Object o) { public boolean equals(Object o)
{
if (this == o) return true; if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; if (o == null || getClass() != o.getClass()) return false;
Movie movie = (Movie) o; Movie movie = (Movie) o;
+75 -23
View File
@@ -6,47 +6,99 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
public class Parser { public class Parser
{
private List<Movie> movies = new ArrayList<>(); private List<Movie> movies = new ArrayList<>();
public Parser(String filePath) throws IOException { public Parser(String filePath) throws IOException
{
setUp(filePath); setUp(filePath);
} }
public List<Movie> sortByTitle() { public List<Movie> sortByTitle()
{
List<Movie> sortedByTitle = new ArrayList<>(movies); List<Movie> 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. for (int i = 0; i < sortedByTitle.size() - 1; i++)
// Example: Comparator.comparing(Movie::getTitle) {
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; return sortedByTitle;
} }
public List<Movie> sortByRating() { public List<Movie> sortByRating()
{
List<Movie> sortedByRating = new ArrayList<>(movies); List<Movie> 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; return sortedByRating;
} }
public List<Movie> sortByYear() { public List<Movie> sortByYear()
{
List<Movie> sortedByYear = new ArrayList<>(movies); List<Movie> 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; return sortedByYear;
} }
private void setUp(String filePath) throws IOException { 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. Elements movieElements = document.select("div.movie");
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie".
// TODO: Iterate through each movie Element. for (Element movieElement : movieElements)
// For each movie element: {
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text. String title = movieElement.select("h3.movie-title").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. String ratingText = movieElement.select("span.movie-rating").text();
// 4. Create a new Movie object with these values and add it to the 'movies' list. 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));
}
} }
} }