def searchClients(self, command, characteristic): ''' Function that searches for a client after a given characteristic Input: command, characteristic Precondition: command - integer characteristic - string Output: - Postcondition: - ''' new_client = Client() clients = [] cnt = 0 i = 0 while i < len(self.__repoClients): search = self.__repoClients.searchClient(characteristic, command, i) new_client = search[0] if new_client.get_client_id() > -1: cnt += 1 i = search[1] clients.append(new_client) i += 1 if cnt == 0: raise ValueError("This client couldn't be found") return clients
def setUp(self): self.__bookId = 5 self.__title = "Fratii Karamazov" self.__author = "Dostoievski" self.__description = "pam pam" self.__clientId = 7 self.__name = "Ion Iliescu" self.__rentalId = 16 self.__rentedDate = 20 self.__dueDate = 13 self.__returnedDate = 30 self.__book = Book(self.__bookId, self.__title, self.__description, self.__author) self.__client = Client(self.__clientId, self.__name) self.__rental = Rental(self.__rentalId, self.__bookId, self.__clientId, self.__rentedDate, self.__dueDate, self.__returnedDate) self.__repoBook = RepoBook() self.__repoClient = RepoClient() self.__undoList = UndoRedo() self.__repoRental = RepoRental(self.__repoBook, self.__repoClient) self.__validBook = BookValidator() self.__validClient = ClientValidator() self.__validRental = RepoValidator(self.__repoBook, self.__repoClient) self.__bookService = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.__clientService = ServiceClient(self.__repoClient, self.__validClient, self.__undoList) self.__rentalService = ServiceRental(self.__repoBook, self.__repoClient, self.__validRental, self.__repoRental, self.__undoList)
def __removeClients(self): try: client = self.serviceRental.removeAparition( self.idtf.get(), "client") client = Client(self.idtf.get(), self.nametf.get()) messagebox.showinfo("Removed", "Element %s was removed" % client.get_name()) print except Exception as e: messagebox.showinfo("Error", "Error removing element - " + str(e))
def __storePressedClients(self): """ Handler method for store button pressed Store the book Show error messages on exceptions """ try: st = self.serviceClients.addNewClient(self.idtf.get(), self.nametf.get()) st = Client(self.idtf.get(), self.nametf.get()) messagebox.showinfo("Stored", "Element %s saved.." % st.get_name()) print except Exception as e: messagebox.showinfo("Error", "Error saving element - " + str(e))
def removeElement(self, element, command): ''' Function that removes a given element from the list if it existed in the list before Input: element Precondition: - Output: - Postcondition: - ''' #if element not in self.__list: #raise ValueError("inexisting element") # i = 0 element = int(element) if command == "1": element = Book(element) index = self.__list.searchElement1(element) del self.__list[index] #while i < len(self.__list): # if self.__list[i].get_book_id() == element: # del self.__list[i] # i -= 1 # i += 1 elif command == "2": element = Client(element) index = self.__list.searchElement1(element) del self.__list[index]
def testBusiness(self): self.__repoBook = RepoBook() self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.assertEquals(len(self.__business),0) self.__business.addNewBook(self.__bookId, self.__title, self.__author, self.__description) self.assertEquals(len(self.__business), 1) self.__business.addNewBook(15, "GoT", "George RR Martin", "abcd") self.assertEquals(len(self.__business), 2) self.__repoClient = RepoClient() self.__business = ServiceClient(self.__repoClient, self.__validClient, self.__undoList) self.__business.addNewClient(self.__clientId, self.__name) self.assertEquals(len(self.__business), 1) self.__business.addNewClient(5, "Nicolae Ceausescu") self.assertEquals(len(self.__business), 2) element = Client(5, "Traian Basescu") self.__repoClient.updateElement(element, 0) search = self.__repoClient.searchElement(5, 2) self.assertEquals(search.get_name(), "Traian Basescu") self.__business.removeClient(5, "Nicolae Ceausescu") self.assertEquals(len(self.__business), 1) self.__repoBook = RepoBook() self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.__business.addNewBook(23, "De veghe in lanul de secara", "JD Salinger", "Roman") self.__business.addNewBook(15, "Martianul", "Andy Weir", "I-a placut lui Leo") self.__business.removeBook(15, 0) self.assertEquals(len(self.__business), 1) element = Book(15, "1984", "George Orwell", "Tot lui Leo i-a placut") self.__repoBook.addElement(element, 0) element = Book(15, "1984", "Pam pam", "Tot lui Leo i-a placut") self.__repoBook.updateElement(element, 0) search = self.__repoBook.searchElement(15, 1) self.assertEquals(search.get_author(), "Pam pam")
def validRepo(self, element): errors = "" book = element.get_book_id() client = element.get_client_id() search = self.__bookRepo.searchElement1(Book(book)) if search == -1: errors += "this book doesn't exist\n" search = self.__clientRepo.searchElement1(Client(client)) if search == -1: errors += "this client doesn't exist\n" if len(errors) > 0: raise RepoError(errors)
def testRepository(self): self.assertEquals(len(self.__repoBook), 0) self.__repoBook.addElement(self.__book, 0) self.assertEquals(len(self.__repoBook), 1) new_book = Book(3, "Ion", "glasul pamantului/glasul iubirii", "Liviu Rebreanu") self.__repoBook.addElement(new_book, 0) self.assertEquals(len(self.__repoBook), 2) try: self.__repoBook.addElement(self.__book, 0) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "existing element") self.assertEquals(len(self.__repoClient), 0) self.__repoClient.addElement(self.__client, 0) self.assertEquals(len(self.__repoClient), 1) new_client = Client(2, "Justin Trudeau") self.__repoClient.addElement(new_client, 0) self.assertEquals(len(self.__repoClient), 2) try: self.__repoClient.addElement(self.__client, 0) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "existing element") self.__repoClient.removeElement(new_client.get_client_id()) self.assertEquals(len(self.__repoClient), 1) try: self.__repoClient.removeElement(new_client.get_client_id()) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "inexisting element") #self.__repo.printBooks() new_book = Book(3, "HP", "JK Rowling", "magic") self.__repoBook.updateElement(new_book, 0) cnt = self.__repoBook.searchElement1(new_book) cnt = int(cnt)
def searchElement(self, element, command): ''' Function that searches for an element in the given list Input: element - the id of the element after which the search is made command - the type of the element Precondition: element, command - integers Output: the element if it was found or -1 otherwise Postcondition:- ''' element = int(element) if command == 1: element = Book(element) else: element = Client(element) for i in range(0, len(self.__list)): if self.__list[i] == element: return self.__list[i] return -1
def removeElement(self, element, param): ''' Function that removes a given element from the list if it existed in the list before Input: element Precondition: - Output: - Postcondition: - ''' element = int(element) element = Client(element) if element not in self.__list: raise ValueError("inexisting element") i = 0 while i < len(self.__list): if self.__list[i] == element: del self.__list[i] i -= 1 i += 1
def addNewClient(self, clientId, name, undo=True): ''' Function that adds a new client in the library list Input: clientId, name Precondition: clientId - integer name - string Output: - Postcondition: - ''' clientId = int(clientId) new_client = Client(clientId, name) self.__valid.validClient(new_client) self.__repoClients.addElement(new_client, 0) index = 1 if undo == True: self.__undoService.addNewOperation(self.__repoClients.addElement, self.removeClient, [new_client, clientId], index)
def updateClient(self, clientId, name, numberOfDays, undo=True): ''' Function that updates a given client Input: clientId, name, numberOfDays Precondition: clientId, numberOfDays - integers name - string Output: - Postcondition: - ''' clientId = int(clientId) old_client = self.__repoClients.searchElement(clientId, 2) new_client = Client(clientId, name, numberOfDays) self.__repoClients.updateElement(new_client, 0) index = 1 if undo == True: self.__undoService.addNewOperation( self.__repoClients.updateElement, self.__repoClients.updateElement, [clientId, old_client], index)
def searchClient(self, element, command, index): ''' Function that searches for a client after a given characteristic Input: element - the characteristic command - the category in which the search is made(id, name) index - the position from which the search should start Precondition: element - string command, index - integers Output: (new_client, i) - a touple Postcondition: new_client - the element found after the search i - the position of the found element ''' new_client = Client(-1) command = int(command) if command == 1: for i in range(index, len(self.__list)): if self.partialString(self.__list[i].get_client_id(), element): return (self.__list[i], i) return (new_client, -1) elif command == 2: for i in range(index, len(self.__list)): if self.partialString(self.__list[i].get_name(), element): return (self.__list[i], i) return (new_client, -1)
def removeAparition(self, bookId, command, undo=True): if command == "book": bookId = int(bookId) book = self.__repoBooks.searchElement(bookId, 1) new_book_Id = book.get_book_id() new_book_title = book.get_title() new_book_author = book.get_author() new_book_description = book.get_description() new_book_number_of_rentals = book.get_number_of_rentals() new_book_number_of_days = book.get_number_of_days() new_book = Book(new_book_Id, new_book_title, new_book_author, new_book_description, new_book_number_of_rentals, new_book_number_of_days) i = 2 rental = self.__repo.findRentalId(bookId, "book") self.__repoBooks.removeElement(bookId) self.__undoService.addNewOperation(self.__repoBooks.removeElement, self.__repoBooks.addElement, [bookId, new_book], i) if type(rental) != int: index = int(self.__repo.searchElement1(rental)) while index != -1: old_rental_id = rental.get_rental_id() old_rental_book_id = rental.get_book_id() old_rental_client_id = rental.get_client_id() old_rental_rented_date = rental.get_rented_date() old_rental_due_date = rental.get_due_date() old_rental_returned_date = rental.get_returned_date() self.__repo.removeElementInCascade(index) old_rental = Rental(old_rental_id, old_rental_book_id, old_rental_client_id, old_rental_rented_date, old_rental_due_date, old_rental_returned_date) self.__undoService.addNewOperation(self.removeAparition, self.__repo.addRental, [0, old_rental], i) i += 1 rental = self.__repo.findRentalId(bookId, "book") if type(rental) != int: index = int(self.__repo.searchElement1(rental)) else: index = -1 elif command == "client": i = 2 bookId = int(bookId) client = self.__repoClients.searchElement(bookId, 2) new_client_Id = client.get_client_id() new_client_name = client.get_name() new_client_number_of_days = client.get_number_of_days() new_client = Client(new_client_Id, new_client_name, new_client_number_of_days) rental = self.__repo.findRentalId(bookId, "client") self.__repoClients.removeElement(bookId) self.__undoService.addNewOperation( self.__repoClients.removeElement, self.__repoClients.addElement, [bookId, new_client], i) if type(rental) != int: index = int(self.__repo.searchElement1(rental)) while index != -1: old_rental_id = rental.get_rental_id() old_rental_book_id = rental.get_book_id() old_rental_client_id = rental.get_client_id() old_rental_rented_date = rental.get_rented_date() old_rental_due_date = rental.get_due_date() old_rental_returned_date = rental.get_returned_date() self.__repo.removeElementInCascade(index) rental = Rental(old_rental_id, old_rental_book_id, old_rental_client_id, old_rental_rented_date, old_rental_due_date, old_rental_returned_date) self.__undoService.addNewOperation(self.removeAparition, self.addNewRental, [0, rental], i) i += 1 rental = self.__repo.findRentalId(bookId, "client") if type(rental) != int: index = int(self.__repo.searchElement1(rental)) else: index = -1
def removeElementUndo(self, element): element = int(element) element = Client(element) index = self.__list.find
class Tests(unittest.TestCase): def setUp(self): self.__bookId = 5 self.__title = "Fratii Karamazov" self.__author = "Dostoievski" self.__description = "pam pam" self.__clientId = 7 self.__name = "Ion Iliescu" self.__rentalId = 16 self.__rentedDate = 20 self.__dueDate = 13 self.__returnedDate = 30 self.__book = Book(self.__bookId, self.__title, self.__description, self.__author) self.__client = Client(self.__clientId, self.__name) self.__rental = Rental(self.__rentalId, self.__bookId, self.__clientId, self.__rentedDate, self.__dueDate, self.__returnedDate) self.__repoBook = RepoBook() self.__repoClient = RepoClient() self.__undoList = UndoRedo() self.__repoRental = RepoRental(self.__repoBook, self.__repoClient) self.__validBook = BookValidator() self.__validClient = ClientValidator() self.__validRental = RepoValidator(self.__repoBook, self.__repoClient) self.__bookService = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.__clientService = ServiceClient(self.__repoClient, self.__validClient, self.__undoList) self.__rentalService = ServiceRental(self.__repoBook, self.__repoClient, self.__validRental, self.__repoRental, self.__undoList) def testModels(self): self.assertEquals(self.__book.get_book_id(), self.__bookId) self.assertEquals(self.__client.get_name(), self.__name) self.assertEquals(self.__rental.get_rental_id(), self.__rentalId) #self.assertEquals self.__rental.get_number_of_rentals() == self.__numberOfRentals def testRepository(self): self.assertEquals(len(self.__repoBook), 0) self.__repoBook.addElement(self.__book, 0) self.assertEquals(len(self.__repoBook), 1) new_book = Book(3, "Ion", "glasul pamantului/glasul iubirii", "Liviu Rebreanu") self.__repoBook.addElement(new_book, 0) self.assertEquals(len(self.__repoBook), 2) try: self.__repoBook.addElement(self.__book, 0) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "existing element") self.assertEquals(len(self.__repoClient), 0) self.__repoClient.addElement(self.__client, 0) self.assertEquals(len(self.__repoClient), 1) new_client = Client(2, "Justin Trudeau") self.__repoClient.addElement(new_client, 0) self.assertEquals(len(self.__repoClient), 2) try: self.__repoClient.addElement(self.__client, 0) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "existing element") self.__repoClient.removeElement(new_client.get_client_id()) self.assertEquals(len(self.__repoClient), 1) try: self.__repoClient.removeElement(new_client.get_client_id()) self.assertEquals(False) except ValueError as re: self.assertEquals(str(re), "inexisting element") #self.__repo.printBooks() new_book = Book(3, "HP", "JK Rowling", "magic") self.__repoBook.updateElement(new_book, 0) cnt = self.__repoBook.searchElement1(new_book) cnt = int(cnt) def testBusiness(self): self.__repoBook = RepoBook() self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.assertEquals(len(self.__business), 0) self.__business.addNewBook(self.__bookId, self.__title, self.__author, self.__description) self.assertEquals(len(self.__business), 1) self.__business.addNewBook(15, "GoT", "George RR Martin", "abcd") self.assertEquals(len(self.__business), 2) self.__repoClient = RepoClient() self.__business = ServiceClient(self.__repoClient, self.__validClient, self.__undoList) self.__business.addNewClient(self.__clientId, self.__name) self.assertEquals(len(self.__business), 1) self.__business.addNewClient(5, "Nicolae Ceausescu") self.assertEquals(len(self.__business), 2) element = Client(5, "Traian Basescu") self.__repoClient.updateElement(element, 0) search = self.__repoClient.searchElement(5, 2) self.assertEquals(search.get_name(), "Traian Basescu") self.__business.removeClient(5, "Nicolae Ceausescu") self.assertEquals(len(self.__business), 1) self.__repoBook = RepoBook() self.__business = ServiceBook(self.__repoBook, self.__validBook, self.__undoList) self.__business.addNewBook(23, "De veghe in lanul de secara", "JD Salinger", "Roman") self.__business.addNewBook(15, "Martianul", "Andy Weir", "I-a placut lui Leo") self.__business.removeBook(15, 0) self.assertEquals(len(self.__business), 1) element = Book(15, "1984", "George Orwell", "Tot lui Leo i-a placut") self.__repoBook.addElement(element, 0) element = Book(15, "1984", "Pam pam", "Tot lui Leo i-a placut") self.__repoBook.updateElement(element, 0) search = self.__repoBook.searchElement(15, 1) self.assertEquals(search.get_author(), "Pam pam") def testRent(self): pass