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) """ BookList.__init__(self) self.__filename = filename try: self.__bookFile = open(self.__filename, "r") except IOError: # File does not exist. Create it. self.__bookFile = open(self.__filename, "w") self.__bookFile.close() self.__bookFile = open(self.__filename, "r") line = "Nonempty String" while (line != ""): line = self.__bookFile.readline() if (line == "" or line == "\n"): break details = line.split("|") book = Book(details[0].strip(), details[1].strip(), details[2].strip()) if details[3] == "True": book.setBorrowed(True) book.setPopularity(int(details[4])) self.bookList.append(book) self.__bookFile.close()
def addNewBook(self, title, author, description): """ Public method that overwrites the one in the BookList Extra functionality: Appends the newly added Book to the file O(1) """ self.bookFile = open(self.__filename, "a") book = Book(title, author, description) errorList = self.validator.validateBook(book) if errorList != []: return errorList self.bookList.append(book) self.bookFile.write(book.getTitle() + "|" + book.getAuthor() + "|" + book.getDescription() + "|" + \ "False" + "|" + "0" +"\n") self.bookFile.close()
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 updateBook(self, oldTitle, oldAuthor, oldDescription, \ newTitle, newAuthor, newDescription): """ Public method that overwrites the one in BookList Extra functionality: Writes the entire list to the file 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 self.__listToFile() return found
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 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 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, title, author, description): """ Controller Method to remove a book the parameters are all strings Returns False if the book was not found, True otherwise Best case O(1) Worst case O(n) Average case O(n) """ book = Book(title, author, description) if not self.__bookList.removeBook(book, 0): return False return True
def test_Book(): book = Book("title", "author", "description") assert book.getTitle() == "title" assert book.getAuthor() == "author" assert book.getDescription() == "description" book.setBorrowed(True) assert book.isBorrowed() == True book.setAuthor("new author") assert book.getAuthor() == "new author" book.setDescription(None) book.setTitle("new title") assert book.getTitle() == "new title" book.setPopularity(1) assert book.getPopularity() == 1 assert book.getDescription() == None book.setId(0)