def __init__(self, stringInput): self._args = stringInput.split('|') self._args[0] = self._args[0].lower() if not self._args[0] in Command.ARGS.keys(): raise LibraryException("CommandError - Unknown command!") if len(self._args) != Command.ARGS[self._args[0]]: raise LibraryException("CommandError - Argument size do not match!") for arg in self._args: if arg == "": raise LibraryException("CommandError - Empty parameters!")
def redo(self): ''' Function to handle the redo command ''' if self._now == len(self._states) - 1: raise LibraryException("Already at newest state!") else: self._now += 1
def returnBook(self, clientCNP, bookID): client = self.searchClient(clientCNP) book = self.searchBook(bookID) if not book in [loan.getBook() for loan in self.getLoans()]: raise LibraryException( "Book is not rented, how could you return it? Maybe a donation?" ) self.removeLoanByBood(bookID)
def undo(self): ''' Function to handle the undo command ''' if self._now == 0: raise LibraryException("Already at earliest state!") else: self._now -= 1
def removeClient(self, clientCNP): ''' Function to remove a Client :param clientCNP: the new client we want to remove :return: ''' for client in [loan.getClient() for loan in self._repo.getLoans()]: if client.getCnp() == clientCNP: raise LibraryException("Client has rented book, please return them and then remove him.") self._repo.removeClient(clientCNP)
def updateTitle(self, bookId, newBookTitle): ''' Function to update the Title of a book :param bookPack: a tuple where the first element is the id of the book and the second element is the new Title of the book ''' for book in self.getBooks(): if book.getId() == bookId: book.setTitle(newBookTitle) return raise LibraryException("Book not found!")
def updateAuthor(self, bookId, newBookAuthor): ''' Function to update the Author of a given book :param bookPack: a tuple where the first element is the id of the book and the second element is the new Author of the book ''' for book in self.getBooks(): if book.getId() == bookId: book.setAuthor(newBookAuthor) return raise LibraryException("Book not found!")
def saveHistory(self): ''' Function to save the whole application information in the history.bin file ''' try: with open("repository/history.bin", "wb") as f: pickle.dump(self, f) return "Successfully saved current state!" except IOError: raise LibraryException("Could not save the current state!")
def removeBook(self, bookId): ''' Function to remove a Book Raises exception if no book was found. :param bookId: :raise: TypeError if the given Book was not found in the Library ''' for book in [loan.getBook() for loan in self._repo.getLoans()]: if book.getId() == bookId: raise LibraryException("Book rented...") self._repo.removeBook(bookId)
def updateClientName(self, clientCnp, clientNewName): ''' Function to update the Name of a client. :param clientPack: a tuple where the first element is the clientCnp and the second one is the new name of the client :raise TypeError if there is no client with the given CNP ''' for client in self.getClients(): if client.getCnp() == clientCnp: client.setName(clientNewName) return raise LibraryException("Client not found!")
def searchBook(self, bookId): ''' Function to search for a book in the list :param bookId: the id of the book we want to search for :return: the unique given book (since the id is unique) :raise TypeError Exception if the book was not found in the array ''' for i in range(len(self._books)): book = self._books[i] if book.getId() == bookId: return book raise LibraryException("Book not found!")
def searchClient(self, clientCNP): ''' Function to search for a client by his CNP :param clientCNP: an integer representing the CNP of the client we want to search for :return: the unique Client (since the CNP is unique) :raise TypeError if there is no client with the given CNP. ''' for i in range(len(self._clients)): client = self._clients[i] if client.getCnp() == clientCNP: return client raise LibraryException("Client not found!")
def removeClient(self, clientCNP): ''' Function to remove a Client :param clientCNP: the new client we want to remove :return: ''' for i in range(len(self._clients)): client = self._clients[i] if client.getCnp() == clientCNP: del self._clients[i] return raise LibraryException("Client not found!")
def removeLoanByBood(self, bookId): ''' Function to remove a Loan by a book id Raises exception if no book was found in the Loans :param bookId: :return: LibraryException if the given BookId is not in the loan array ''' for i in range(len(self._loans)): loan = self._loans[i] if loan.getBook().getId() == bookId: del self._loans[i] return raise LibraryException("Book not found")
def removeBook(self, bookId): ''' Function to remove a Book Raises exception if no book was found. :param bookId: :raise: TypeError if the given Book was not found in the Library ''' for i in range(len(self._books)): book = self._books[i] if book.getId() == bookId: del self._books[i] return raise LibraryException("Book not found!")
def updateClientCnp(self, clientCnp, clientNewCnp): ''' Function to update the CNP of a client. :param clientPack: a tuple where the first element is the clientCnp and the second one is the new cnp of the client :raise TypeError if there is no client with the given CNP, or the newCNP already exist ''' try: self.searchClient(clientNewCnp) raise ValueError("Client CNP already exists!") except LibraryException: for client in self.getClients(): if client.getCnp() == clientCnp: client.setCnp(clientNewCnp) return raise LibraryException("Client not found!")
def getArg(self, pos): if pos >= self.getArgsSize(): raise LibraryException("Command error - Not enough parameters.") return self._args[pos]
def toRemoveClient(self): try: return int(self.getArg(1)) except ValueError as ve: raise LibraryException("Client CNP should be an integer.")
def toRemoveBook(self): try: return int(self.getArg(1)) except ValueError as ve: raise LibraryException("Book number should be an integer.")
def rentBook(self, clientCNP, bookID): clt = self.searchClient(clientCNP) bk = self.searchBook(bookID) if bk in [rental.getBook() for rental in self.getLoans()]: raise LibraryException("Book already rented to somebody!") self._loans.append(Loan(clt, bk))
def toUpdateName(self): try: return (int(self.getArg(1)), self.getArg(2)) except ValueError: raise LibraryException("Client CNP should be an integer.")
def toUpdateTitle(self): try: return (int(self.getArg(1)), self.getArg(2)) except ValueError: raise LibraryException("Book ID should be an integer.")