Overall goal is to create a library simulation that allows searches for books as well as transactions eg to borrow a book. I've only included the parts I think are relevant.
I have the array stock
in the Biblio
class as follows:
public class Biblio{
private ArrayList<Book> stock;
and the Book
class with the constructor:
public Book(String author , String title)
{
this.author = author;
this.title = title ;
code = null;//this is a String
}
In the main method I am trying to search for a Book object using the code
(which is String
), I am using the findCode
method in the Biblio
class:
public Book findCode(String searchedCode){
Book foundBook = null;
for(Book bookObj : stock){
if(bookObj.getCode().equxals(searchedCode)){
bookObj= foundBook;}//
return foundBook;
}
and here is the code in the main method I currently have:
Biblio libSim;
libSim = generateLib();
...
System.out.println(libSim.findCode(scan.next()));
but the output is coming up as null and I think it is because the foundBook
equals null. However I can't return bookObj
.
I do not know how I can find an object in an array by searching for the code
variable. The books are added to the library in another method and using a setter method I have set any code
that equals null
to a string with the format LIB0001
, this is done before I call the findCode
method.
How do you find an object by searching the array for a specific instance variable?