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
class ClientList(): """ This class encapsulates the client list and adds easy to use methods for manipulating it """ def __init__(self): self.clientList = list() self.validator = Validator() def addNewClient(self, name, cnp): """ Adds a new client to the client list All the parameters are strings Returns the error list generated by the Validator O(n) """ client = Client(name, cnp) errorList = self.validator.validateClient(self.clientList, client) if errorList != []: return errorList self.clientList.append(client) def removeClient(self, name, cnp): """ Removes a client from the list All the parameters are strings Returns False if the client is not found, True otherwise O(n) """ client = Client(name, cnp) found = False for clientInList in reversed(self.clientList): if clientInList == client: found = True self.clientList.pop(self.clientList.index(clientInList)) return found def updateClient(self, oldName, oldCnp, newName, newCnp): """ Replaces a client from the list with a new client All the parameters are strings Returns False if the client is not found, True otherwise O(n) """ client = Client(oldName, oldCnp) newClient = Client(newName, newCnp) found = False for clientInList in reversed(self.clientList): if clientInList == client: found = True self.clientList[self.clientList.index(clientInList)] = newClient return found def borrowBook(self, book, client): """ Adds a Book instance to a client's borrowed book list book is an instance of a Book O(1) """ client.addBorrowedBook(book) def returnBook(self, book, client): """ Removes a Book instance from a client's borrowed book list book is an instance of a Book client is an instance of a Client O(n) """ client.removeBorrowedBook(book) def numberOfClients(self): """ Returns the number of clients from the list O(1) """ return len(self.clientList) def lastAddedClient(self): """ Returns the instance of the latest added client O(1) """ return self.clientList[len(self.clientList) - 1] def clientInList(self, name, cnp): """ Returns the client object from the list, which has the passed details name and cnp are strings Returns False if no clients were found Best case O(1) Worst case O(n) Average case O(n) """ client = Client(name, cnp) for clientInList in reversed(self.clientList): if clientInList == client: return clientInList return False def findClient(self, query): """ Searches for books in the client list Returns a list with instances of all the found clients query is a string O(n) """ foundClients = list() for clientInList in reversed(self.clientList): if clientInList.getName() == query or clientInList.getCnp() == query: foundClients.append(clientInList) return foundClients def orderClients(self, direction): """ Returns a client list ordered by activity Direction is set by the parameter direction which can be asc or desc O(n) """ reverse = True if direction == "asc": reverse = False return sorted(self.clientList, key=lambda client:client.getActivity(), reverse=reverse) def __str__(self): clientList = "" for client in self.clientList: clientList += str(client) + '\n' return clientList
def __init__(self): self.clientList = list() self.validator = Validator()
def __init__(self): self.bookList = list() self.validator = Validator()