2
I am creating a simple bank system (studying), there is a Account class with basic registration information, and a System class that manages all accounts. I intend to create a class ContaDiretor
which should extend from Conta
(as it is an account) and Sistema
(as it must have access to class-unique methods/attributes Sistema
).
package Conta;
import Banco.Cliente;
import Banco.Data;
public class Conta {
public int numero;
protected double saldo;
public String extrato[] = new String [100];
public Cliente titular = new Cliente();
public Data DataDeAbertura = new Data();
public void depositar (double saldo){
this.saldo += saldo;
}
public double getSaldo(){
return saldo;
}
protected boolean podeSacar(double valor){
if (saldo < valor)
return false;
else
return true;
}
public void sacar (double valor, Conta conta){
if (podeSacar(valor)) {
saldo -= valor;
System.out.println("Novo Saldo: ");
System.out.print(conta.getSaldo());
System.out.println("");
}
else
System.out.println("Saldo indisponível");
}
public void transferencia (Conta c2){
}
public void getExtrato () {
for (int i = 0; i < extrato.length; i++) {
if (extrato[i] == null){
break;
}
else
System.out.println(extrato[i]);
}
}
public void setExtrato(String nome, String atividade, double valor) {
String extrato = nome + " " + atividade + " " + Double.toString(valor);
for (int i = 0; i < this.extrato.length; i++) {
if (this.extrato[i] == null) {
this.extrato[i] = extrato;
break;
}
}
}
public void setExtrato(String nome, String atividade, double valor, String nome2) {
String extrato = nome + " " + atividade + " " + Double.toString(valor) +" para "+ nome2;
for (int i = 0; i < this.extrato.length; i++) {
if (this.extrato[i] == null) {
this.extrato[i] = extrato;
break;
}
}
}
public boolean contaequals(Conta a){
return (this.titular.login == a.titular.login);
}
}
package Banco;
import Conta.Conta;
public class Sistema{
public Conta contas[] = new Conta[100];
public Conta contaPublic = new Conta();
private String nomeBanco = "Banco do Brasil"; //O diretor deve ser o único capaz de alterar o nomeBanco
private int totalContas = 0; //O diretor deve ser o único capaz de receber totalContas
public String getNomeBanco() {
return nomeBanco;
}
public void adicionaConta(Conta conta){
this.contas[totalContas] = conta;
totalContas++;
}
public Conta getConta(int pos) {
return contas[pos];
}
public Conta[] getContaVetor() {//retorna o vetor inteiro
return contas;
}
}
I know very little of interface, I tried to create one, but found that all attributes of an interface are final (are not modifiable).
If I create in the interface the abstract methods as setNomeBanco
and getTotalContas
as ContaDiretor
will change the attributes that only exist in Sistema
?
I don’t want to leave you public (totalContas
and nomeBanco
) because I just want ContaDiretor
can receive and change them respectively.
Like ContaDiretor
will inherit the methods and attributes of the Account and System classes at the same time?
Below is the class draft ContaDiretor
and interface SistemaInterface
:
package Conta;
import Banco.InterfaceSistema;
public class ContaDiretor extends Conta implements InterfaceSistema{
public static String nomeDoBanco;
public void setNomeDoBanco(String nome) {
this.nomeDoBanco = nome;
// TODO Auto-generated constructor stub
}
public String getNomeBanco() {
}
}
package Banco;
import Conta.Conta;
public interface InterfaceSistema {
public String getNomeBanco();
public void adicionaConta(Conta conta);
public Conta getConta(int pos);
public Conta[] getContaVetor();
}
Try with composition, maybe it will. I recommend the reading
– renanvm
Java does not have multiple inheritance. You can extend from just one class. Interfaces are contracts where the methods to be implemented are defined by the classes that will execute the behavior defined by the interface. More about interfaces.
– Badaro
ContaDiretor
is aConta
which is managed bySistema
. It makes no sense to have system features. What is the problem statement? With this information will be easier to help you.– Badaro