290 lines
11 KiB
Java
290 lines
11 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.PrintWriter;
|
|
import java.util.*;
|
|
|
|
public class Main {
|
|
|
|
private static void lerDadosDeArquivo(BST arvoreBST, AVL arvoreAVL) {
|
|
int incompletecount = 0;
|
|
|
|
try (InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("titles.csv");
|
|
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
String line;
|
|
br.readLine();
|
|
|
|
while ((line = br.readLine()) != null) {
|
|
String[] fields = splitCSVLine(line);
|
|
|
|
boolean empty = false;
|
|
for (String field : fields) {
|
|
if (field.isEmpty()) {
|
|
empty = true;
|
|
}
|
|
}
|
|
if (empty) {
|
|
incompletecount++;
|
|
} else if (fields.length == 15) {
|
|
ProgramaNetflix programa = new ProgramaNetflix(
|
|
fields[0], // id
|
|
fields[1], // título
|
|
fields[2], // showType
|
|
fields[3], // descrição
|
|
Integer.parseInt(fields[4]), // releaseYear
|
|
fields[5], // ageCertification
|
|
Integer.parseInt(fields[6]), // runtime
|
|
fields[7], // generos
|
|
fields[8], // productionCountries
|
|
Float.parseFloat(fields[9]), // temporadas
|
|
fields[10], // imdbId
|
|
Float.parseFloat(fields[11]), // imdbScore
|
|
Float.parseFloat(fields[12]), // imdbVotes
|
|
Float.parseFloat(fields[13]), // tmdbPopularity
|
|
Float.parseFloat(fields[14]) // tmdbScore
|
|
);
|
|
|
|
// Inserir o objeto nas árvores BST e AVL
|
|
arvoreBST.insert(programa);
|
|
arvoreAVL.insert(programa);
|
|
}
|
|
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
System.out.println(incompletecount + " linhas ignoradas devido à falta de informações.");
|
|
}
|
|
|
|
private static void treeToFile(List<String> data, String fileName) {
|
|
try (PrintWriter writer = new PrintWriter(fileName)) {
|
|
for (String line : data) {
|
|
writer.println(line);
|
|
}
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private static String[] splitCSVLine(String line) {
|
|
List<String> fields = new ArrayList<>();
|
|
boolean aspas = false;
|
|
StringBuilder atual = new StringBuilder();
|
|
for (char c : line.toCharArray()) {
|
|
if (c == '"') {
|
|
aspas = !aspas;
|
|
} else if (c == ',' && !aspas) {
|
|
fields.add(atual.toString().trim());
|
|
atual.setLength(0);
|
|
} else {
|
|
atual.append(c);
|
|
}
|
|
}
|
|
fields.add(atual.toString().trim());
|
|
return fields.toArray(new String[0]);
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
BST arvoreBST = new BST();
|
|
AVL arvoreAVL = new AVL();
|
|
int opcao;
|
|
|
|
String id = null;
|
|
String titulo;
|
|
String show_type;
|
|
String descricao;
|
|
int release_year;
|
|
String age_certification;
|
|
int runtime;
|
|
String generos;
|
|
String production_countries;
|
|
float temporadas;
|
|
String imdb_id;
|
|
double imdb_score;
|
|
float imdb_votes;
|
|
double tmdb_popularity;
|
|
double tmdb_score;
|
|
Scanner entrada = new Scanner(System.in);
|
|
|
|
do {
|
|
System.out.println("\nMenu:");
|
|
System.out.println("1 - Ler dados do arquivo");
|
|
System.out.println("2 - Análise de dados");
|
|
System.out.println("3 - Inserir Programa");
|
|
System.out.println("4 - Buscar Programa");
|
|
System.out.println("5 - Remover Programa");
|
|
System.out.println("6 - Exibir a altura das árvores");
|
|
System.out.println("7 - Salvar dados em arquivo");
|
|
System.out.println("8 - Sair");
|
|
opcao = entrada.nextInt();
|
|
entrada.nextLine();
|
|
|
|
switch (opcao) {
|
|
case 1: // FUNCIONANDO
|
|
lerDadosDeArquivo(arvoreBST, arvoreAVL);
|
|
break;
|
|
case 2: // FUNCIONANDO
|
|
// 1
|
|
System.out.print("10 países com a melhor pontuação no IMDB\n");
|
|
arvoreAVL.pontuacaoMediaPorPais();
|
|
|
|
System.out.print("\nPressione ENTER para continuar...\n");
|
|
entrada.nextLine();
|
|
|
|
// 2
|
|
System.out.print("Quantidade de filmes por década\n");
|
|
arvoreAVL.filmesPorDecada();
|
|
|
|
System.out.print("\nPressione ENTER para continuar...\n");
|
|
entrada.nextLine();
|
|
|
|
// 3
|
|
System.out.print("Duração média por classificação etária\n");
|
|
arvoreAVL.mediaDuracaoPorClassificacao();
|
|
|
|
System.out.print("\nPressione ENTER para continuar...\n");
|
|
entrada.nextLine();
|
|
|
|
// 4
|
|
System.out.print("Percentual de títulos por classificação etária\n");
|
|
arvoreAVL.percentualPorClassificacaoEtaria();
|
|
|
|
System.out.print("\nPressione ENTER para continuar...\n");
|
|
entrada.nextLine();
|
|
|
|
// 5
|
|
System.out.print("Países com mais títulos\n");
|
|
arvoreAVL.paisesComMaisTitulos();
|
|
break;
|
|
|
|
case 3: // FUNCIONANDO
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
System.out.println("Insira o ID do título");
|
|
id = sc.nextLine();
|
|
|
|
System.out.println("Insira o título do título");
|
|
titulo = sc.nextLine();
|
|
|
|
System.out.println("Insira o tipo (SHOW | MOVIE)");
|
|
show_type = sc.nextLine();
|
|
|
|
System.out.println("Insira a descrição do título");
|
|
descricao = sc.nextLine();
|
|
|
|
System.out.println("Insira o ano de lançamento do título");
|
|
release_year = sc.nextInt();
|
|
|
|
System.out.println("Insira a certificação parental do título");
|
|
age_certification = sc.nextLine();
|
|
age_certification = sc.nextLine();
|
|
|
|
System.out.println("Insira o tempo (em minutos) do título");
|
|
runtime = sc.nextInt();
|
|
|
|
System.out.println("Insira os gêneros do título");
|
|
generos = sc.nextLine();
|
|
generos = sc.nextLine();
|
|
|
|
System.out.println(
|
|
"Insira os países de produção do título (No formato ['Country Code', 'Country Code', 'Country Code'] )");
|
|
production_countries = sc.nextLine();
|
|
|
|
System.out.println("Insira a quantidade de temporadas do título");
|
|
temporadas = sc.nextFloat();
|
|
|
|
System.out.println("Insira o imdb id do título");
|
|
imdb_id = sc.nextLine();
|
|
imdb_id = sc.nextLine();
|
|
|
|
System.out.println("Insira o imdb score do título");
|
|
imdb_score = sc.nextDouble();
|
|
|
|
System.out.println("Insira o imdb votes do título");
|
|
imdb_votes = sc.nextFloat();
|
|
|
|
System.out.println("Insira o tmdb popularity do título");
|
|
tmdb_popularity = sc.nextDouble();
|
|
|
|
System.out.println("Insira o tmdb_score do título");
|
|
tmdb_score = sc.nextDouble();
|
|
|
|
ProgramaNetflix programa = new ProgramaNetflix(id, titulo, show_type, descricao, release_year,
|
|
age_certification, runtime, generos, production_countries, temporadas, imdb_id, imdb_score,
|
|
imdb_votes, tmdb_popularity, tmdb_score);
|
|
|
|
arvoreAVL.insert(programa);
|
|
arvoreBST.insert(programa);
|
|
System.out.println("Valor na Árvore AVL: " + arvoreAVL.search(id));
|
|
System.out.println("Valor na Árvore BST: " + arvoreBST.search(id));
|
|
break;
|
|
|
|
case 4: // FUNCIONANDO
|
|
System.out.print("Informe o ID do programa: ");
|
|
String programaId = entrada.nextLine().trim().toLowerCase();
|
|
|
|
// Buscar na AVL
|
|
long startTimeAVL = System.nanoTime();
|
|
arvoreAVL.search(programaId);
|
|
long endTimeAVL = System.nanoTime();
|
|
long durationAVL = (endTimeAVL - startTimeAVL);
|
|
|
|
// Buscar na BST
|
|
long startTimeBST = System.nanoTime();
|
|
arvoreBST.search(programaId);
|
|
long endTimeBST = System.nanoTime();
|
|
long durationBST = (endTimeBST - startTimeBST);
|
|
|
|
// Exibir as estatísticas da AVL
|
|
System.out.println("Nodes tocados pela arvore AVL: " + arvoreAVL.getCount());
|
|
System.out.println("Tempo de execução da busca na AVL: " + durationAVL + " nanossegundos");
|
|
|
|
// Exibir as estatísticas da BST
|
|
System.out.println("Nodes tocados pela arvore BST: " + arvoreBST.getCount());
|
|
System.out.println("Tempo de execução da busca na BST: " + durationBST + " nanossegundos");
|
|
|
|
break;
|
|
|
|
case 5: // FUNCIONANDO
|
|
System.out.print("Informe o ID do programa a ser removido: ");
|
|
String id_remove = entrada.nextLine().trim().toLowerCase();
|
|
|
|
// Remover da AVL
|
|
arvoreAVL.remove(id_remove);
|
|
System.out.println("Programa removido da AVL");
|
|
|
|
// Remover da BST
|
|
arvoreBST.remove(id_remove);
|
|
System.out.println("Programa removido da BST");
|
|
break;
|
|
|
|
case 6:
|
|
// Exibir a altura da árvore AVL
|
|
int alturaAVL = arvoreAVL.height();
|
|
System.out.println("Altura da Árvore AVL: " + alturaAVL);
|
|
|
|
// Exibir a altura da árvore BST
|
|
int alturaBST = arvoreBST.height();
|
|
System.out.println("Altura da Árvore BST: " + alturaBST);
|
|
break;
|
|
|
|
case 7:
|
|
System.out.print("Insira o nome do arquivo: ");
|
|
String fileName = entrada.nextLine();
|
|
treeToFile(arvoreAVL.dataToStringList(), fileName);
|
|
break;
|
|
|
|
case 8:
|
|
System.out.println("Saindo...\n");
|
|
break;
|
|
|
|
default:
|
|
System.out.println("Opção inválida");
|
|
}
|
|
|
|
} while (opcao != 8);
|
|
entrada.close();
|
|
}
|
|
} |