def __init__(self, filename): """ Constructor method Opens the file and loads the entries in memory filename is a string, the path to the file used for storage O(n) """ ClientList.__init__(self) self.__filename = filename try: self.__clientFile = open(filename, "r") except IOError: # File does not exist. Create it. self.__clientFile = open(filename, "w") self.__clientFile.close() self.__clientFile = open(filename, "r") line = "Nonempty String" while line != "": line = self.__clientFile.readline() if line == "" or line == "\n": break details = line.split("|") client = Client(details[0], details[1]) if details[2] != "": borrowedBooks = details[2].split(";") for borrowedBook in borrowedBooks: if borrowedBook != "": bookDetails = borrowedBook.split() book = Book(bookDetails[0], bookDetails[1], bookDetails[2]) client.addBorrowedBook(book) client.setActivity(int(details[3])) self.clientList.append(client) self.__clientFile.close()
def test_Client(): client = Client("name", "cnp") client.setName("nname") assert client.getName() == "nname" assert client.getCnp() == "cnp" client.setCnp("0") assert client.getCnp() == "0" client.setId(0)
def addNewClient(self, name, cnp): """ Public method that overwrites the one in the ClientList Extra functionality: Appends the newly added Client to the file O(n) """ client = Client(name, cnp) self.__clientFile = open(self.__filename, "a") errorList = self.validator.validateClient(self.clientList, client) if errorList != []: return errorList self.clientList.append(client) self.__clientFile.write(client.getName() + "|" + client.getCnp() + "|" + "|" + "0" + "\n") self.__clientFile.close()
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 updateClient(self, oldName, oldCnp, newName, newCnp): """ Public method that overwrites the one in the ClientList Extra functionality: Appends the newly added Client to the file 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 self.__listToFile() return found
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 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 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 removeClient(self, name, cnp): """ Public method that overwrites the one in the ClientList Extra functionality: Appends the newly added Client to the file 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)) self.__listToFile() return found