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).