Complete class Main

This commit is contained in:
2026-05-12 14:02:32 +03:30
parent c91e72a304
commit a802256569
2 changed files with 23 additions and 8 deletions
+19 -4
View File
@@ -1,4 +1,5 @@
import java.io.IOException; import java.io.IOException;
import java.util.List;
public class Main { public class Main {
public static void main(String[] args) { public static void main(String[] args) {
@@ -9,11 +10,25 @@ public class Main {
// 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. System.out.println("====== Sorted by title ======");
// Example: Call your sorting methods, iterate through the returned list, List<Movie> sortedByTitle = parser.sortByTitle();
// and print out the movies to see if they match the expected results in the Help folder. for (int i = 1; i <= sortedByTitle.size(); i++) {
System.out.println(i + ". " + sortedByTitle.get(i - 1));
}
System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); System.out.println("\n====== Sorted by rating ======");
List<Movie> sortedByRating = parser.sortByRating();
for (int i = 1; i <= sortedByRating.size(); i++) {
System.out.println(i + ". " + sortedByRating.get(i - 1));
}
System.out.println("\n====== Sorted by year ======");
List<Movie> sortedByYear = parser.sortByYear();
for (int i = 1; i <= sortedByYear.size(); i++) {
System.out.println(i + ". " + sortedByYear.get(i - 1));
}
System.out.println("\nMovies parsed successfully! Total movies: " + parser.sortByTitle().size());
} catch (IOException e) { } 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());
+4 -4
View File
@@ -35,15 +35,15 @@ public class Parser {
File file = new File(filePath); File file = new File(filePath);
Document doc = Jsoup.parse(file, "UTF-8"); Document doc = Jsoup.parse(file, "UTF-8");
Elements movieElements = doc.select("movie"); Elements movieElements = doc.select("div.movie");
for (Element movieElement : movieElements) for (Element movieElement : movieElements)
{ {
String title = movieElement.select("h3.movie-title").toString(); String title = movieElement.select("h3.movie-title").text();
Double rating = Double.parseDouble(movieElement.select("span.movie-rating").toString().replace("/10","")); Double rating = Double.parseDouble(movieElement.select("span.movie-rating").text().replace("/10",""));
int year = Integer.parseInt(movieElement.select("span.movie-year").toString()); int year = Integer.parseInt(movieElement.select("span.movie-year").text());
Movie movie = new Movie(title, rating, year); Movie movie = new Movie(title, rating, year);
movies.add(movie); movies.add(movie);