Merge pull request 'Complete tasks' (#1) from develop into main
Nice work! You did great, this was a bonus assignment and you pulled it off perfectly. Well done!(100/100)
This commit was merged in pull request #1.
This commit is contained in:
@@ -26,11 +26,17 @@
|
||||
<version>5.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- <dependency>-->
|
||||
<!-- <groupId>org.jsoup</groupId>-->
|
||||
<!-- <artifactId>jsoup</artifactId>-->
|
||||
<!-- <version>1.14.3</version>-->
|
||||
<!-- </dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
<version>1.14.3</version>
|
||||
<groupId>org.jsoup</groupId>
|
||||
<artifactId>jsoup</artifactId>
|
||||
<version>1.17.2</version> <!-- use latest -->
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
+12
-1
@@ -8,7 +8,18 @@ public class Main {
|
||||
try {
|
||||
// Instantiate the parser
|
||||
Parser parser = new Parser(filePath);
|
||||
|
||||
System.out.println("Top 3 movies based on title: ");
|
||||
for (int i = 0; i < 3; i ++) {
|
||||
System.out.println(i+1 + ". "+ parser.sortByTitle().get(i));
|
||||
}
|
||||
System.out.println("\nTop 3 movies based on year: ");
|
||||
for (int i = 0; i < 3; i ++) {
|
||||
System.out.println(i+1 + ". "+ parser.sortByYear().get(i));
|
||||
}
|
||||
System.out.println("\nTop 3 movies based on rating: ");
|
||||
for (int i = 0; i < 3; i ++) {
|
||||
System.out.println(i+1 + ". "+ parser.sortByRating().get(i));
|
||||
}
|
||||
// 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.
|
||||
|
||||
@@ -6,6 +6,9 @@ public class Movie {
|
||||
private int year;
|
||||
|
||||
public Movie(String title, double rating, int year) {
|
||||
this.title = title;
|
||||
this.rating = rating;
|
||||
this.year = year;
|
||||
// TODO: Initialize the instance variables (this.title, this.rating, this.year)
|
||||
// with the values passed as arguments to this constructor.
|
||||
}
|
||||
@@ -24,9 +27,9 @@ public class Movie {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Movie: " + this.title + ", Rating: " + this.rating + ", Year: " + this.year;
|
||||
// TODO: Return a string representation of the movie matching the exact required format.
|
||||
// Example format: "Movie: The Godfather, Rating: 9.2, Year: 1972"
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -15,6 +15,7 @@ public class Parser {
|
||||
|
||||
public List<Movie> sortByTitle() {
|
||||
List<Movie> sortedByTitle = new ArrayList<>(movies);
|
||||
sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
|
||||
// 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)
|
||||
@@ -23,6 +24,7 @@ public class Parser {
|
||||
|
||||
public List<Movie> sortByRating() {
|
||||
List<Movie> sortedByRating = new ArrayList<>(movies);
|
||||
sortedByRating.sort(Comparator.comparing(Movie::getRating).reversed());
|
||||
// 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;
|
||||
@@ -30,18 +32,43 @@ public class Parser {
|
||||
|
||||
public List<Movie> sortByYear() {
|
||||
List<Movie> sortedByYear = new ArrayList<>(movies);
|
||||
sortedByYear.sort(Comparator.comparing(Movie::getYear).reversed());
|
||||
// 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;
|
||||
}
|
||||
|
||||
private void setUp(String filePath) throws IOException {
|
||||
File input = new File(filePath);
|
||||
Document doc = Jsoup.parse(input, "UTF-8");
|
||||
Elements movieElements = doc.select(".movie");
|
||||
|
||||
// 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.
|
||||
// Hint: Use document.select() or document.getElementsByClass() to find all elements with the class "movie".
|
||||
|
||||
for (Element movie: movieElements) {
|
||||
double rating = 0;
|
||||
int year = 0;
|
||||
String title = "";
|
||||
Element titleElement = movie.selectFirst("h3.movie-title");
|
||||
if (titleElement != null) {
|
||||
title = titleElement.text();
|
||||
}
|
||||
Element yearElement = movie.selectFirst("span.movie-year");
|
||||
if (yearElement != null) {
|
||||
year = Integer.parseInt(yearElement.text());
|
||||
}
|
||||
Element ratingElement = movie.selectFirst("span.movie-rating");
|
||||
if (ratingElement != null) {
|
||||
String ratingString = ratingElement.text().replace("/10", "");
|
||||
rating = Double.parseDouble(ratingString);
|
||||
}
|
||||
Movie movieToAdd = new Movie(title, rating, year);
|
||||
movies.add(movieToAdd);
|
||||
}
|
||||
// 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.
|
||||
|
||||
Reference in New Issue
Block a user