1 Commits
Author SHA1 Message Date
HesamGhazi a26f420c90 Implementation of HW5-IMDB-Scraper is complete 2026-06-20 23:54:29 +03:30
3 changed files with 107 additions and 32 deletions
+30 -2
View File
@@ -1,4 +1,13 @@
/**
*
* @author Hesam Ghazi
* Student Number: 403222015
* @version 1.0
*/
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) {
@@ -12,11 +21,30 @@ public class Main {
// TODO: You can test your code here before you run the unit tests. // TODO: You can test your code here before you run the unit tests.
// 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.
System.out.println("Movies parsed successfully! Total movies: " + parser.sortByTitle().size()); // Here I am printing lists of movies in three sorting order by rating, title and year
System.out.println("===== Sorted By Rating =====");
printMovies(parser.sortByRating());
System.out.println();
System.out.println("===== Sorted By Title =====");
printMovies(parser.sortByTitle());
System.out.println();
System.out.println("===== Sorted By Year =====");
printMovies(parser.sortByYear());
} 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());
} }
} }
private static void printMovies(List<Movie> movies) {
for (int i = 0; i < movies.size(); i++) {
System.out.println((i + 1) + ". " + movies.get(i));
}
}
} }
+22 -10
View File
@@ -1,13 +1,20 @@
/**
*
* @author Hesam Ghazi
* Student Number: 403222015
* @version 1.0
*/
import java.util.Objects; import java.util.Objects;
public class Movie { public class Movie {
private String title; private final String title;
private double rating; private final double rating;
private int year; private final 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 +31,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
@@ -34,6 +39,13 @@ public class Movie {
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;
return Double.compare(movie.rating, rating) == 0 && movie.year == year && Objects.equals(title, movie.title); return Double.compare(movie.rating, rating) == 0 &&
movie.year == year &&
Objects.equals(title, movie.title);
} }
}
@Override
public int hashCode() {
return Objects.hash(title, rating, year);
}
}
+55 -20
View File
@@ -1,13 +1,25 @@
/**
*
* @author Hesam Ghazi
* Student Number: 403222015
* @version 1.0
*/
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element; import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import org.jsoup.select.Elements;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
public class Parser { public class Parser {
private List<Movie> movies = new ArrayList<>();
private final List<Movie> movies = new ArrayList<>();
public Parser(String filePath) throws IOException { public Parser(String filePath) throws IOException {
setUp(filePath); setUp(filePath);
@@ -15,38 +27,61 @@ 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.
// Hint: You can use Collections.sort() or the List.sort() method along with a custom Comparator. sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
// 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).
// Hint: Use Double.compare() in your Comparator or Comparator.comparingDouble().reversed(). sortedByRating.sort(
Comparator.comparingDouble(Movie::getRating)
.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).
// Hint: Compare the year integers. Use Integer.compare() or Comparator.comparingInt().reversed(). sortedByYear.sort(
Comparator.comparingInt(Movie::getYear)
.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'.
// 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);
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie".
// TODO: Iterate through each movie Element. Document document = Jsoup.parse(file, "UTF-8");
// For each movie element:
// 1. Find the title element (e.g., using selector "h3.movie-title") and get its text. Elements movieElements = document.select(".movie");
// 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. for (Element movieElement : movieElements) {
// 4. Create a new Movie object with these values and add it to the 'movies' list.
String title = Objects.requireNonNull(
movieElement.selectFirst("h3.movie-title")
).text();
double rating = Double.parseDouble(
Objects.requireNonNull(
movieElement.selectFirst("span.movie-rating")
).text().replace("/10", "")
);
int year = Integer.parseInt(
Objects.requireNonNull(
movieElement.selectFirst("span.movie-year")
).text()
);
movies.add(new Movie(title, rating, year));
}
} }
} }