fixes + final document
This commit is contained in:
97
Main.java
97
Main.java
@@ -1,5 +1,4 @@
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
@@ -8,10 +7,15 @@ import java.util.*;
|
||||
|
||||
public class Main {
|
||||
|
||||
private static void lerDadosDeArquivo(BST arvoreBST, AVL arvoreAVL) {
|
||||
private static void lerDadosDeArquivo(Scanner entrada, BST arvoreBST, AVL arvoreAVL) {
|
||||
int incompletecount = 0;
|
||||
|
||||
try (InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("titles.csv");
|
||||
arvoreBST.cleanup();
|
||||
arvoreAVL.cleanup();
|
||||
|
||||
System.out.print("Insira o nome do arquivo CSV: ");
|
||||
String csvFileName = entrada.nextLine();
|
||||
try (InputStream inputStream = Main.class.getClassLoader().getResourceAsStream(csvFileName);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
|
||||
String line;
|
||||
br.readLine();
|
||||
@@ -53,21 +57,11 @@ public class Main {
|
||||
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.out.println("Erro ao ler o arquivo " + csvFileName);
|
||||
}
|
||||
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;
|
||||
@@ -123,7 +117,7 @@ public class Main {
|
||||
|
||||
switch (opcao) {
|
||||
case 1: // FUNCIONANDO
|
||||
lerDadosDeArquivo(arvoreBST, arvoreAVL);
|
||||
lerDadosDeArquivo(entrada, arvoreBST, arvoreAVL);
|
||||
break;
|
||||
case 2: // FUNCIONANDO
|
||||
// 1
|
||||
@@ -141,7 +135,7 @@ public class Main {
|
||||
entrada.nextLine();
|
||||
|
||||
// 3
|
||||
System.out.print("Duração média por classificação etária\n");
|
||||
System.out.print("Duração média de um episódio de uma série por classificação etária\n");
|
||||
arvoreAVL.mediaDuracaoPorClassificacao();
|
||||
|
||||
System.out.print("\nPressione ENTER para continuar...\n");
|
||||
@@ -215,10 +209,26 @@ public class Main {
|
||||
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));
|
||||
if (arvoreAVL.search(id).isEmpty() || arvoreBST.search(id).isEmpty()) {
|
||||
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));
|
||||
} else {
|
||||
System.out.println("Programa já existe em pelo menos uma árvore");
|
||||
System.out.println("Deseja sobrescrever? (S/N)");
|
||||
String resposta = sc.nextLine();
|
||||
resposta = sc.nextLine();
|
||||
|
||||
if (resposta.equalsIgnoreCase("S")) {
|
||||
arvoreAVL.remove(id);
|
||||
arvoreBST.remove(id);
|
||||
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
|
||||
@@ -227,37 +237,52 @@ public class Main {
|
||||
|
||||
// Buscar na AVL
|
||||
long startTimeAVL = System.nanoTime();
|
||||
arvoreAVL.search(programaId);
|
||||
String nomeSerieAVL = arvoreAVL.search(programaId);
|
||||
long endTimeAVL = System.nanoTime();
|
||||
long durationAVL = (endTimeAVL - startTimeAVL);
|
||||
|
||||
// Buscar na BST
|
||||
long startTimeBST = System.nanoTime();
|
||||
arvoreBST.search(programaId);
|
||||
String nomeSerieBST = 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("\nNodes tocados pela arvore AVL: " + arvoreAVL.getCount());
|
||||
System.out.println("Tempo de execução da busca na AVL: " + durationAVL + " nanossegundos");
|
||||
if (nomeSerieAVL.isEmpty()) {
|
||||
System.out.println("Não encontrado na árvore AVL!");
|
||||
} else {
|
||||
System.out.println("Encontrado na árvore AVL: " + nomeSerieAVL);
|
||||
}
|
||||
|
||||
// Exibir as estatísticas da BST
|
||||
System.out.println("Nodes tocados pela arvore BST: " + arvoreBST.getCount());
|
||||
System.out.println("\nNodes tocados pela arvore BST: " + arvoreBST.getCount());
|
||||
System.out.println("Tempo de execução da busca na BST: " + durationBST + " nanossegundos");
|
||||
|
||||
if (nomeSerieBST.isEmpty()) {
|
||||
System.out.println("Não encontrado na árvore BST!");
|
||||
} else {
|
||||
System.out.println("Encontrado na árvore BST: " + nomeSerieBST);
|
||||
}
|
||||
break;
|
||||
|
||||
case 5: // FUNCIONANDO
|
||||
case 5: // FUNCIONANDO (REMOVER DA AVL E DA BST)
|
||||
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");
|
||||
if (arvoreAVL.remove(id_remove)) {
|
||||
System.out.println("Programa removido da AVL");
|
||||
} else {
|
||||
System.out.println("Programa não encontrado na AVL, nada removido");
|
||||
}
|
||||
|
||||
// Remover da BST
|
||||
arvoreBST.remove(id_remove);
|
||||
System.out.println("Programa removido da BST");
|
||||
if (arvoreBST.remove(id_remove)) {
|
||||
System.out.println("Programa removido da BST");
|
||||
} else {
|
||||
System.out.println("Programa não encontrado na BST, nada removido");
|
||||
}
|
||||
break;
|
||||
|
||||
case 6:
|
||||
@@ -273,7 +298,17 @@ public class Main {
|
||||
case 7:
|
||||
System.out.print("Insira o nome do arquivo: ");
|
||||
String fileName = entrada.nextLine();
|
||||
treeToFile(arvoreAVL.dataToStringList(), fileName);
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(fileName)) {
|
||||
|
||||
writer.println(
|
||||
"id,title,type,description,release_year,age_certification,runtime,genres,production_countries,seasons,imdb_id,imdb_score,imdb_votes,tmdb_popularity,tmdb_score");
|
||||
arvoreAVL.saveTreeToCSV(writer);
|
||||
|
||||
} catch (Exception e) {
|
||||
// tratar erro de arquivo
|
||||
System.out.println("Erro ao salvar o arquivo " + fileName);
|
||||
}
|
||||
break;
|
||||
|
||||
case 8:
|
||||
|
||||
Reference in New Issue
Block a user