I do not remember some concepts about Class Arrays

0

I'm wondering ..

1 - Is my reasoning correct? For example, if I put " Funcionário funcionario " inside class " Empresa ", it means that: "In the company has employee". Correct?

2 - How do I define a class array, that is, I want to define how many clients can have in the database, how many cards can have for each client.

Rgn.java
class Banco {
    static String nome = "Banco Saad";
    static String cnpj = "000.000.000";

    Cliente clientes;
    Conta contas;
    Cartoes cartoes;
}

class Cliente {
    private String nome;
    private String cpf;

    Cartoes cartoes;

    Cliente(String nome,String cpf){
        this.nome = this.nome;
        this.cpf = this.cpf;
    }

    }

    public String getCliente(){
        return this.nome;

    }
}


class Conta {
    private String agencia;
    private String conta;
    private double saldo;

    Cartoes cartoes;

    Conta(String agencia, String conta){
        this.agencia = this.agencia;
        this.conta = this.conta;
    }
}

class Cartoes {
    private String nomeCliente;
    private String tipoStats[] = {"Fit", "Diamond"};
    private String tipoCartao[] = {"Débito", "Crédito"};
    private String tipoLocal[] = {"Nacional", "Internacional"};
    private String bandeiraCartao = "Mastercard";
    private String numeroCartao;
    private double limiteCartao;

    Conta contas;

    Cartoes (String nomeCliente, String numeroCartao, double limiteCartao){
        this.nomeCliente = this.nomeCliente;
        this.numeroCartao = this.numeroCartao;
        this.limiteCartao = this.limiteCartao;
    }

}
    
asked by anonymous 19.07.2016 / 06:59

2 answers

1

First, leave the class names in the singular. Therefore " Cartoes " should be replaced by " Cartao ".

Second, do not forget the modifier private or public if you are not interested in package visibility (this is seldom the case).

Third, there is an additional% w / w remaining in the middle of the } class.

Fourth, if you want to define arrays, you can do this:

class Banco {
    private static String nome = "Banco Saad";
    private static String cnpj = "000.000.000";

    private Cliente[] clientes;
    private Conta[] contas;
    private Cartao[] cartoes;
}

The array has a fixed and defined size. You can create it like this:

clientes = new Cliente[50];

And this will create an array with 50 positions. Initially all of them will be empty (containing Cliente ).

To put an element in the array:

 Cliente fulano = ...;
 clientes[10] = fulano;

Or to put several:

 Cliente fulano = ...;
 clientes[10] = fulano;
 Cliente ciclano = ...;
 clientes[11] = fulano;
 Cliente beltrano = ...;
 clientes[12] = beltrano;

To get the array size (in the total number of positions, not in the number of positions used):

 int tamanho = clientes.length;

However, you will quickly realize that arrays are somewhat low-level and using them is a bit hard work. To insert elements, you will have to trace in which position it will be inserted. To exclude elements from the middle (by putting a null in some position), a hole will remain in the array and you will have to turn around to fix it. If the size of the array is too small, it will not fit, and then you have to create a larger array, copy all the elements, and use that new array to replace the original. In addition to other difficulties.

So you might want to use null , which is much easier and more convenient to use than arrays:

class Banco {
    private static String nome = "Banco Saad";
    private static String cnpj = "000.000.000";

    private List<Cliente> clientes;
    private List<Conta> contas;
    private List<Cartao> cartoes;
}

You may notice that java.util.List is an interface. There are some implementations, such as List or java.util.ArrayList , and a few more for more specific purposes.

For example, let's create a list of clients:

 clientes = new ArrayList<>();

Please note that you do not need to specify the size. java.util.LinkedList adjusts its own internal size automatically when needed. If you specify the size anyway (eg ArrayList ), when inserting an element that pops up, it changes its own internal size without any problems (this may have some performance cost, but it works anyway).

To add elements in ArrayList<>(20) :

 Cliente fulano = ...;
 clientes.add(fulano);

Or to add multiple clients at once:

 Cliente fulano = ...;
 clientes.add(fulano);

 Cliente ciclano = ...;
 clientes.add(ciclano);

 Cliente beltrano = ...;
 clientes.add(beltrano);

And it's already added automatically at the end, you do not have to keep track of the proper position to do this. To get the size (with List ) it gives you the number of elements inserted and not the number of internal positions available, which is much more useful information in practice. Also, by removing an element from the middle, it automatically eliminates the hole that it would be.

To limit the number of elements, just use cliente.size() . For example:

public class Cliente {

    private List<Cartao> cartoes;

    // ...

    public void adicionarCartao(Cartao paraAdicionar) {
        if (cartoes.size >= maximoCartao) throw new IllegalStateException("Este cliente já tem cartões demais.");
        cartoes.add(paraAdicionar);
    }
}

Finally, when we see this code:

class Banco {
    private static String nome = "Banco Saad";
    private static String cnpj = "000.000.000";

    private List<Cliente> clientes;
    private List<Conta> contas;
    private List<Cartao> cartoes;
}

We can interpret as:

  • Each bank has a list of clients.
  • Each bank has a list of accounts.
  • Each bank has a list of cards.
  • All banks share a common name (I do not think it's what you want, but that's what that code says).
  • All banks share a common CNPJ (again, I do not think it's what you want, but that's what that code says).
19.07.2016 / 07:40
1

1 - It is correct, everything in the code (except a syntactic error), in the concept of a general way, at least how far to identify. Of course in real code a lot is wrong there. So it is difficult to try to learn concepts with simulations of problems, in thing invented everything can be certain. A real problem depends on the specification and would probably be quite different from what was modeled there. "Invented" problems can be useful to help at specific coding points, not to architect a system.

2 - You can do the same as already done on cards:

Cliente[] clientes = new Cliente[50]; //para 50 clientes

If you do not want a specific limit use a list:

ArrayList<Cliente> clientes = new ArrayList<>(); //inicializando p/ evitar exceção

Of course there are several other things that need to be improved in the classes.

    
19.07.2016 / 07:20