Kotlinlearncs.online LogoJava
Return to List

Solve: Library Class

Created By: Pranav Narayanan
/ Version: 2024.10.0

Write a class Library that keep track of a library catalog. This class must have the following methods:

addBook: Takes a Book object and adds it to the library catalog. It should be available after being added. If the book is already in the catalog, this method should mark it as available. This method should not return any value.

removeBook: Takes a Book object and removes it from the catalog. If it is not in the catalog, return false. Return true if successfully removed.

isAvailable: Takes a Book object and returns true if it is currently available. If it is in the catalog but not currently available, return false. If it is not in the catalog, throw an IllegalArgumentException.

borrowBook: Takes a Book object and attempts to check it out. If successfully checked out, you should mark it as unavailable and return true. If it is in the catalog but not available, return false. If it is not in the catalog, throw an IllegalArgumentException.

returnBook: Takes a Book object and attempts to return it to the library. If successfully returned, mark it as available and return true. If it is already available, return false. If it is not in the catalog, throw an IllegalArgumentException.

You may use the Book class here as it is predefined. You may use the equality operator == with Books. However, if you use a Map to maintain the catalog, it will correctly check Book equality automatically.

Related Lessons

Stuck? You may find these lessons helpful:

Report a Problem

Attribution must link to this page: https://www.learncs.online/solve/kotlin/library-class/[email protected]