This commit is contained in:
2026-05-09 03:50:38 +03:30
parent 7ffa76fd45
commit 0aa0a60e74
5 changed files with 70 additions and 39 deletions
+22
View File
@@ -0,0 +1,22 @@
import java.io.IOException;
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 {
// 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) {
System.err.println("Error reading or parsing the HTML file: " + e.getMessage());
}
}
}
+5 -3
View File
@@ -6,7 +6,8 @@ public class Movie {
private int year;
public Movie(String title, double rating, int year) {
//TODO
// TODO: Initialize the instance variables (this.title, this.rating, this.year)
// with the values passed as arguments to this constructor.
}
public String getTitle() {
@@ -23,7 +24,8 @@ public class Movie {
@Override
public String toString() {
//TODO
// TODO: Return a string representation of the movie matching the exact required format.
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
return "";
}
@@ -34,4 +36,4 @@ public class Movie {
Movie movie = (Movie) o;
return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title);
}
}
}
+24 -19
View File
@@ -7,41 +7,46 @@ import java.io.IOException;
import java.util.*;
public class Parser {
static List<Movie> movies = new ArrayList<>();
private List<Movie> movies = new ArrayList<>();
public Parser(String filePath) throws IOException {
setUp(filePath);
}
public List<Movie> sortByTitle() {
List<Movie> sortedByTitle = new ArrayList<>(movies);
// Sort movies alphabetically by title
//TODO
// 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)
return sortedByTitle;
}
public List<Movie> sortByRating() {
List<Movie> sortedByRating = new ArrayList<>(movies);
// Sort movies by rating (highest to lowest)
//TODO
// 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;
}
public List<Movie> sortByYear() {
List<Movie> sortedByYear = new ArrayList<>(movies);
// Sort movies by year (newest to oldest)
//TODO
// 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;
}
public void setUp() throws IOException {
// Parse the HTML file using Jsoup
//TODO
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.
// Extract data from the HTML
//TODO
// 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".
// Iterate through each movie div to extract movie data
//TODO
// 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.
}
public static void main(String[] args) {
// You can test your code here before you run the unit tests
}
}
}
+2 -2
View File
@@ -12,8 +12,8 @@ public class ParserTest {
@BeforeAll
static void setUp() throws IOException {
handle = new Parser();
handle.setUp();
String filePath = "src/main/resources/Movies.html";
handle = new Parser(filePath);
}
@Test