Skip to content
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class ListaPessoas {

public static List<Pessoa> lerCsvEConverteParaPessoas(String arquivoCSV) {
List<Pessoa> pessoasList = null;
try (BufferedReader br = new BufferedReader(new FileReader(arquivoCSV))) {
pessoasList = new ArrayList<>();
String linha;
boolean primeiraLinha = true;
while ((linha = br.readLine()) != null) {
if (primeiraLinha) {
primeiraLinha = false;
continue; // Ignora o cabeçalho
}
String[] campos = linha.split(",");
if (campos.length == 3) {
String nome = campos[0].trim();
int idade = Integer.parseInt(campos[1].trim());
String email = campos[2].trim();
Pessoa pessoa = new Pessoa(nome, idade, email);
pessoasList.add(pessoa);
}
}
} catch (IOException e) {
System.out.println("Erro ao ler o arquivo: " + e.getMessage());
}
return pessoasList;
}

public static double calcularMediaIdade(List<Pessoa> pessoaList){
return pessoas.stream()
.mapToDouble(Pessoa::getIdade)
.average()
.orElse(0.0);
}

Comment on lines +35 to +41
Copy link
Collaborator

@isdrchagas isdrchagas Nov 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aqui não está compilando porquê pessoas não existe, o nome do parametro é pessoaList

public static void main(String[] args) {
String pathArquivoCsv = "Exception Handling/ManipulacaoCSV/com.manipulacao-csv.java/dados.csv";
List<Pessoa> pessoas = PreencheLista(pathArquivoCsv);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mesma coisa aqui, como mudou o nome do metodo, o PreencheLista não existe mais

if(!pessoas.isEmpty()) {
pessoas.forEach(System.out::println);
System.out.println("Média de idade das pessoas: "+ calcularMediaIdade(pessoas));
} else {
System.out.println("Arquivo está vazio");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

public class Pessoa{
private String nome;
private int idade;
private String email;

public Pessoa(String nome, int idade, String email) {
this.nome = nome;
this.idade = idade;
this.email = email;
}

public String getNome() {
return nome;
}

public int getIdade() {
return idade;
}

public String getEmail() {
return email;
}

@Override
public java.lang.String toString() {
return "Pessoa{" +
"nome='" + nome + '\'' +
", idade=" + idade +
", email='" + email + '\'' +
'}';
}

public static void main(String[] args) {
Pessoa pessoa1 = new Pessoa("Fran", 28, "fran@email.com");

System.out.println(pessoa1);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Nome,Idade,Email
Ada Lovelace,36,ada@email.com
Grace Hopper,55,grace@email.com
Hedy Lamarr,65,hedy@email.com
Katherine Johnson,101,kath@email.com
Radia Perlman,70,radia@email.com
Shafi Goldwasser,68,shafi@email.com
Adele Goldberg,74,adele@@email.com
Rear Admiral Grace Murray Hopper,85, rear@email.com
Margaret Hamilton,85,margaret@email.com
Susan Kare,65,susan@email.com
2 changes: 1 addition & 1 deletion java-trainning.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Design Patterns/Strategy Pattern/com.strategy.java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/Design Patterns/Builder/com.builder.java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/Exception Handling/ManipulacaoCSV/com.manipulacao-csv.java" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
Expand Down