diff --git a/README.md b/README.md index 31f1d61..9026c0a 100644 --- a/README.md +++ b/README.md @@ -70,11 +70,23 @@ The primary objectives of this assignment are: Utilize JSoup to parse the HTML file located in the resources folder at this path: `src/main/resources/Movies.html` and extract relevant information such as: - - `` = for movie titles + - `

` = for movie titles - `` = for movie ratings - `` = for movie release years - **Note: The span tags are nested within other tags and div closures. Look out for unneeded text like '/10' inside elements.** + **Note: The elements are nested within other tags and div closures. Look out for unneeded text like '/10' inside elements.** + + Here is a general structure of the HTML you will be working with: + + ```html +
+

The Godfather

+
+ Rating: 9.2/10
+ Year: 1972
+
+
+ ``` 5. **Implement Sorting Functionality:** @@ -89,15 +101,7 @@ The primary objectives of this assignment are: 7. **Visualize Results:** - - Clearly present the sorted movie data. - -## Bonus Objectives (For Advanced Users) 🌟 - -To enhance your project further: - -- Expand data extraction to additional websites, such as [Oscar Winning Films](https://www.scrapethissite.com/pages/ajax-javascript/). -- Develop a user interface for interactive querying and sorting. -- Utilize SQL or NoSQL databases to store the scraped data. + - Clearly present the sorted movie data using the `Main` class you implement. ## Notes 📝 @@ -106,7 +110,6 @@ Here are some important points to keep in mind: - There is a `Help` folder located at the root directory of the project which contains the sorted movies each by different priorities. It is there just so you can see what your output should look like in the end. - The unit tests are provided to assist you in understanding the project requirements. Your final grade is not solely dependent on their results; they are meant to aid your learning process. Remember to enable GitHub Actions for the test workflow to run on GitHub. - Feel free to leverage ChatGPT for learning web scraping and resolving any challenges that you may not find solutions to on the internet. Utilize its capabilities to enhance your understanding and overcome obstacles effectively. -**But it is strictly prohibited to use ChatGPT or any other AI generative model for completing any section of this assignment. Failure to comply will result in a score of 0 without any warnings.** ## Evaluation 🧐 @@ -132,10 +135,9 @@ If you have any further questions or need clarification, do not hesitate to reac ## TA Tutorial Videos 🎥 -To help you succeed in this assignment, our TA team has recorded dedicated tutorial videos covering the essential topics. Please watch these before starting: +To help you succeed in this assignment, our TA team has recorded dedicated tutorial videos covering the essential topics. Please watch these before starting. You can access the folder containing the tutorial videos here: -- [Tutorial: Basics of HTML & CSS]([INSERT_LINK_HERE]) -- [Tutorial: Web Scraping in Java using JSoup]([INSERT_LINK_HERE]) +- [TA Tutorial Videos - Movies Folder](https://drive.meshcomp.ir/d/3e149a8347a841f6bb81/) ## Additional Resources 📚 diff --git a/src/main/java/Main.java b/src/main/java/Main.java new file mode 100644 index 0000000..473dd4d --- /dev/null +++ b/src/main/java/Main.java @@ -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()); + } + } +} diff --git a/src/main/java/Movie.java b/src/main/java/Movie.java index fcdcf30..fc257fe 100644 --- a/src/main/java/Movie.java +++ b/src/main/java/Movie.java @@ -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); } -} \ No newline at end of file +} diff --git a/src/main/java/Parser.java b/src/main/java/Parser.java index f69045b..516cf14 100644 --- a/src/main/java/Parser.java +++ b/src/main/java/Parser.java @@ -7,41 +7,46 @@ import java.io.IOException; import java.util.*; public class Parser { - static List movies = new ArrayList<>(); + private List movies = new ArrayList<>(); + + public Parser(String filePath) throws IOException { + setUp(filePath); + } public List sortByTitle() { List 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 sortByRating() { List 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 sortByYear() { List 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 - } -} \ No newline at end of file +} diff --git a/src/test/java/ParserTest.java b/src/test/java/ParserTest.java index 28f4912..8b58e0a 100644 --- a/src/test/java/ParserTest.java +++ b/src/test/java/ParserTest.java @@ -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