From 0582874d819500030fb7c5d5f84bc0336f717589 Mon Sep 17 00:00:00 2001 From: TiagoRG <35657250+TiagoRG@users.noreply.github.com> Date: Mon, 15 May 2023 21:28:07 +0100 Subject: [PATCH] [POO] aula12 ex3 added --- 1ano/2semestre/poo/src/aula12/ex3/Main.java | 54 ++++++++++++++++++++ 1ano/2semestre/poo/src/aula12/ex3/Movie.java | 8 +++ 2 files changed, 62 insertions(+) create mode 100644 1ano/2semestre/poo/src/aula12/ex3/Main.java create mode 100644 1ano/2semestre/poo/src/aula12/ex3/Movie.java diff --git a/1ano/2semestre/poo/src/aula12/ex3/Main.java b/1ano/2semestre/poo/src/aula12/ex3/Main.java new file mode 100644 index 0000000..3d473d2 --- /dev/null +++ b/1ano/2semestre/poo/src/aula12/ex3/Main.java @@ -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 movies = new ArrayList<>(); + TreeSet titles = new TreeSet<>(); + + List 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 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(); + } + }); + } +} diff --git a/1ano/2semestre/poo/src/aula12/ex3/Movie.java b/1ano/2semestre/poo/src/aula12/ex3/Movie.java new file mode 100644 index 0000000..af68069 --- /dev/null +++ b/1ano/2semestre/poo/src/aula12/ex3/Movie.java @@ -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); + } +}