How can I associate 3 authors to just one book?

Asked

Viewed 243 times

1

package crud

class Start {
  static main(args) {
    Autor autor = new Autor()
    autor.nome = "Aline Gonzaga"
    autor.email ='[email protected]'
    autor.cpf =' 78544378654'


    Livro livro = new Livro(autor)
    livro.nome='Java: '
    livro.descricao= 'java'
    livro.valor= 378.99
    //livro.isbn= "8975849-54-5665-34-3-324-656-32-34-123"

    //livro.autor = autor


    livro.mostrarDetalhes()

    Ebook ebook = new Ebook()

    Autor outroAutor = new Autor()
    outroAutor.cpf ='754.548.545-34'
    outroAutor.email='[email protected]'
    outroAutor.nome='Jesus Cristo'

    Livro outroLivro = new Livro(outroAutor)
    outroLivro.descricao =' Como fazer o bem ao próximo?'
    outroLivro.isbn = ' 8754868596845986946'
  //    outroLivro.nome =' Fazendo o que é agradável a Deus'
    outroLivro.valor = 467.99
  //    outroLivro.autor = outroAutor
    outroLivro.mostrarDetalhes()


  }
}

package crud

class Book {

String nome
String descricao
double valor
String isbn
Autor autor

void mostrarDetalhes() {
    println "mostrando detalhes do livro: "
    println "Nome: " + nome
    println "Descrição: " + descricao
    println "ISBN " + isbn
    println "Valor: "+ valor
    autor.mostrarDetalhes()
    println "-----------------------------"
}
    public  Livro(Autor autor) {
    this()
    this.autor = autor
    }

    public Livro(){
        this.isbn = "0000-0000000-000000-000000-00"
        this.nome = " "
    }

}

2 answers

1

If I understood correctly you could make a constructror in the Book class that receives 3 arguments where each one would be an author. This is if you always want to associate 1 author or 3 authors to a book. If you prefer to make this association dynamically (ranging from 1 to x authors per book), I believe that a solution would be using an array of x positions where each position holds an author. Ai only pass this array as argument in constructor.

  • How could I make this array?

  • TO no IDE at the moment, but soon edit the answer with the code, can put the code of the class Book?

1


I don’t know if you’re familiar with Collections and Generics, but there is a way to solve the problem.

Instead of your class Livro have a type attribute Autor, you may have a List<Autor> representing a list of authors of the respective book.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Livro {

    List<Autor> listaDeAutores = new ArrayList<>();
    // Outros atributos do livro...

    Livro(Autor autor){
        listaDeAutores.add(autor);
    }

    Livro (Autor...autores){
        listaDeAutores.addAll(Arrays.asList(autores));
    }

    // Métodos...
}


This way you can create a new book containing an author. For example:

Autor autor = // ...
Livro livro = new Livro(autor);


Or, with several authors. For example:

Autor autor = // ...
Autor segundoAutor = // ...
Autor terceiroAutor = // ...

Livro livro = new Livro(autor, segundoAutor, terceiroAutor);

To display the author list you can retrieve the list contained in the attribute listaDeAutores and loop it around:

List<Autor> autoresDoLivro = livro.listaDeAutores;

for(Autor autor : autoresDoLivro){
   System.out.println("Nome: " + autor.nome);
}

It is not related to what was asked, but consider encapsulating the attributes and methods of your objects.
This question has some explanations.

  • I liked the answer! But that part I didn’t understand:------------------------------Book (Author...authors){ listDeuthors.addAll(Arrays.asList(authors)); }

  • Aline, in a big way, Arrays#asList() transforms an array into an Collection. In the builder of Livro, the authors are passed by varargs, as if it were Autor[] autores. Then (again, roughly) the array of Autor turns out to be a Collection authors.

  • 1

    This is Groovy and not Java, in addition to the code not being idiomatic for Groovy, the final observation does not make sense in Groovy, it is already encapsulated. Groovy lists have their own syntax in the language: http://www.groovy-lang.org/groovy-dev-kit.html#Collections-Lists Works normally but is not the most used style.

  • After she posted another question about Groovy I did see, at first I thought it was Java.

Browser other questions tagged

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