1 Commits
Author SHA1 Message Date
Arshida_0 ba4c3188c0 HW 05 2026-05-15 16:21:25 +04:30
3 changed files with 88 additions and 8 deletions
+28
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) {
@@ -13,6 +14,33 @@ public class Main {
// Example: Call your sorting methods, iterate through the returned list, // 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. // and print out the movies to see if they match the expected results in the Help folder.
List<Movie> sortedByTitle = parser.sortByTitle();
int c1 = 0;
System.out.println("Sorted by title: ");
for (Movie m : sortedByTitle) {
c1++;
System.out.println(c1 + ". " + m.toString());
}
System.out.println("__________");
List<Movie> sortedByRating = parser.sortByRating();
int c2 = 0;
System.out.println("Sorted by rating: ");
for (Movie m : sortedByRating) {
c2++;
System.out.println(c2 + ". " + m.toString());
}
System.out.println("__________");
List<Movie> sortedByYear = parser.sortByYear();
int c3 = 0;
System.out.println("Sorted by year: ");
for (Movie m : sortedByYear) {
c3++;
System.out.println(c3 + ". " + m.toString());
}
System.out.println("__________");
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) { } catch (IOException e) {
+8 -3
View File
@@ -6,8 +6,12 @@ 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) // TODO: Initialize the instance variables (this.title, this.rating, this.year). .
// with the values passed as arguments to this constructor. // with the values passed as arguments to this constructor.
this.title = title;
this.rating = rating;
this.year =year;
} }
public String getTitle() { public String getTitle() {
@@ -24,9 +28,10 @@ public class Movie {
@Override @Override
public String toString() { public String toString() {
// TODO: Return a string representation of the movie matching the exact required format. // TODO: Return a string representation of the movie matching the exact required format. .
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972" // Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
return "";
return String.format("Movie: %s, Rating: %.1f, Year: %d", title, rating, year);
} }
@Override @Override
+51 -4
View File
@@ -18,6 +18,12 @@ public class Parser {
// TODO: Sort the 'sortedByTitle' list alphabetically by the movie's title. // 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. // Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator.
// Example: Comparator.comparing(Movie::getTitle) // Example: Comparator.comparing(Movie::getTitle)
Comparator<Movie> comparator = Comparator
.comparing(Movie::getTitle);
sortedByTitle.sort(comparator);
return sortedByTitle; return sortedByTitle;
} }
@@ -25,6 +31,12 @@ public class Parser {
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). // 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(). // Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed().
Comparator<Movie> comparator = Comparator
.comparing(Movie::getRating).reversed();
sortedByRating.sort(comparator);
return sortedByRating; return sortedByRating;
} }
@@ -32,21 +44,56 @@ public class Parser {
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). // 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(). // Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed().
Comparator<Movie> comparator = Comparator
.comparing(Movie::getYear).reversed();
sortedByYear.sort(comparator);
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: 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. // TODO: Parse the HTML file using Jsoup.parse(file, "UTF-8"). This returns a Document object. .
// TODO: Extract the list of movie elements from the Document. File file = new File(filePath);
Document doc = 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". // Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie".
// TODO: Iterate through each movie Element. Elements moviesLst = doc.getElementsByClass("col-md-4 movie");
// TODO: Iterate through each movie Element. .
// For each movie element: // For each movie element:
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text. // 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. // 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. // 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. // 4. Create a new Movie object with these values and add it to the 'movies' list.
for (Element element : moviesLst) {
Element movieTitle = element.select("h3.movie-title").first();
Element movieRating = element.select("span.movie-rating").first();
Element movieYear = element.select("span.movie-year").first();
String title = "";
double rating = 0.0;
int year = 0;
if (movieTitle != null) {
title = movieTitle.ownText();
}
if (movieRating != null) {
String ratingOutOfTen = movieRating.ownText();
String ratingStr = ratingOutOfTen.substring(0, 3);
rating = Double.parseDouble(ratingStr);
}
if (movieYear != null) {
String yearStr = movieYear.ownText();
year = Integer.valueOf(yearStr);
}
Movie movie = new Movie(title, rating, year);
movies.add(movie);
}
} }
} }