def moviesPastDueDate(self, movieRepo): """ Get list of movies past due date """ rentalsPassedDueDate = List() constants = Constants() for rental in self.getRentalList(): if rental.getReturnedDate() is None and rental.getDueDate( ).isBeforeDate(self.__constants.currentDay()): rentalsPassedDueDate.append(rental) for i in range(0, len(rentalsPassedDueDate) - 1): for j in range(i + 1, len(rentalsPassedDueDate)): if constants.currentDay().daysUntilDate( rentalsPassedDueDate[i].getRentedDate( )) < constants.currentDay().daysUntilDate( rentalsPassedDueDate[j].getRentedDate()): aux = rentalsPassedDueDate[i] rentalsPassedDueDate[i] = rentalsPassedDueDate[j] rentalsPassedDueDate[j] = aux moviesPassedDueDateSorted = List() for rental in rentalsPassedDueDate: moviesPassedDueDateSorted.append( movieRepo.getMovieWithId(rental.getMovieId())) return moviesPassedDueDateSorted
def moviesMostRentedByDays(self, movieRepo): """ Get list of movies most rented by days rented """ constants = Constants() rentals = self.getRentalList() movies = movieRepo.getList() moviesDictionary = {} for movie in movies: moviesDictionary[movie.getId()] = 0 for rental in rentals: if rental.getMovieId() in moviesDictionary: if rental.getReturnedDate() is None: daysToAdd = constants.currentDay().daysUntilDate( rental.getRentedDate()) else: daysToAdd = rental.getRentedDate().daysUntilDate( rental.getReturnedDate()) moviesDictionary[rental.getMovieId()] += daysToAdd sortedMovies = sorted(moviesDictionary.items(), key=operator.itemgetter(1)) sortedMovies.reverse() sortedMovieList = List() for movie in sortedMovies: sortedMovieList.append(movieRepo.getMovieWithId(movie[0])) return sortedMovieList
def mostActiveClients(self, clientRepo): """ Get list of most active clients by days rented """ constants = Constants() rentals = self.getRentalList() clients = clientRepo.getList() clientsDictionary = {} for client in clients: clientsDictionary[client.getId()] = 0 for rental in rentals: if rental.getClientId() in clientsDictionary: if rental.getReturnedDate() is None: daysToAdd = constants.currentDay().daysUntilDate( rental.getRentedDate()) else: daysToAdd = rental.getRentedDate().daysUntilDate( rental.getReturnedDate()) clientsDictionary[rental.getClientId()] += daysToAdd sortedClients = sorted(clientsDictionary.items(), key=operator.itemgetter(1)) sortedClients.reverse() sortedClientList = List() for client in sortedClients: sortedClientList.append(clientRepo.getClientWithId(client[0])) return sortedClientList
def test_undoReturn(self): constants = Constants() self.undoRunner.addCommandToUndo(["return", "0", "0"], self.rentalController, self.stack, "rental", self.commandsStack) self.rentalController.returnMovieByClient(0, 0) self.assertEqual(self.rentalController.getRentalWithId(0).getReturnedDate(), constants.currentDay()) self.undoRunner.undo(self.clientController, self.movieController, self.rentalController, self.stack) self.assertIsNone(self.rentalController.getRentalWithId(0).getReturnedDate())
def setUp(self): self.movieRepo = MovieRepo() self.movieRepo.addMovie(MovieDAO("Titanic", "lovely", "Romance")) self.movieRepo.addMovie(MovieDAO("Avatar", "lovely1", "Romance1")) self.movieRepo.addMovie(MovieDAO("TestMovie", "lovely1", "Romance1")) self.movieRepo.addMovie(MovieDAO("Titanic", "lovely", "Romance")) self.movieRepo.addMovie(MovieDAO("Avatar", "lovely1", "Romance1")) self.movieRepo.addMovie(MovieDAO("TestMovie", "lovely1", "Romance1")) self.clientRepo = ClientRepo() self.clientRepo.addClient(ClientDAO("dani")) self.clientRepo.addClient(ClientDAO("ancu")) self.clientRepo.addClient(ClientDAO("dani1")) self.clientRepo.addClient(ClientDAO("ancu1")) self.clientRepo.addClient(ClientDAO("dani2")) self.clientRepo.addClient(ClientDAO("ancu2")) self.rentalRepo = RentalRepo() self.rentalRepo.addRental( RentalDAO(0, 0, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo)) self.rentalRepo.addRental( RentalDAO(1, 1, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo)) self.printer = Printer() self.constants = Constants() self.rentalController = RentalController(self.rentalRepo)
def clientHasMoviesNotReturned(self, clientId, rentalRepo): """ Checks whether client has movies not returned """ constants = Constants() for rental in rentalRepo.getList(): if rental.getClientId() == clientId: if rental.getReturnedDate() is None and rental.getDueDate().isBeforeDate(constants.currentDay()): return True return False
def returnMovieByClient(self, clientId, movieId): """ Return movie if rented by client """ movieFound = False for rental in self.getRentalList(): if rental.getMovieId() == movieId and rental.getClientId( ) == clientId and rental.getReturnedDate() is None: constants = Constants() rental.setReturnedDate(constants.currentDay()) self.updateRentalWithId(rental.getId(), rental) movieFound = True if not movieFound: raise MovieNotCurrentlyRentedByClientException
def returnOppositeCommand(self, parsedInputCommand, controller): # got "return" "client id" "movie id" oppositeCommand = ["rental", "returnedDateToNone"] constants = Constants() for rental in controller.getRentalList(): if rental.getMovieId() == int( parsedInputCommand[2]) and rental.getClientId() == int( parsedInputCommand[1] ) and rental.getReturnedDate() is None: oppositeCommand.append(str(rental.getId())) return oppositeCommand
def __init__(self, clientController, movieController, rentalController, undoStack, commandsStack, redoStack, undoRunner) -> None: self.printer = Printer() self.validator = Validator() self.constants = Constants() self.clientController = clientController self.movieController = movieController self.rentalController = rentalController self.undoStack = undoStack self.commandsStack = commandsStack self.redoStack = redoStack self.undoRunner = undoRunner
def test_return(self): printer = Printer() self.rentalController.getRepo().addRental( RentalDAO(3, 4, Date(5, 4, 2012), Date(7, 9, 2013), self.movieRepo, self.clientRepo)) printer.printList(self.rentalController.getRentalList()) with self.assertRaises(MovieNotCurrentlyRentedByClientException): self.rentalController.returnMovieByClient(2, 5) self.rentalController.returnMovieByClient(1, 1) rentalTest1 = RentalDAO(0, 0, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo) rentalTest1.setRentalId(0) rentalTest2 = RentalDAO(1, 1, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo) rentalTest2.setRentalId(1) constants = Constants() rentalTest2.setReturnedDate(constants.currentDay()) rentalTest3 = RentalDAO(3, 4, Date(5, 4, 2012), Date(7, 9, 2013), self.movieRepo, self.clientRepo) rentalTest3.setRentalId(2) self.assertEqual(self.rentalController.getRentalList(), [rentalTest1, rentalTest2, rentalTest3])
class Printer: """ Class that prints stuff to console. """ constant = Constants() def printMenu(self, menu): """ Print the menu, if it exists in Constants :param menu: the menu to print """ print(menu) print() for menuList in self.constant.getConstant(menu): print(menuList) print() print("Please choose the menu you want to use by inserting its command") def printSubmenu(self, submenu): """ Print the menu, if it exists in Constants :param submenu: the submenu to print """ print(submenu) print() for submenuList in self.constant.getConstant(submenu): print(submenuList) print() print("Command:") def printObject(self, plainObject): print(str(plainObject)) print() def printList(self, listToPrint): if len(listToPrint) == 0: print("empty list") for plainObject in listToPrint: self.printObject(plainObject) print()
class RentalController: __constants = Constants() def __init__(self, rentalRepo) -> None: self.__rentalRepo = rentalRepo def getRepo(self): return self.__rentalRepo def addRentalWithId(self, rental): self.__rentalRepo.addRentalWithId(rental) def addRental(self, rental): # TODO check if exceptions should be handled self.__rentalRepo.addRental(rental) def getRentalWithId(self, rentalId): return self.__rentalRepo.getRentalWithId(rentalId) def getRentalList(self): return self.__rentalRepo.getList() def removeRentalWithId(self, rentalId): self.__rentalRepo.removeRentalWithId(rentalId) def hasRentalWithId(self, rentalId): return self.__rentalRepo.hasRentalWithId(rentalId) def updateRentalWithId(self, rentalId, updatedRental): self.__rentalRepo.updateRentalWithId(rentalId, updatedRental) def rentMovieByClientUntilDate(self, clientId, movieId, dueDate, movieRepo, clientRepo): """ Create a new rental between client and movie from today until due date """ if dueDate.isBeforeDate(self.__constants.currentDay()): raise DatesNotOrderedException else: if self.__clientHasPassedDueDateMovies(clientId): raise ClientHasMoviesNotReturnedException else: if not self.__isMovieAvailable(movieId): raise MovieNotAvailableException else: self.addRental( RentalDAO(clientId, movieId, self.__constants.currentDay(), dueDate, movieRepo, clientRepo)) def returnMovieByClient(self, clientId, movieId): """ Return movie if rented by client """ movieFound = False for rental in self.getRentalList(): if rental.getMovieId() == movieId and rental.getClientId( ) == clientId and rental.getReturnedDate() is None: constants = Constants() rental.setReturnedDate(constants.currentDay()) self.updateRentalWithId(rental.getId(), rental) movieFound = True if not movieFound: raise MovieNotCurrentlyRentedByClientException def __clientHasPassedDueDateMovies(self, clientId): clientRentalList = List() for rental in self.getRentalList(): if rental.getClientId() == clientId: clientRentalList.append(rental) for rental in clientRentalList: if rental.getReturnedDate() is None and rental.getDueDate( ).isBeforeDate(self.__constants.currentDay()): return True return False def __isMovieAvailable( self, movieId ): # TODO check if have to check if movie exist (probably handled in ui) movieRentalList = List() for rental in self.getRentalList(): if rental.getMovieId() == movieId: movieRentalList.append(rental) if len(movieRentalList) == 0: # movie never rented return True for rental in movieRentalList: if rental.getReturnedDate() is None: return False return True def populateRepo(self, movieRepo, clientRepo): self.__rentalRepo.populateWithFew(movieRepo, clientRepo) def moviesMostRentedByTimesRented(self, movieRepo): """ Get list of movies most rented by times rented """ rentals = self.getRentalList() movies = movieRepo.getList() moviesDictionary = {} for movie in movies: moviesDictionary[movie.getId()] = 0 for rental in rentals: if rental.getMovieId() in moviesDictionary: moviesDictionary[rental.getMovieId()] += 1 sortedMovies = sorted(moviesDictionary.items(), key=operator.itemgetter(1)) sortedMovies.reverse() sortedMovieList = List() for movie in sortedMovies: sortedMovieList.append(movieRepo.getMovieWithId(movie[0])) return sortedMovieList def moviesMostRentedByDays(self, movieRepo): """ Get list of movies most rented by days rented """ constants = Constants() rentals = self.getRentalList() movies = movieRepo.getList() moviesDictionary = {} for movie in movies: moviesDictionary[movie.getId()] = 0 for rental in rentals: if rental.getMovieId() in moviesDictionary: if rental.getReturnedDate() is None: daysToAdd = constants.currentDay().daysUntilDate( rental.getRentedDate()) else: daysToAdd = rental.getRentedDate().daysUntilDate( rental.getReturnedDate()) moviesDictionary[rental.getMovieId()] += daysToAdd sortedMovies = sorted(moviesDictionary.items(), key=operator.itemgetter(1)) sortedMovies.reverse() sortedMovieList = List() for movie in sortedMovies: sortedMovieList.append(movieRepo.getMovieWithId(movie[0])) return sortedMovieList def mostActiveClients(self, clientRepo): """ Get list of most active clients by days rented """ constants = Constants() rentals = self.getRentalList() clients = clientRepo.getList() clientsDictionary = {} for client in clients: clientsDictionary[client.getId()] = 0 for rental in rentals: if rental.getClientId() in clientsDictionary: if rental.getReturnedDate() is None: daysToAdd = constants.currentDay().daysUntilDate( rental.getRentedDate()) else: daysToAdd = rental.getRentedDate().daysUntilDate( rental.getReturnedDate()) clientsDictionary[rental.getClientId()] += daysToAdd sortedClients = sorted(clientsDictionary.items(), key=operator.itemgetter(1)) sortedClients.reverse() sortedClientList = List() for client in sortedClients: sortedClientList.append(clientRepo.getClientWithId(client[0])) return sortedClientList def moviesCurrentlyRented(self, movieRepo): """ Get list of movie currently rented """ moviesRentedNow = List() for rental in self.getRentalList(): if rental.getReturnedDate() is None: moviesRentedNow.append( movieRepo.getMovieWithId(rental.getMovieId())) return moviesRentedNow def moviesPastDueDate(self, movieRepo): """ Get list of movies past due date """ rentalsPassedDueDate = List() constants = Constants() for rental in self.getRentalList(): if rental.getReturnedDate() is None and rental.getDueDate( ).isBeforeDate(self.__constants.currentDay()): rentalsPassedDueDate.append(rental) for i in range(0, len(rentalsPassedDueDate) - 1): for j in range(i + 1, len(rentalsPassedDueDate)): if constants.currentDay().daysUntilDate( rentalsPassedDueDate[i].getRentedDate( )) < constants.currentDay().daysUntilDate( rentalsPassedDueDate[j].getRentedDate()): aux = rentalsPassedDueDate[i] rentalsPassedDueDate[i] = rentalsPassedDueDate[j] rentalsPassedDueDate[j] = aux moviesPassedDueDateSorted = List() for rental in rentalsPassedDueDate: moviesPassedDueDateSorted.append( movieRepo.getMovieWithId(rental.getMovieId())) return moviesPassedDueDateSorted
class TestRentalController(TestCase): def setUp(self): self.movieRepo = MovieRepo() self.movieRepo.addMovie(MovieDAO("Titanic", "lovely", "Romance")) self.movieRepo.addMovie(MovieDAO("Avatar", "lovely1", "Romance1")) self.movieRepo.addMovie(MovieDAO("TestMovie", "lovely1", "Romance1")) self.movieRepo.addMovie(MovieDAO("Titanic", "lovely", "Romance")) self.movieRepo.addMovie(MovieDAO("Avatar", "lovely1", "Romance1")) self.movieRepo.addMovie(MovieDAO("TestMovie", "lovely1", "Romance1")) self.clientRepo = ClientRepo() self.clientRepo.addClient(ClientDAO("dani")) self.clientRepo.addClient(ClientDAO("ancu")) self.clientRepo.addClient(ClientDAO("dani1")) self.clientRepo.addClient(ClientDAO("ancu1")) self.clientRepo.addClient(ClientDAO("dani2")) self.clientRepo.addClient(ClientDAO("ancu2")) self.rentalRepo = RentalRepo() self.rentalRepo.addRental( RentalDAO(0, 0, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo)) self.rentalRepo.addRental( RentalDAO(1, 1, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo)) self.printer = Printer() self.constants = Constants() self.rentalController = RentalController(self.rentalRepo) def tearDown(self): self.movieRepo.clean() self.clientRepo.clean() self.rentalRepo.clean() def test_rent(self): printer = Printer() with self.assertRaises(DatesNotOrderedException): self.rentalController.rentMovieByClientUntilDate( 0, 0, Date(1, 1, 1999), self.movieRepo, self.clientRepo) self.rentalController.getRepo().addRental( RentalDAO(3, 4, Date(5, 4, 2012), Date(7, 9, 2013), self.movieRepo, self.clientRepo)) printer.printList(self.rentalController.getRepo().getList()) with self.assertRaises(ClientHasMoviesNotReturnedException): self.rentalController.rentMovieByClientUntilDate( 3, 3, Date(5, 5, 2025), self.movieRepo, self.clientRepo) with self.assertRaises(MovieNotAvailableException): self.rentalController.rentMovieByClientUntilDate( 0, 4, Date(5, 5, 2025), self.movieRepo, self.clientRepo) self.rentalController.rentMovieByClientUntilDate( 5, 5, Date(5, 5, 2025), self.movieRepo, self.clientRepo) # printer.printList(self.rentalController.getRepo().getList()) rentalTest1 = RentalDAO(0, 0, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo) rentalTest1.setRentalId(0) rentalTest2 = RentalDAO(1, 1, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo) rentalTest2.setRentalId(1) rentalTest3 = RentalDAO(3, 4, Date(5, 4, 2012), Date(7, 9, 2013), self.movieRepo, self.clientRepo) rentalTest3.setRentalId(2) rentalTest4 = RentalDAO(5, 5, self.constants.currentDay(), Date(5, 5, 2025), self.movieRepo, self.clientRepo) rentalTest4.setRentalId(3) self.assertEqual(self.rentalController.getRentalList(), [rentalTest1, rentalTest2, rentalTest3, rentalTest4]) with self.assertRaises(MovieNotAvailableException): self.rentalController.rentMovieByClientUntilDate( 5, 5, Date(5, 5, 2025), self.movieRepo, self.clientRepo) def test_return(self): printer = Printer() self.rentalController.getRepo().addRental( RentalDAO(3, 4, Date(5, 4, 2012), Date(7, 9, 2013), self.movieRepo, self.clientRepo)) printer.printList(self.rentalController.getRentalList()) with self.assertRaises(MovieNotCurrentlyRentedByClientException): self.rentalController.returnMovieByClient(2, 5) self.rentalController.returnMovieByClient(1, 1) rentalTest1 = RentalDAO(0, 0, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo) rentalTest1.setRentalId(0) rentalTest2 = RentalDAO(1, 1, Date(5, 4, 2018), Date(7, 9, 2020), self.movieRepo, self.clientRepo) rentalTest2.setRentalId(1) constants = Constants() rentalTest2.setReturnedDate(constants.currentDay()) rentalTest3 = RentalDAO(3, 4, Date(5, 4, 2012), Date(7, 9, 2013), self.movieRepo, self.clientRepo) rentalTest3.setRentalId(2) self.assertEqual(self.rentalController.getRentalList(), [rentalTest1, rentalTest2, rentalTest3])
class RentalRepo(Repository): __constants = Constants() def __init__(self) -> None: self.__rentalList = List() def hasRentalWithId(self, rentalId): """ Checks whether there is a rental in the repo with rentalId """ for rental in self.__rentalList: if rental.getId() == rentalId: return True return False def addRental(self, rental): if type(rental).__name__ == 'RentalDAO': if not RentalRepo.hasRentalWithId(self, rental.getId()): rental.setRentalId(self.__maximumIndexInRentalList() + 1) self.__rentalList.append(rental) # self.__sortRentalList() # sortListById(self.__rentalList) sortListByObjectAttribute( self.__rentalList, lambda a, b: True if a < b else False, lambda a: a.getId()) else: raise ObjectAlreadyInCollectionException else: raise TypeError def __maximumIndexInRentalList(self): maximumIndex = -1 for rental in self.__rentalList: if rental.getId() > maximumIndex: maximumIndex = rental.getId() return maximumIndex def getList(self): # caution use return self.__rentalList def getRentalWithId(self, rentalId): for rental in self.__rentalList: if rental.getId() == rentalId: return rental raise ObjectNotInCollectionException def removeRentalWithId(self, rentalId): """ Remove rental with rentalId from repo """ indexOfRentalToRemoveInList = -1 for i in range(0, len(self.__rentalList)): if (self.__rentalList[i]).getId() == rentalId: indexOfRentalToRemoveInList = i if indexOfRentalToRemoveInList == -1: raise ObjectNotInCollectionException else: del self.__rentalList[indexOfRentalToRemoveInList] def updateRentalWithId(self, rentalId, updatedRental): """ Update rental with rentalId to updatedRental """ indexOfRentalToUpdateInList = -1 for i in range(0, len(self.__rentalList)): if (self.__rentalList[i]).getId() == rentalId: indexOfRentalToUpdateInList = i if indexOfRentalToUpdateInList == -1: raise ObjectNotInCollectionException else: if updatedRental.getId() is None: updatedRental.setRentalId(rentalId) self.__rentalList[indexOfRentalToUpdateInList] = updatedRental def populateWithFew(self, movieRepo, clientRepo): self.addRental( RentalDAO(0, 0, Date(12, 5, 2011), Date(13, 6, 2012), movieRepo, clientRepo)) self.addRental( RentalDAO(1, 1, Date(12, 5, 2012), Date(13, 6, 2019), movieRepo, clientRepo)) self.__rentalList[1].setReturnedDate(Date(12, 6, 2013)) self.addRental( RentalDAO(2, 2, Date(12, 5, 2013), Date(13, 6, 2013), movieRepo, clientRepo)) self.__rentalList[2].setReturnedDate(Date(12, 6, 2014)) self.addRental( RentalDAO(3, 3, Date(12, 5, 2014), Date(13, 6, 2014), movieRepo, clientRepo)) self.__rentalList[3].setReturnedDate(Date(12, 6, 2015)) self.addRental( RentalDAO(4, 4, Date(12, 5, 2015), Date(13, 6, 2015), movieRepo, clientRepo)) self.__rentalList[4].setReturnedDate(Date(12, 6, 2016)) self.addRental( RentalDAO(5, 5, Date(12, 5, 2016), Date(13, 6, 2016), movieRepo, clientRepo)) self.__rentalList[5].setReturnedDate(Date(12, 6, 2017)) self.addRental( RentalDAO(6, 6, Date(12, 5, 2001), Date(13, 6, 2001), movieRepo, clientRepo)) self.__rentalList[6].setReturnedDate(Date(12, 6, 2002)) self.addRental( RentalDAO(7, 7, Date(12, 5, 2002), Date(13, 6, 2002), movieRepo, clientRepo)) self.__rentalList[7].setReturnedDate(Date(12, 6, 2003)) self.addRental( RentalDAO(8, 8, Date(12, 5, 2003), Date(13, 6, 2003), movieRepo, clientRepo)) self.__rentalList[8].setReturnedDate(Date(12, 6, 2004)) self.addRental( RentalDAO(9, 9, Date(12, 5, 2004), Date(13, 6, 2004), movieRepo, clientRepo)) self.addRental( RentalDAO(10, 10, Date(12, 5, 2000), Date(13, 6, 2000), movieRepo, clientRepo)) self.__rentalList[10].setReturnedDate(Date(12, 6, 2006)) def addRentalWithId(self, rental): self.__rentalList.append(rental) sortListByObjectAttribute(self.__rentalList, lambda a, b: True if a < b else False, lambda a: a.getId()) def clean(self): self.__rentalList = List()