Skip to content

Commit

Permalink
heranca, polimorfismo
Browse files Browse the repository at this point in the history
  • Loading branch information
vinicius committed Dec 21, 2020
1 parent f66dea9 commit afa121e
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Banco/src/Acionista.java
@@ -0,0 +1,8 @@
public class Acionista {

private int quantidadeDeCotas;

private float totalDividendosRecebidos;


}
2 changes: 0 additions & 2 deletions Banco/src/ContaCorrente.java
Expand Up @@ -71,8 +71,6 @@ public void sacar(float valor) {
valor, new Date()));
}



/**
* Transfere um valor desta conta para a conta destino informada, se houver saldo suficiente
* nesta conta.
Expand Down
39 changes: 39 additions & 0 deletions Banco/src/Funcionario.java
@@ -0,0 +1,39 @@
public class Funcionario extends Pessoa {

private int matricula;

private float salario;

private String cargo;

public Funcionario(String nome, long cpf, int matricula) {
/* A primeira linha de qualquer construtor PRECISA SER
uma chamada ao construtor da superclasse, via super(.....).
Se nós não fizermos explicitamente essa chamada,
o compilador vai acrescentar automaticamente
a chamada
super();
*/

super(nome, cpf);

this.matricula = matricula;
this.cargo = "sem cargo";
this.salario = 1000f;
}

public int getMatricula() {
return matricula;
}

public void receberAumento(float percentual) {
salario *= (1 + percentual/100);
}

@Override
public String toString() {
return String.format("%d --- %s",
matricula, super.toString());
}
}
17 changes: 17 additions & 0 deletions Banco/src/Gerente.java
@@ -0,0 +1,17 @@
public class Gerente extends Funcionario {

private int nivel; // 1: gerente de conta; 2: gerente geral

public Gerente(String nome, long cpf, int matricula) {
super(nome, cpf, matricula);
nivel = 1;
}

public void setNivel(int nivel) {
if (nivel < 1 && nivel > 2) {
throw new RuntimeException("Nivel invalido!");
}

this.nivel = nivel;
}
}
22 changes: 17 additions & 5 deletions Banco/src/Pessoa.java
Expand Up @@ -2,18 +2,19 @@

public class Pessoa {

private String nome;
protected String nome; // protecgted é "package private" + subclasses

private final long cpf; // final indica que o campo JAMAIS poderá ser atualizado
protected final long cpf; // final indica que o campo JAMAIS poderá ser atualizado

private Date dataDeNascimento;

private String endereco;

// overload do construtor
public Pessoa(String nomeDaPessoa, long cpfDaPessoa) {
nome = nomeDaPessoa;
cpf = cpfDaPessoa;
endereco = "Endereço desconhecido";
this.nome = nomeDaPessoa;
this.cpf = cpfDaPessoa;
this.endereco = "Endereço desconhecido";
}

public void setEndereco(String endereco) {
Expand All @@ -23,7 +24,18 @@ public void setEndereco(String endereco) {
this.endereco = endereco;
}

public String getNome() {
return nome;
}

public long getCpf() {
return cpf;
}

@Override
public String toString() {
return String.format("%s (CPF: %d)",
nome,
cpf);
}
}
52 changes: 52 additions & 0 deletions Banco/src/Principal.java
@@ -1,6 +1,58 @@
import java.util.Scanner;

public class Principal {

public static void main(String[] args) {

Pessoa fulano = new Pessoa("Fulano de Tal", 12345);

System.out.println("cpf = " + fulano.getCpf());

System.out.println(fulano.toString());


Funcionario paiva;
paiva = new Funcionario("Paiva", 234567, 1111);

Pessoa beltrano;
beltrano = criarFuncionario();

beltrano.setEndereco("Rua Tal, numero 1");

//beltrano.receberAumento(10); // essa linha não compilaria!!!!

paiva.receberAumento(10);

System.out.println(beltrano.toString());


}

private static Pessoa criarFuncionario() {
Scanner sc = new Scanner(System.in);

System.out.println("Nome: ");
String nome = sc.nextLine();

System.out.println("CPF: ");
long cpf = sc.nextLong();

// consome o "\n" que o nextLong não irá consumir
sc.nextLine();

System.out.println("É funcionário (S|N)? ");
String resposta = sc.nextLine();
boolean ehFuncionario = resposta.equals("S") ||
resposta.equals("s");

if (ehFuncionario) {
System.out.println("Matrícula: ");
int matricula = sc.nextInt();
return new Funcionario(nome, cpf, matricula);
}

//return new Object(); // não compilaria!!!!!

return new Pessoa(nome, cpf);
}
}

0 comments on commit afa121e

Please sign in to comment.