[POO] aula12 ex3 added

This commit is contained in:
TiagoRG 2023-05-15 21:28:07 +01:00
parent dfb9dbc031
commit 0582874d81
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,54 @@
package aula12.ex3;
import utils.UserInput;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
List<Movie> movies = new ArrayList<>();
TreeSet<String> titles = new TreeSet<>();
List<Movie> unsortedMovies = new ArrayList<>();
Scanner reader = new Scanner(new FileReader("datafiles/aula12/movies.txt"));
reader.nextLine();
while (reader.hasNextLine()) {
String[] line = reader.nextLine().split("\t");
Movie movie = new Movie(line[0], Double.parseDouble(line[1]), line[2], line[3], Integer.parseInt(line[4]));
unsortedMovies.add(movie);
titles.add(movie.title().toLowerCase());
}
for (String title : titles)
movies.add(unsortedMovies.stream().filter(movie -> movie.title().equalsIgnoreCase(title)).findFirst().get());
System.out.println(movies);
System.out.println("\n\n\n");
System.out.println(movies.stream().sorted(Comparator.comparing(Movie::score).reversed()).toList());
System.out.println("\n\n\n");
System.out.println(movies.stream().sorted(Comparator.comparing(Movie::duration)).toList());
System.out.println("\n\n\n");
TreeMap<String, Integer> genres = new TreeMap<>();
movies.forEach(movie -> genres.put(movie.genre(), genres.getOrDefault(movie.genre(), 0) + 1));
System.out.println(genres);
System.out.println("\n\n\n");
String genre = UserInput.input(new Scanner(System.in), "Genre: ");
movies.stream().filter(movie -> movie.genre().equalsIgnoreCase(genre) && movie.score() > 60).forEach(movie -> {
try {
Files.writeString(Path.of("datafiles/aula12/myselection.txt"), movie.toString());
} catch (IOException e) {
e.printStackTrace();
}
});
}
}

View File

@ -0,0 +1,8 @@
package aula12.ex3;
public record Movie(String title, double score, String rating, String genre, int duration) {
@Override
public String toString() {
return String.format("%s\n\tScore: %.1f\n\tRating: %s\n\tGenre: %s\n\tDuration: %d\n\n", title, score, rating, genre, duration);
}
}