Code Logo

Library Book Status

Published at22 Apr 2026
Java OOP Easy 0 views
Like0

A school library keeps a simple record for each book: the title and whether that book is currently checked out. When a student asks about a book, the desk wants to show a short message explaining its status.

Your task is to use a small Book class and return a message in one of these two formats:

"Title is available" if the book is not checked out, or "Title is checked out" if it is.

For example, if the title is "Math Club" and the checked-out value is false, the answer should be "Math Club is available". If the title is "Science Lab" and the checked-out value is true, the answer should be "Science Lab is checked out".

This is an easy object-oriented exercise where the class stores the book's state, and one of its methods turns that state into a readable message.

Example Input & Output

Example 1
Input
title = "History Notes", checkedOut = false
Output
"History Notes is available"
Explanation

The title should stay intact inside the final message.

Example 2
Input
title = "Science Lab", checkedOut = true
Output
"Science Lab is checked out"
Explanation

A checked-out book should return the checked-out message.

Example 3
Input
title = "Math Club", checkedOut = false
Output
"Math Club is available"
Explanation

A book that is not checked out should be marked available.

Algorithm Flow

Recommendation Algorithm Flow for Library Book Status
Recommendation Algorithm Flow for Library Book Status

Solution Approach

This problem is a nice example of using an object to hold state and then letting the object decide how to describe itself.

The Book class can store two fields: the title and the checked-out flag. Once those are saved in the constructor, the class can expose a method like getStatusMessage() that returns the correct sentence based on the boolean value.

The main logic sits naturally inside the class:

public String getStatusMessage() {
    if (checkedOut) {
        return title + " is checked out";
    }
    return title + " is available";
}

Then the outer solution method only needs to create the object and call that method. This keeps the decision about the book's message inside the Book class, which is the most object-oriented part of the exercise.

You could also use a ternary operator instead of an if statement, and that would still be correct. The important idea is not the exact syntax. It is the pattern of giving a class both the data and the behavior that depends on that data.

The runtime is constant, O(1), because each call builds one object and returns one short string. The challenge is mainly about class structure and method placement, which is exactly why it fits a Java OOP category so well.

Best Answers

java - Approach 1
class Book {
    private String title;
    private boolean checkedOut;

    public Book(String title, boolean checkedOut) {
        this.title = title;
        this.checkedOut = checkedOut;
    }

    public String getStatusMessage() {
        if (checkedOut) {
            return title + " is checked out";
        }
        return title + " is available";
    }
}

class Solution {
    public static String checkBookStatus(String title, boolean checkedOut) {
        return new Book(title, checkedOut).getStatusMessage();
    }
}