58 lines
1.7 KiB
Java
58 lines
1.7 KiB
Java
import org.jsoup.Jsoup;
|
|
import org.jsoup.nodes.Document;
|
|
import org.jsoup.nodes.Element;
|
|
import org.jsoup.select.Elements;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.*;
|
|
|
|
public class Parser {
|
|
private List<Movie> movies = new ArrayList<>();
|
|
|
|
public Parser(String filePath) throws IOException {
|
|
setUp(filePath);
|
|
}
|
|
|
|
public List<Movie> sortByTitle() {
|
|
List<Movie> sortedByTitle = new ArrayList<>(movies);
|
|
sortedByTitle.sort(Comparator.comparing(Movie::getTitle));
|
|
return sortedByTitle;
|
|
}
|
|
|
|
public List<Movie> sortByRating() {
|
|
List<Movie> sortedByRating = new ArrayList<>(movies);
|
|
sortedByRating.sort(Comparator.comparingDouble(Movie::getRating).reversed());
|
|
return sortedByRating;
|
|
}
|
|
|
|
public List<Movie> sortByYear() {
|
|
List<Movie> sortedByYear = new ArrayList<>(movies);
|
|
sortedByYear.sort(Comparator.comparingInt(Movie::getYear).reversed());
|
|
return sortedByYear;
|
|
}
|
|
|
|
private void setUp(String filePath) throws IOException {
|
|
|
|
File file = new File(filePath);
|
|
|
|
Document document = Jsoup.parse(file, "UTF-8");
|
|
|
|
Elements movieElements = document.select("div.movie");
|
|
|
|
for (Element movieElement : movieElements) {
|
|
|
|
String title = movieElement.select("h3.movie-title").text();
|
|
|
|
String ratingText = movieElement.select("span.movie-rating").text();
|
|
ratingText = ratingText.replace("/10", "");
|
|
double rating = Double.parseDouble(ratingText);
|
|
|
|
String yearText = movieElement.select("span.movie-year").text();
|
|
int year = Integer.parseInt(yearText);
|
|
|
|
Movie movie = new Movie(title, rating, year);
|
|
|
|
movies.add(movie);
|
|
}
|
|
}
|
|
} |