How to initialize an Arraylist in the constructor?

Asked

Viewed 1,245 times

0

I have to develop a program in Java that will rent a library.

In one part of the program I have to make a ArrayList of publications, follows the question:

We must create a system for controlling libraries and their loans. A library has a name, an address and stores a set of publications. Currently, the available publications are articles, books and theses. All publications have date of publication, a title, a list of other publications to which they refer and the set of authors of the publication. Attributes relevant to an author are their name and title. Relevant attributes for articles are their title, their date of publication, their authors, the others publications he refers to and his summary. For books it is relevant to store their title, their date of publication, the other publications that he references, their authors, the issue number, the name of the publisher and their ISBN. For the theses it is relevant to store its title, its date of publication, the other publications it references, the author of the thesis, the number of pages, abstract, date of the defense and the institution in which the thesis was defended.

When I create the class Biblioteca, and put the ArrayList of the publications in the constructor, how the implementation of this ArrayList? Do I have to do a method within the class to fill it in? It follows code from the class I did.

import java.util.ArrayList;

public class Biblioteca {
    private String nome;
    private String endereco;
    private ArrayList<Publicacoes> publicacoes;


    public Biblioteca(String nome, String endereco, ArrayList<Publicacoes> publicacoes) {
        super();
        this.nome = nome;
        this.endereco = endereco;
        this.publicacoes = publicacoes;
    }


    String getNome() {
        return nome;
    }


    void setNome(String nome) {
        this.nome = nome;
    }


    String getEndereco() {
        return endereco;
    }


    void setEndereco(String endereco) {
        this.endereco = endereco;
    }


    ArrayList<Publicacoes> getPublicacoes() {
        return publicacoes;
    }


    void setPublicacoes(ArrayList<Publicacoes> publicacoes) {
        this.publicacoes = publicacoes;
    }


}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful to you. You can also vote on any question or answer you find useful on the entire site

1 answer

1

There are some weird things in this class. The first is to call the super()in a class that inherits nothing but Object, can already say that this is not necessary, after all the builder of Object does nothing.

It makes no sense the external code to pass one array in the constructor or access it directly, actually external code should not even know it has a array in there, that is abstraction leak. To Biblioteca should allow you to add, remove, modify and access a post. Then you have to create methods to do these operations. I think that already answers the question, unless you want us to do these methods for you, which is not our goal.

Here comes another conceptual error, this array which should be internal contains several elements of the Publicacao since each one is individual, does not fit plural there.

I would always question the use of getter and Setter. There are cases you should use, but not at all, this is one of the plagues that took the codes because people only copy what others do without understanding the motivation.

Browser other questions tagged

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