Boa tarde,
Estou com uma duvida a respeito da herança, tenho 4 classes , sendo uma classe pai, uma classe filha, uma classe validadora de cpf e uma classe principal pra manipular os dados. O problema é que a classe filha não está mostrando o cpf quando uso o metodo toString.
Alguem poderia me auxiliar?
public class Cpf {
private String cpf;
private int d1, d2;
public void setCpf(String cpf) {
this.cpf = cpf;
}
public void setD1(int d1) {
this.d1 = d1;
}
public void setD2(int d2) {
this.d2 = d2;
}
public int getD1() {
return this.d1;
}
public int getD2() {
return this.d2;
}
public String getCpf() {
return this.cpf;
}
// FAZ O CÁLCULO DO 1º DÍGITO VERIFICADOR
public int dV1(String cpf) {
int v[] = new int[11];
int soma = 0, sequencia = 10, resto;
for (int i = 0; i < 9; i++) {
v[i] = Integer.parseInt(cpf.substring(i, i + 1));
soma = soma + (v[i] * sequencia);
sequencia--;
}
resto = soma % 11;
if (resto == 0 || resto == 1) {
return 0;
} else {
return (11 - resto);
}
}
/**
* FAZ O CÁLCULO DO 2º DÍGITO VERIFICADOR A ÚNICA DIFERENÇA É QUE A
* MULTIPLICAÇÃO DA SEQUÊNCIA É FEITA A PARTIR DO SEGUNDO ALGARISMO DO CPF
* ATÉ O 1º DÍGITO VERIFICADOR
*/
public int dV2(String cpf) {
int v[] = new int[11];
int soma = 0, sequencia = 10, resto;
for (int i = 1; i < 10; i++) {
v[i] = Integer.parseInt(cpf.substring(i, i + 1));
soma = soma + (v[i] * sequencia);
sequencia--;
}
resto = soma % 11;
if (resto == 0 || resto == 1) {
return 0;
} else {
return (11 - resto);
}
}
public boolean validaCpf(){
d1 = Integer.parseInt(cpf.substring(9,10));
d2 = Integer.parseInt(cpf.substring(10,11));
if (d1 == dV1(cpf) && d2 == dV2(cpf)){
System.out.println("Cpf é Valido");
return true;
}else
System.out.println("Cpf é Invalido");
return false;
}
}
public class PessoaFisica extends Pessoa{
protected Cpf cpf;
protected char sexo;
public Cpf getCpf() {
return cpf;
}
public void setCpf(Cpf cpf) {
this.cpf = cpf;
}
public char getSexo() {
return sexo;
}
public void setSexo(char sexo) {
this.sexo = sexo;
}
@Override
public String toString() {
return "PessoaFisica [cpf=" + cpf + ", sexo=" + sexo + ", telefone="
+ telefone + ", nome=" +nome + ", endereco=" +endereco+ "]";
}
}
//ESTA É A CLASSE PAI.
public class Pessoa {
protected String nome;
protected String endereco;
protected String telefone;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
}
import java.util.Scanner;
public class Principal {
/**
* @param args
*/
public static Scanner entrada = new Scanner (System.in);
public static void main(String[] args) {
int opcao = menu ();
PessoaFisica p = new PessoaFisica ();
Cpf cpf = new Cpf ();
while (opcao != 4){
switch (opcao){
case 1:
System.out.println("======== Cadastrar Pessoa Fisica ========");
System.out.print("Digite o nome: ");
p.setNome(entrada.next());
System.out.print("Digite o endereco: ");
p.setEndereco(entrada.next());
System.out.print("Digite o telefone: ");
p.setTelefone(entrada.next());
do {
System.out.print("Digite o cpf: ");
cpf.setCpf(entrada.next());
} while (cpf.validaCpf() == false);
cpf.validaCpf();
break;
case 2:
System.out.println("=============== Cadastrar Pessoa Juridica ============");
break;
case 3:
System.out.println(p);
break;
case 4:
System.out.println("Saindo...");
break;
}
opcao = menu ();
}
}
public static int menu (){
System.out.println("---------MENU----------");
System.out.println("1 Cadastrar Pessoa Fisica");
System.out.println("2 Cadastar Pessoa Juridica");
System.out.println("3 Mostrar Cadastros");
System.out.println("4 Sair");
System.out.print("Qual a opcao desejada? ");
return entrada.nextInt();
}
}