# L-24 MCS 260 : class Catalog

"""
The Catalog object encapsulates a list.
"""

from classbook import Book

class Catalog(object):
    """
    The class Catalog imports the class Book.
    It represents the library's book collection.
    """
    def __init__(self):
        "sets the data attribute"
        self.collection = []

    def __str__(self):
        "returns the string representation"
        result = ""
        for book in self.collection:
            result += str(book) + '\n'
        return result

    def add(self, book):
        "adds the book to the catalog"
        self.collection.append(book)

    def search_on_key(self, key):
        """
        Returns the book with the key if it in
        the collection, else None is returned.
        """
        for book in self.collection:
            if book.key == key:
                return book
        return None

    def checkout(self, key):
        "Checks out the book with key."
        book = self.search_on_key(key)
        if book == None:
            print('no book with key = ', key)
        else:
            if not book.check():
                print(book)
            else:
                book.change()

    def checkin(self, key):
        "Checks in the book with key."
        book = self.search_on_key(key)
        if book == None:
            print('no book with key = ', key)
        elif isinstance(book, Book):
            if not book.check():
                book.change()

    def delete(self, key):
        "Deletes the book with key."
        for k in range(0, len(self.collection)):
            book = self.collection[k]
            if book.key == key:
                popped = self.collection.pop(k)
                print(popped)
                print('has been deleted')
                break
