First, leave the class name in the singular. So "Cartoes
" should be exchanged for "Cartao
".
Second, don’t forget the modifier private
or public
if you are not interested in package visibility (rarely is the case).
Third, there is a }
the most left in the middle of the class Cliente
.
Room, if you want to set arrays, can do so:
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 size and defined. 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 null
).
To place 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 size of the array (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 too low level and using them is both laborious and difficult. To enter elements, you will have to track which position it will be inserted into. To delete elements from the medium (by placing a null
at some position), a hole will be 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 will have to create another larger array, copy all the elements and use this new array to replace the original one. In addition to other difficulties.
So maybe you want to use java.util.List
, which is much easier and practical 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 List
is an interface. There are some implementations, such as java.util.ArrayList
or java.util.LinkedList
, in addition to some others for more specific purposes.
For example, let’s create a customer list:
clientes = new ArrayList<>();
Note that you do not need to specify the size. The ArrayList
adjusts its own internal size automatically when necessary. If you specify the size anyway (ex: ArrayList<>(20)
), by inserting an element that bursts the size, it changes its own internal size smoothly (this may have some performance cost, but it works anyway).
To add elements to List
:
Cliente fulano = ...;
clientes.add(fulano);
Or else 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 automatically added at the end, you don’t have to be in control of what the proper position is to do this. To get the size (with cliente.size()
) It gives you the number of elements entered and not the number of internal positions available, which is a much more useful information in practice. Also, by removing an element from the middle, it automatically already eliminates the hole that would be.
To limit the number of elements, just use a if
. 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 how:
- Each bank has a customer list.
- Each bank has a list of accounts.
- Each bank has a list of cards.
- All banks share a common name (I don’t think that’s what you want, but that’s what this code says).
- All banks share a CNPJ in common (again, I don’t think that’s what you want, but that’s what this code says).
Possible duplicate of What is the name of the concept used in this code?
– user28595
Help here! Curl PHP webservice
– Alisson Hoepers