Example #1
0
class BookList():
    """
    This class encapsulates the book list and adds easy to use methods for manipulating it
    """
    def __init__(self):
        self.bookList = list()
        self.validator = Validator()
    
    def addNewBook(self, title, author, description):
        """
        Adds a new book to the book list
        All the parameters are strings
        Returns an error list generated by the Validator 
        O(1)
        """
        book = Book(title, author, description)
        errorList = self.validator.validateBook(book) 
        if errorList != []:
            return errorList
        self.bookList.append(book)
        
        
    def removeBook(self, book, p):
        """
        Public method that overwrites the one in BookList
        Extra functionality: Writes the entire list to the file
        Best case O(1)
        Worst case O(n)
        Average case O(n)
        """
        if self.bookList[p] == book:
            self.bookList.pop(p)
            return True
        else:
            ret = self.removeBook(book, p+1)
        if ret != True:
            return False
        else:
            return True
    
    def updateBook(self, oldTitle, oldAuthor, oldDescription, \
                   newTitle, newAuthor, newDescription):
        """
        Replaces a book from the list with a new book
        All the parameters are strings
        Returns False if the book is not found, True otherwise
        O(n)
        """
        book = Book(oldTitle, oldAuthor, oldDescription)
        newBook = Book(newTitle, newAuthor, newDescription)
        found = False
        for bookInList in reversed(self.bookList):
            if bookInList == book:
                found = True
                self.bookList[self.bookList.index(bookInList)] = newBook
        return found
    
    def numberOfBooks(self):
        """
        Returns the number of books from the list
        O(1)
        """
        return len(self.bookList)
    
    def lastAddedBook(self):
        """
        Returns the instance of the latest added book
        O(1)
        """
        return self.bookList[len(self.bookList) - 1]
    
    def bookInList(self, title, author, description):
        """
        Returns a list of book instances that have the passed details
        All the parameters are strings
        O(n)
        """
        book = Book(title, author, description)
        booksFound = []
        for bookInList in reversed(self.bookList):
            if bookInList == book:
                booksFound.append(bookInList)
        return booksFound
    
    def findBook(self, query):
        """
        Searches for a book in the list
        query is a string
        Returns a list of found books
        O(n)
        """
        booksFound = list()
        for bookInList in reversed(self.bookList):
            if bookInList.getAuthor() == query or bookInList.getTitle() == query or bookInList.getDescription() == query:
                booksFound.append(bookInList)
        return booksFound
    
    def orderBooks(self, direction):
        """
        Returns a sorted list of books by popularity based on the direction passed
        direction can be either "asc" or "desc"
        O(n)
        """
        reverse = True
        if direction == "asc":
            reverse = False
        return sorted(self.bookList, key=lambda book:book.getPopularity(), reverse=reverse)
    
    def lendBook(self, books, client):
        """
        Sets a book's borrowed property to True
        books is a list of Book instances
        client is an instance of a Client
        returns the instance of the book that was set or False if all the books are borrowed
        O(n)
        """
        for book in books:
            if not book.isBorrowed():
                book.setBorrowed(True)
                return book
        return False
    
    def returnBook(self, books, client):
        """
        Sets a book's borrowed property to False
        books is a list of Book instances
        client is an instance of a Client
        Returns the instance of the book that was set or False if all the books are borrowed
        O(n)
        """
        for book in books:
            if book.isBorrowed():
                book.setBorrowed(False)
                return book
        return False
    
    def __str__(self):
        bookList = ""
        for book in self.bookList:
            bookList += str(book) + '\n'
        return bookList