How to add several objects to an object?

2

I have a class called Book in which the attributes are name id and an object of another class called author. My question is whether it is possible to add several authors to a single object of the book class?

    package p;
    import java.util.ArrayList;

    public class Libro {
    private String nombre;
    private int id;
    private Autor autor;    

    public Autor getAutor() {
        return autor;
    }

    public void setAutor(Autor autor) {
        this.autor = autor;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    ArrayList<Libro> librolist = new ArrayList<>();
    ArrayList<Autor> autorlist = new ArrayList<>();

    public ArrayList<Libro> getLibrolist() {
        return librolist;
    }

    public void setLibrolist(ArrayList<Libro> librolist) {
        this.librolist = librolist;
    }

    public ArrayList<Autor> getAutorlist() {
        return autorlist;
    }

    public void setAutorlist(ArrayList<Autor> autorlist) {
        this.autorlist = autorlist;
    }   
}

Author Class

package p;

public class Autor {
    private String nombre;
    private int id;

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }   
}
    
asked by EduardoCh 12.12.2018 в 22:55
source

2 answers

0

hand in hand with the solution that gave @jachguate I would like to add another option, the use of varargs . varargs allows us to define a method that allows from 1 to n paramenters of the same type and I think that this is an excellent option for this question therefore using varargs serious:

public class Libro {
private String nombre;
private int id;
private Autor[] autores;    

//...

public void agregarAutores(Autor ...newAutores) {
    if (autores != null) {
       autores = Stream.concat(Arrays.stream(autores),
           Arrays.stream(newAutores)).toArray(Autor[]::new);
    } else {
      autores = newAutores;
    }
}
//...
}

Note that varargs is similar to using an array therefore from the perspective of agregarAutores(Autor ...newAutores) and agregarAutores(Autor newAutores[]) is similar with respect to Array.Stream() that is explained in this link (but basically this line of code combines 2 arrays in a single array)

now how to use the agregarAutores(Autor ...newAutores) method is to simply call it with 1 or more paramtros of type Author:

milibrox.agregarAutores(autor1,autor2);

milibrox.agregarAutores(autor3);

milibrox.agregarAutores(autor1,autor2,autor3);

milibrox.agregarAutores(autor1,autor2,...); etc ...

    
answered by 13.12.2018 / 05:37
source
0

If you can, but you do not give enough detail to produce a solution, so I'll give you two alternatives so that you can use the values that are best for you:

Add a defined number of authors

For example, make it possible to have 3 authors, each one being a member of the class, something like:

public class Libro {
    private String nombre;
    private int id;
    private Autor autor1, autor2, autor3;    

    public Autor getAutor1() {
        return autor1;
    }

    public void setAutor1(Autor autor) {
        this.autor1 = autor;
    }

    public Autor getAutor2() {
        return autor2;
    }

    public void setAutor2(Autor autor) {
        this.autor2 = autor;
    }

    public Autor getAutor3() {
        return autor3;
    }

    public void setAutor3(Autor autor) {
        this.autor3 = autor;
    }

Add a variable number of authors

In this case, the author can be zero, one or several. Again, there would be several ways to implement this, for example, declaring as a member of the class a vector of authors and methods for adding new authors. It would not make much sense to add a method to remove an author, but it could be done as well. In code, you might see something like this:

public class Libro {
    private String nombre;
    private int id;
    private Autor[] autores;    

    public void agregarAutor(Autor autor) {
        if (autores != null) {
           Autor[] arr2 = Arrays.copyOf(autores, autores.length + 1);
           arr2[arr2.length - 1] = autor;
           autores = arr2;
        } else {
          autores = new Autor[1];
          autores[0] = autor;
        }
    }

Warning: The code may have errors, was written here in the browser and is for illustrative purposes only.

    
answered by 12.12.2018 в 23:40