0
I created a program for the following exercise:
Exercise: Do a program to register customers from an accountant’s company. It is important to store customer information such as name, address, phone number and email.
In addition, it should be noted that the accountant meets both individuals and legal entities, which differ by the fact that the first has CPF and the second has a CNPJ. The program must register customers indefinitely until the user no longer wishes.
At the time of registration, the program should ask the user what type of client wants to register, after that the program should display a report with all registered clients as follows:
type: physics
name:
Cpf:
The code is as follows:
public class Cliente{
private String nome;
private String endereco;
private String telefone;
private String email;
public Cliente(String nome, String endereco, String telefone, String email){
this.nome=nome;
this.endereco=endereco;
this.telefone=telefone;
this.email=email;
}
public String getNome(){
return nome;
}
public String getEndereco(){
return endereco;
}
public String getTelefone(){
return telefone;
}
public String getEmail(){
return email;
}
}
public class PessoaFisica extends Cliente{
private String cpf;
public PessoaFisica(String nome, String endereco, String telefone, String email, String cpf){
super(nome, endereco, telefone, email);
this.cpf=cpf;
}
public String getCpf(){
return cpf;
}
public String toString(){
return "tipo: fisica";
}
}
public class PessoaJuridica extends Cliente{
private String cnpj;
public PessoaJuridica(String nome, String endereco, String telefone, String email, String cnpj){
super(nome, endereco, telefone, email);
this.cnpj=cnpj;
}
public String getCnpj(){
return cnpj;
}
public String toString(){
return "tipo: juridica";
}
}
import java.util.*;
public class MainContador{
public static void main(String args[]){
int op=1;
int tipo;
List<Cliente> listaClientes = new ArrayList<>();
Scanner dado = new Scanner(System.in);
do{
System.out.println("Opcoes de cadastro:\n(1)pessoa fisica.\n(2)pessoa juridica");
tipo = dado.nextInt();
dado = new Scanner(System.in);
if(tipo==1){
System.out.println("Nome:");
String nome = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Endereco:");
String endereco = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Telefone:");
String telefone = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Email:");
String email = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Cpf:");
String cpf = dado.nextLine();
dado = new Scanner(System.in);
PessoaFisica pf = new PessoaFisica(nome, endereco, telefone, email, cpf);
listaClientes.add(pf);
}
else if(tipo==2){
System.out.println("Nome:");
String nome = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Endereco:");
String endereco = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Telefone:");
String telefone = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Email:");
String email = dado.nextLine();
dado = new Scanner(System.in);
System.out.println("Cnpj:");
String cnpj = dado.nextLine();
dado = new Scanner(System.in);
PessoaJuridica pj = new PessoaJuridica(nome, endereco, telefone, email, cnpj);
listaClientes.add(pj);
}
System.out.println("Para cadastrar outro cliente, digite 1, para sair digite outro numero qualquer.");
op = dado.nextInt();
dado = new Scanner(System.in);
}while(op==1);
for(int i=0; i<listaClientes.size(); i++){
Cliente c = listaClientes.get(i);
if(c.toString()=="tipo: física"){
System.out.println(c.toString());
System.out.println("\nNome:" + c.getNome());
System.out.println("\nEndereco:" + c.getEndereco());
System.out.println("\nTelefone:"+c.getTelefone());
System.out.println("\nEmail:"+c.getEmail());
//System.out.println("\nCPF:"+c.getCpf());
System.out.println("\n___________________\n");
}
else{
System.out.println(c.toString());
System.out.println("\nNome:" + c.getNome());
System.out.println("\nEndereco:" + c.getEndereco());
System.out.println("\nTelefone:"+c.getTelefone());
System.out.println("\nEmail:"+c.getEmail());
//System.out.println("\nCNPJ:"+c.getCnpj());
System.out.println("\n___________________\n");
}
}
}
}
Commenting on the lines of c.getCnpj()
and c.getCpf()
the program runs normally, but then I cannot display these values of my object. The question is, when compiling, the method is not found in the objects. Why the method getString()
is found and the getCpf()
or getCnpj()
No? What’s the problem?
Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).
– Maniero