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.
So there are two different lists, one library and within each library, a list of books, correct?
– user28595
That, exactly
– PHP developer
You can use Arraylist for example?
– user28595
Might as well, I just couldn’t do it.
– PHP developer