Hi,
I am creating a program that when you print an author, you must show all the information of all the books that the author has written. Add a new attribute in the Author class that will be "BooksWriting: List (Book)".

I have expanded the program so that it also stores the created authors (the books will be stored by each created author), so that the program can be given the possibility to list all the books of authors that have a gender (sex) determined and that are not worth More than a certain price.

(The name of the variables is in Spanish, sorry)

public class Libro {

    private final String nombreLibro;
    private final double precioLibro;
    private final int cantidadEnStock;
    private final  Autor[] autores;
    private final String librosEscritos;

    public Libro(String nombreLibro, double precioLibro, int cantidadEnStock, Autor[] autores, String librosEscritos) {
        this.nombreLibro = nombreLibro;
        this.precioLibro = precioLibro;
        this.cantidadEnStock = cantidadEnStock;
        this.autores = autores;
        this.librosEscritos = librosEscritos;
    }

    //Metodos get y set
    public String getNombre() {
        return nombreLibro;
    }

    public String getNombreLibro() {
        return nombreLibro;
    }

    public double getPrecioLibro() {
        return precioLibro;
    }

    public int getCantidadEnStock() {
        return cantidadEnStock;
    }

    public Autor[] getAutores() {
        return autores;
    }

    public String getLibrosEscritos() {
        return librosEscritos;
    }

    public String setLibrosEscritos() {
        return librosEscritos;
    }

    public Autor[] setAutores() {
        return autores;
    }

    public String setNombre() {
        return nombreLibro;
    }

    public double setPrecioLibro() {
        return precioLibro;
    }

    public int setCantidadEnStock() {
        return cantidadEnStock;
    }  

public class Autor {

    private final String[] listaNombreAutor;
    private final String email;
    private final char[] listaSexoAutores;
    private final Libro[] libros;
    private final Libro[] listaLibrosEscritos;

    public Autor(String[] listaNombreAutor, String email, char[] listaSexoAutores, Libro[] libros, Libro[] listaLibrosEscritos) {
        this.listaNombreAutor = listaNombreAutor;
        this.email = email;
        this.listaSexoAutores = listaSexoAutores;
        this.libros = libros;
        this.listaLibrosEscritos = listaLibrosEscritos;
    }   

    //Metodos get y set
    public String[] getListaNombreAutor() {
        return listaNombreAutor;
    }

    public Libro[] getListaLibrosEscritos() {
        return listaLibrosEscritos;
    }

    public String getEmail() {
        return email;
    }    

    public Libro[] getLibros() {
        return libros;
    }

    public char[] getListaSexoAutores() {
        return listaSexoAutores;
    }

     public char[] setListaSexoAutores() {
        return listaSexoAutores;
    }        

    public String[] setListaNombreAutor() {
        return listaNombreAutor;
    }

    public Libro[] setLibros() {
        return libros;
    }

    public String setEmail() {
        return email;
    }

Recommended Answers

All 3 Replies

Sorry, my lack of Spnish makes that hard to understand, but I can see that you are using arrays when the spec asks for List. Anyway - what exactly is your question?

Hello, I do not know how to create arrays of objects in the main.

Here's an outline of how something like this can be structured... it contains the ideas you need. Once again - you were asked to use List, not arrays.

class Library { // top=level class, holds everything else, has search methods

   pubic static void main( …

   List<Author> authors = new LinkedList<>();

   void addAuthor(Author a) { …
   List<Author> getAuthors() { …

   List<Book> searchByGenderAndPrice(Gender g, double maxPrice) {
       for (Author a : getAuthors()) {
            if (a.get Gender == etc
            for (Book b: a.getBooks) {
                 if (b.getPrice <= …
                     add this book to the results list
      …            
}

class Author {
    String name, Gender …
    List<Book> books = new LinkedList<>();

    Author(Library lib, String name … ) {
        …
        lib.addAuthor(this);   //  maintain list of all Authors in Library

    void addBook(Book b)  {
    List<Book> getBooks() { …

}

class Book {

    Author author;
    String title, double price etc

    Book(Author a, String title …
         a.addBook(this);  // maintain list of all Books for this Author
         …

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.