Working with list and objects in Java

Asked

Viewed 341 times

2

I have a java class called Library. with attributes: name, city, number of employees:

public class Biblioteca{
   String nome;
   String cidade;
   int qtdFuncionarios;

   //depois getters e setters e construtor...
}

And a Book class, containing: title, publisher, year.

public class Livro{
   String titulo;
   String editora;
   String ano;

   //depois getters e setters e construtor...
}

What I can’t do is this:

I need to create a library with a list of books for that library. That is, register a library and then register books for that library, saving it in a list. I can have several libraries, each containing their respective books. After saving to the list, a method that lists all books receiving the library name as parameter. Could someone help me with this?

  • So there are two different lists, one library and within each library, a list of books, correct?

  • That, exactly

  • You can use Arraylist for example?

  • Might as well, I just couldn’t do it.

1 answer

2


Add the Book Class inside the library

public class Biblioteca {
   private String nome;
   private String cidade;
   private int qtdFuncionarios;
   private ArrayList<Livro> lstLivro;    
}

then create a default constructor for the class, so you can decide if there will be library without book

public Biblioteca (String nome, String cidade, int qtdFuncionarios, ArrayList<Livro> lstLivro) {
   this.nome = nome;
   //-- resto da implementação
}

At your control you can create an Arraylist or a Hashmap to control several libraries.

...
HashMap<string, Biblioteca> lstBiblioteca = new HashMap<>();
lstBiblioteca.put(Biblioteca.getNome(), Biblioteca);
...

or

ArrayList<Biblioteca> lstBiblioteca = new ArrayList<>()
lstBiblioteca.add(Biblioteca)

Using Arraylist vc tera q do a foreach to perform the search (do not recommend).

With Hashmap you can use . containsKey(Key) to verify the existence in the hash table and retrieve the pointer with . get(Key)

I am assuming that you are loading the entire file to memory, because if you are using SQL you better do the CRUD search and return the dataset to your list containing only the necessary or displaying the result if it is unique.

//-- para efetuar busca por parametro q não seja o Key do hashmap
//-- Ex.: Nome Editora
   ArrayList<Biblioteca> tmpLstBiblioteca = new ArrayList<>();
   for(Biblioteca biblioteca : lstBiblioteca.value) {
      for(Livro livro : biblioteca.getLstLivro()) {
         if(livro.getEditora().toUpper().contains(NOME_BUSCADO.toUpper())) {
            tmpLstBiblioteca.add(biblioteca);
         }
      }
   }
   return tmpLstBiblioteca;

I use . contains(str) to do a partial search and . toUpper() to not have problem with the case sensitive, good would be before adding the name and before searching do the removal of spaces ". Trim()" of strings.

  • And library research by name?

  • Hashmap<string, Library> lstBiblioteca = new Hashmap<>() lstBiblioteca.put(Library.getNome(), Library).

  • 1

    Add the answer to make it complete :)

  • To search books by publisher for example, searching books from a particular publisher of any library, works in the same way?

  • Using this example not, because the key (key) of your Hashmap is the name, in this case you would have to foreach (Library : lstBiblioteca.value) { //-Codigo... }, At the end you will have 2 foreach, one to run all libraries and one to fetch the publisher q vc wants, if you want put an example

  • If you have an example, please facilitate understanding, since it is the first time I work with it.

Show 1 more comment

Browser other questions tagged

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