Read the end, here at the beginning is the code:
class:
package Relatorio;
public class Cliente {
private String nome;
private String sexo;
private int idade;
private double salario;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public double getSalario() {
return salario;
}
public void setSalario(double salario) {
this.salario = salario;
}
}
class:
package Relatorio;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
public class Leitor {
Charset utf8 = StandardCharsets.UTF_8;
Path path = null;
String line = null;
public ArrayList<String> ler(String url) {
path = Paths.get(url);
ArrayList<String> lines = null;
try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
lines = new ArrayList<>();
while((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
public ArrayList<Object> lerRelatorio(String url, Relatorio relatorio) {
path = Paths.get(url);
ArrayList<Object> lines = null;
try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
lines = new ArrayList<>();
while((line = reader.readLine()) != null) {
lines.add(relatorio.tipoDoRelatorio(line));
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
}
class:
package Relatorio;
import java.util.ArrayList;
import Praticando.Cliente;
public abstract class Relatorio {
public abstract Cliente tipoDoRelatorio(String line);
public abstract void apresentarRelatorio(ArrayList<Cliente> listaClientes);
}
class:
package Relatorio;
import java.util.ArrayList;
import Praticando.Cliente;
public class RelatorioBasico extends Relatorio {
Cliente cliente = null;
int aux = 0;
@Override
public Cliente tipoDoRelatorio(String line) {
if(aux == 0) {
cliente = new Cliente();
cliente.setNome(line);
}
if(aux == 1) {
cliente.setSexo(line);
}
if(aux == 2) {
cliente.setIdade(Integer.parseInt(line));
}
if(aux == 3) {
cliente.setSalario(Double.parseDouble(line));
aux = -1;
}
aux++;
return cliente;
}
@Override
public void apresentarRelatorio(ArrayList<Cliente> listaClientes) {
for(Cliente cli : listaClientes) {
System.out.println("Nome: " + cli.getNome() + ", tem " + cli.getIdade() + " anos e é do sexo " + cli.getSexo() + ".");
}
}
}
class:
package Relatorio;
public class AppTeste {
public static void main(String[] args) {
Leitor leitor = new Leitor();
RelatorioBasico relatorio = new RelatorioBasico();
relatorio.apresentarRelatorio(leitor.lerRelatorio("C:/arquivo.txt", relatorio));
}
}
error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method apresentarRelatorio(ArrayList<Cliente>) in the type RelatorioBasico is not applicable for the arguments (ArrayList<Object>)
at Relatorio.AppTeste.main(AppTeste.java:10)
seemed to be all right, even when I went to test in the AppTeste class, when I declared:
relatorio.apresentarRelatorio(leitor.lerRelatorio("C:/arquivo.txt", relatorio));
already appeared an alert saying:
The method apresentarRelatorio(ArrayList<Cliente>) in the type RelatorioBasico is not applicable for the arguments (ArrayList<Object>)
I thought it might be because of the method in the Reader class:
public ArrayList<Object> lerRelatorio(String url, Relatorio relatorio) {
path = Paths.get(url);
ArrayList<Object> lines = null;
try (BufferedReader reader = Files.newBufferedReader(path, utf8)) {
lines = new ArrayList<>();
while((line = reader.readLine()) != null) {
lines.add(relatorio.tipoDoRelatorio(line));
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
the parts:
public ArrayList<Object> lerRelatorio(String url, Relatorio relatorio) {
ArrayList<Object> lines = null;
because of the ArrayList, but even being specific as ArrayList, it keeps going wrong, I've read and reread it many times and I do not know what it can be and where I'm wrong ...
Please, if anyone realized the mistake could you tell me?
if anyone needs the source, link: https://github.com/frnndio/Praticando/tree/master/PraticandoJava/src/Relatorio