Merge PR 'Do all of Home Work 5 tasks.' (#1) from develop into main

Full Mark
This commit was merged in pull request #1.
This commit is contained in:
2026-07-10 19:40:51 +00:00
3 changed files with 34 additions and 29 deletions
+16 -4
View File
@@ -9,11 +9,23 @@ 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, int i = 1;
// and print out the movies to see if they match the expected results in the Help folder. for (Movie movie : parser.sortByTitle()) {
System.out.println(i++ + ". " + movie);
}
System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); System.out.println("\n=== Sorted by Rating (Highest First) ===");
i = 1;
for (Movie movie : parser.sortByRating()) {
System.out.println(i++ + ". " + movie);
}
System.out.println("\n=== Sorted by Year (Newest First) ===");
i = 1;
for (Movie movie : parser.sortByYear()) {
System.out.println(i++ + ". " + movie);
}
} 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 -5
View File
@@ -6,8 +6,9 @@ public class Movie {
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) this.title = title;
// with the values passed as arguments to this constructor. this.rating = rating;
this.year = year;
} }
public String getTitle() { public String getTitle() {
@@ -24,9 +25,7 @@ public class Movie {
@Override @Override
public String toString() { public String toString() {
// TODO: Return a string representation of the movie matching the exact required format. return "Movie: " + title + ", Rating: " + rating + ", Year: " + year;
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
return "";
} }
@Override @Override
+13 -19
View File
@@ -15,38 +15,32 @@ public class Parser {
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. sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
// Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator.
// Example: Comparator.comparing(Movie::getTitle)
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). sortedByRating.sort(Comparator.comparingDouble(Movie::getRating).reversed());
// Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed().
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). sortedByYear.sort(Comparator.comparingInt(Movie::getYear).reversed());
// Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed().
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'. File file = new File(filePath);
// TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object. Document document = Jsoup.parse(file, "UTF-8");
Elements movieElements = document.select(".movie");
// TODO: Extract the list of movie elements from the Document. for (Element movieElement : movieElements) {
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie". String title = movieElement.select("h3.movie-title").text();
String ratingText = movieElement.select("span.movie-rating").text().replace("/10", "");
// TODO: Iterate through each movie Element. double rating = Double.parseDouble(ratingText);
// For each movie element: int year = Integer.parseInt(movieElement.select("span.movie-year").text());
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text. movies.add(new Movie(title, rating, year));
// 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.
} }
} }