Store customers inside an Arraylist

Asked

Viewed 2,260 times

0

Taking into account the classes below, how can I - within the Bank class - store within the Arraylist all my customers?

Bench

public class Banco {
    private ArrayList<Conta> contas;

    public Banco() {
        contas = new ArrayList<Conta>();
    }
}

Client

public class Cliente {
    private String nome;
    private String telefone;

    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }
    public String getTelefone() {
        return telefone;
    }
    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }
}

Bill

public class Conta {
    private int numero;
    private double saldo;
    private Cliente cli;

    public Conta() {

    }

    public int getNumero() {
        return numero;
    }
    public void setNumero(int numero) {
        this.numero = numero;
    }
    public double getSaldo() {
        return saldo;
    }
    public void setSaldo(double saldo) {
        this.saldo = saldo;
    }
    public Cliente getCli() {
        return cli;
    }
    public void setCli(Cliente cli) {
        this.cli = cli;
    }
}
  • The important thing is to work :)

  • Could put the code of the part where creates the customers, the accounts and the bank?

  • The objects Conta already have a reference to its object Cliente, so the bank does not store customers, but stores accounts that have references to their respective customers. There is nothing wrong with this idea and the modeling of the classes is correct, I did not understand your doubt.

  • My goal is to create a method within the Bank class that lists names and phones of customers who have negative balance

  • Um yes, I added this method to my reply. I confess that I initially got a bit confused.

2 answers

0

Uses a method in the Bank class similar to setCli() of the Account class. Kind of:

public class Banco {
    private ArrayList<Conta> contas;

    public Banco() {
        contas = new ArrayList<Conta>();
    }

    public void setConta(Conta conta) {
        contas.add(conta)
    }
}

0


Taking into account the classes as they are, you can create a method in the class Banco that returns all customers in one ArrayList.

public ArrayList<Cliente> listarClientes(){
    ArrayList<Cliente> clientes = new ArrayList();
    for(Conta conta : contas){
        clientes.add(conta.getCli());
    }
    return clientes;
}



public ArrayList<Cliente> listarInadimplentes(){
    ArrayList<Cliente> inadimplentes = new ArrayList();
    for(Conta conta : contas){
        if(conta.getSaldo()<0)
            inadimplentes.add(conta.getCli());
    }
    return clientes;
}
  • This code works, but does not follow the recommendations of reference by Interface. That is, it should return a type List<E> instead of Arraylist<E>, another point is that there is a raw type when instantiating Arraylist. Should be Arraylist<Client>(); or Arraylist<>(); For Java 7. Otherwise there will be warnings in your code.

  • In fact I wanted to facilitate once she urged accounts as arrayslist.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.