AlgoMaster Logo
AlgoMasterDesign Library Bookeasy

Design Library Book

easy

Design a Book class that represents one physical library copy. Its title, author, and ISBN identify the book, while its availability changes as people borrow and return it. A copy is always in one of two states: available on the shelf or currently borrowed.

Implement the Book class:

  • Book(String title, String author, String isbn) creates a copy with the given details. Every new copy starts available.
  • boolean borrow() checks out an available copy, changes its state to borrowed, and returns true. If the copy is already borrowed, leave it unchanged and return false.
  • boolean returnBook() returns a borrowed copy to the shelf, changes its state to available, and returns true. If the copy is already available, leave it unchanged and return false.
  • boolean isAvailable() returns true when the copy is on the shelf and false when it is borrowed. It does not change the state.
  • String getInfo() returns the copy's current details in the exact format "<title> by <author> (ISBN: <isbn>) - <status>". Use Available or Borrowed for <status> based on the state at the time of the call.

For example, an available copy of Clean Code by Robert Martin with ISBN 978-0132350884 returns "Clean Code by Robert Martin (ISBN: 978-0132350884) - Available".

Keep the book's state encapsulated. Callers may borrow, return, or inspect the copy, but they must not be able to assign its availability directly.

Example 1:

Input:

Output:

Explanation:

  • Book book = new Book("The Pragmatic Programmer", "David Thomas", "978-0135957059"); // starts available
  • book.getInfo(); // returns "... - Available"
  • book.borrow(); // returns true, the book is now checked out
  • book.getInfo(); // returns "... - Borrowed"
  • book.borrow(); // returns false, it is already checked out, nothing changes
  • book.returnBook(); // returns true, the book is back on the shelf
  • book.getInfo(); // returns "... - Available"
Example 2:

Input:

Output:

Explanation: Returning a book that was never borrowed fails and leaves it on the shelf. Borrowing then succeeds and it is no longer available.

Constraints:
  • 1 <= title.length <= 100
  • 1 <= author.length <= 60
  • 1 <= isbn.length <= 20
  • At most 100 calls will be made across all methods.

How the design is graded

needs 7/10 to pass
  • Encapsulation

    Full marks when title, author, isbn and the availability flag are private (or the language's closest equivalent) and no method lets a caller set availability directly. Lose points for public mutable state or a setAvailable-style method that bypasses the borrow and return rules.

  • State transitions

    Full marks when borrow and return each check the current state first and a rejected operation leaves the book exactly as it was. Lose points when a failed operation still mutates state, when the two operations can both succeed from the same state, or when the guard is written around specific test values rather than the rule.

  • Structure and naming

    Full marks for one cohesive class with descriptive names and no dead code. Lose points for unused fields, printing to stdout instead of returning, or logic unrelated to a library book.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new Book("The Pragmatic Programmer", "David Thomas", "978-0135957059")null
getInfo()"The Pragmatic Programmer by David Thomas (ISBN: 978-0135957059) - Available"
borrow()true
getInfo()"The Pragmatic Programmer by David Thomas (ISBN: 978-0135957059) - Borrowed"
borrow()false
returnBook()true
getInfo()"The Pragmatic Programmer by David Thomas (ISBN: 978-0135957059) - Available"

The book starts available. The first borrow succeeds. The second fails because it is already out. Returning it makes it available again.

Run checks these cases. Submit also runs a larger hidden set.