Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1083337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:25:18+00:00 2026-05-16T22:25:18+00:00

I’ve this question from an assignment to create a Store which rent out books,

  • 0

I’ve this question from an assignment to create a Store which rent out books, using a Store.java and Book.java. I’ve finished this assignment, but I’m curious for better algorithm to a specific part.

—

Book.java

public class Book {

    private String name;

    Book(String name)
        this.name = name;

    public String getName()
        return name;

}

Store.java

Inside main();

 Book bookObj[] = new Book[3]; //Create 3 Array of Object.
 bookObj[0] = new Book("Game Over");
 bookObj[1] = new Book("Shrek"); 
 bookObj[2] = new Book("Ghost");
 Scanner console = new Scanner(System.in)
 input = console.nextLine();

Assuming, input = Devil.

Now, I need to do a simple search to check whether the specific book exist.

Example:

 for(int i = 0; i < bookObj.length; i++) {
     if(bookObj[i].getName().equals(input))
         System.out.println("Book Found!");
 }

Apparently, this is a for loop that cycles through the array of object and checks whether such Book exist. Now, the problem arise when I want to give an output that the Book was not found.

Example:

 for(int i = 0; i < bookObj.length; i++) {
     if(bookObj[i].getName().equals(input))
         System.out.println("Book Found!");
     else
         System.out.println("Book not Found!");
 }

The problem with the above code is that Book not Found would be printed thrice. My goal is to avoid such problem. I do have solutions to this, but I’m still in search for a better one to use that utilizes getName(), which in my opinion still has room to improve.

Usually, in structural programming, I would do the following,

for(int i = 0; i < bookObj.length; i++) {
     if(bookObj[i].getName().equals(input))
         System.out.println("Book Found!");
     else if(i == bookObj.length - 1)
         System.out.println("Book not Found!");
 }

This is useful to tell whether it’s the end of the loop, and the search has ended, but there was no successful result from the search.

How should I think of it in Object Oriented way?

All in all, my question is,

  1. Is there a better way to write the above code rather than checking that it’s the end of the line?
  2. Is there a better way to utilize getName() method or to use other methods?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:25:19+00:00Added an answer on May 16, 2026 at 10:25 pm

    You should loop through the array and use an index / boolean flag to store whether or not the book is found. Then print the message in the end, based on the index / flag value.

    int foundAtIndex = -1;
    for(int i = 0; i < bookObj.length; i++) {
        if(bookObj[i].getName().equals(input)) {
            foundAtIndex = i;  // store the actual index for later use
            break;             // no need to search further
        }
    }
    if(foundAtIndex >= 0)
        System.out.println("Book Found!");
    else
        System.out.println("Book not Found!");
    

    Alternatively (unless your assignment specifically requires using an array) you should prefer a Set, which can do the search for you with a single call to contains().

    How should I think of it in Object Oriented way?

    When looking at a single method, there is not much difference between procedural and OO style. The differences start to appear at a higher level, when trying to organize a bunch of conceptually related data and methods that operate on these.

    The OO paradigm is to tie the methods to the data they operate on, and encapsulate both into coherent objects and classes. These classes are preferably representations of important domain concepts. So for your book store, you may want to put all book related code into your Book class. However, the above search method (and the collection of books it operates on) is not related to any particular book instance, so you have different choices:

    • put both the collection of books and the search method into Store (probably as regular members), or
    • put them into Book as static members.

    The first choice is more natural, so I normally would prefer that. However, under specific circumstances the second option might be preferable. In (OO) design, there are hardly ever clean “yes/no” answers – rather tradeoffs between different options, each having their own strengths and weaknesses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.