Example #1
0
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