I can’t remember some concepts about class arrays

Asked

Viewed 121 times

0

I’m wondering.

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

2 - How do I define a class array, ie I want to define how many clients can have in the bank, 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;
    }

}

2 answers

2

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

2 - Can do the same as already done on cards:

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

If you want there to be no specific limit use a list:

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

I put in the Github for future reference.

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

  • Hi @bigown, could you mention what I should improve? I’m studying for Alura and just want to see what I have to review in order to advance the course. I have no experience in "real system" because I’m in college yet, I’m getting certified in Java Junior Developer Student to see if I can get an internship.

  • Actually, no, the list is huge, it is far from adequate, the question would be broad and would have to be closed to answer everything. Another problem is that to improve you need to have a more specific problem. In something invented, anything can serve. You can say "that’s how I wanted it," that’s right. Most of these courses don’t teach well. You have to take it one step at a time. It is no use trying to learn architecture, modeling without mastering the language well. First learn to program the basic grade 10, then think about putting it all together.

  • But it is in a better way than many people who only follow the recipe for cake. You ask very specific questions and improve each point.

  • I got @bigown, like you said.. Did you use "Arraylist" that in case I haven’t learned yet, more I could do Client[] clients = new Client[]; To record "infinitely" ? Do you think that these courses of Alura is worth only to achieve internship in the area of programming? Because here in RJ it is very complicated to get internship and I do not want to send for employment, because I’m really afraid to start already employed without having internship experience. rs

  • Can’t, array has fixed size. I don’t know what it’s worth, I know I wouldn’t hire someone who learned exclusively by this type of course. But the market accepts so much, I don’t know. I like the right things. There is even a crisis that complicates things a little, but if in Rio is bad, then only in SP will be good. What I do not think is true. There are cases that do not have vacancy, but a lot of case is because the person is not yet prepared. Do not seek internship as a favor they will do you, make your hiring desirable that will open vacancies where you do not have.

2


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).
  • thank you very much.. In case I will create only with Arraylist now and without vectors.. But you just said that to add just use : Customer so = ...; customers.add(so-and-so); But if I want to add more than one customer, what should I do with this so-and-so "variable"? .

  • Had misdirected the comment, already set up.

  • @Thiagodebonis Edited. What do you think?

Browser other questions tagged

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