So far, you have received some pretty good advice. You asked if there is a more object-oriented way of thinking about a problem, so I thought that I would try to shed light on it. Since Peter has already mentioned at this level of design, this is the only implementation of the method, so the approach will be quite similar, such as the procedural approach. What is the advantage? In a word reuse. If you need to find a book by name in many places, moving your code to it will help your own class.
So, you have one copy of a book to encapsulate behavior around one book, but you want to have behavior across multiple books or a collection of books. You can store data (an array of books) and the method that their account shares, as you described in your program. However, if we wanted to gather a place for behavior in a collection of books, we can define a new class. Let me call it a library, and we can do something like the following:
public class Library { private Book[] books; private bookCount = 0; public Library( int numberOfTotalBooks ) { books = new Book[numberOfTotalBooks]; } public boolean addBook( Book book ) { if( bookCount < book.length ) { books[bookCount++] = book; return true; } return false; } public Book findByTitle( String title ) { for( int i = 0; i < bookCount; i++ ) { if( books[i].getTitle().equals( title ) ) { return books[i]; } }
So, a few things to note that they do. Firstly, when we work with the library, we do not know that there is an array there. We could use an array, set, list, or database (the most common). The point, which is the code that calls these functions, works only with the library interface (and not with the literal Java interface, but with the signature of the library method). It is also a higher level interface. We are not worried about sorting through books, doing for loops, if statements, etc. We just call a method that says "Hey, find the name of this book in the Library." How this was done, we do not care. This is the main tenant of Object Orientation, called encapsulation, and deceptively powerful. It really is about how we delegate responsibility in our program and give details of work to an individual class or classes. If there were only public participants in the Library (i.e. Books and bookCount), or getter / setters, then the client would not receive any benefits, because the client would still have to complete the whole difficult climb. The trick for OO is that you can delegate from an object without creating problems. It requires practice and experience.
Secondly, we separated the presentation from the act of searching for a book. The method you wrote suggested the next step, which was to print "Hey, we found it." However, the library object simply returns you the book when it finds it, or null if it is not. This allows you to print to the console, display in the GUI, or serialize it to a JSON stream on the server. The act of searching for a book is separate from visualization. This is another important aspect of programming in general, but some of them are related to object orientation and encapsulation. This is usually called separation of concerns. The console application has problems with user interface support and console printing. Although the Library only manages cataloging and book collection management. How these details are carried out do not care.
At the end, the library is a reusable class. We can use it in a console application, desktop, network or intermediate server. Moreover, we can also reuse calls to findByTitle or addBooks from several places within the same program. In addition, by placing methods with data, we create a barrier to where this function can be used. You cannot do this in your program. You must have a link to the library. Unless you have a reference to a library instance, you should not call it. This can be unpleasant for new developers, because they lack the experience to properly organize their programs, so as not to get into trouble with this (then they start making value objects, creating statics, single games, etc., and everything turns in a big ball of mud). This is a double-barreled sword.
Another thing I would like to point out is to say that we would like to model two libraries. We have a library in the city center and in the city center, and we want people to be able to check books from the Library. With OO that are very easy to imagine:
Library uptown = new Library( 50 ); Library downtown = new Library( 100 );
Now we can check books from one or the other. And I did not use statics (i.e. Global variables), so reusing this logic is very simple. These are the basics of OO, so they are really deep topics. It is strange how I can write so much on very simple topics. In any case, I hope this helps you understand your program a little deeper and see how you can use OO to help you.