def setUp(self): self.repo = Repository() self.brepo = Repository() self.crepo = Repository() self.Ucontroller = UndoController() self.book = Book(21, 'titlu', 'descriere', 'author') self.book2 = Book(22, 'titlu2', 'descriere2', 'author') self.brepo.add(self.book) self.brepo.add(self.book2) self.client = Client(23, 'alex') self.client2 = Client(24, 'ana') self.client3 = Client(29, 'ana') self.crepo.add(self.client) self.crepo.add(self.client2) self.rental = Rental(21236, 21, 24, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental2 = Rental(21238, 22, 24, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental3 = Rental(21238, 23, 24, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental4 = Rental(21238, 21, 29, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental5 = Rental(21231, 21, 23, date(2017, 11, 5), date(2017, 10, 6), None) self.controller = RentalController(self.repo, self.brepo, self.crepo, self.Ucontroller) self.ceva = LateRentalCount(12, 32) self.altceva = BookRentalCount(12, 23) self.nu = AuthorRentalCount('da', 23) self.da = ClientRentalCount(32, 12)
def setUp(self): self.repo = Repository() self.rentalRepo = Repository() self.Ucontroller = UndoController() self.book = Book(21, 'titlu', 'descriere', 'author') self.book2 = Book(22, 'titlu2', 'descriere2', 'author2') self.controller = BookController(self.repo, self.Ucontroller, self.rentalRepo)
def setUp(self): self.repo = Repository() self.rentalRepo = Repository() self.Ucontroller = UndoController() self.client = Client(11, 'alex') self.client2 = Client(12, 'ana') self.controller = ClientController(self.repo, self.Ucontroller, self.rentalRepo)
def statisticsExample(): """ An example for the creation of statistics. Several cars, clients and rentals are created and then a statistics is calculated over them. Follow the code below and figure out how it works! """ undoController = UndoController() ''' Start client Controller ''' clientRepo = FileClientRepository() clientValidator = ClientValidator() clientController = ClientController(undoController, clientValidator, clientRepo) ''' Start car Controller ''' carRepo = FileCarRepository() carValidator = CarValidator() carController = CarController(undoController, carValidator, carRepo) ''' Start rental Controller ''' rentRepo = FileRentalRepository(clientRepo, carRepo) rentValidator = RentalValidator() rentController = RentalController(undoController, rentValidator, rentRepo, carRepo, clientRepo) ''' Print each statistics item ''' for cr in rentController.mostRentedCars(): print(cr)
def undoExampleMedium(): undoController = UndoController() clientRepo = Repository() carRepo = Repository() ''' Start rental Controller ''' rentRepo = Repository() rentValidator = RentalValidator() rentController = RentalController(undoController, rentValidator, rentRepo, carRepo, clientRepo) ''' Start client Controller ''' clientValidator = ClientValidator() clientController = ClientController(undoController, rentController, clientValidator, clientRepo) ''' Start car Controller ''' carValidator = CarValidator() carController = CarController(undoController, rentController, carValidator, carRepo) ''' We add 3 clients ''' clientSophia = clientController.create(103, "2990511035588", "Sophia") clientCarol = clientController.create(104, "2670511035588", "Carol") clientBob = clientController.create(105, "2590411035588", "Bob") printReposWithMessage("We added 3 clients", clientRepo, None, None) ''' We delete 2 of the clients ''' clientController.delete(103) clientController.delete(105) printReposWithMessage("Deleted Sophia and Bob", clientRepo, None, None) ''' We undo twice ''' undoController.undo() printReposWithMessage("1 undo, so Bob is back", clientRepo, None, None) undoController.undo() printReposWithMessage("Another undo, so Sophia is back too", clientRepo, None, None) ''' We redo once ''' undoController.redo() printReposWithMessage("1 redo, so Sophia is again deleted", clientRepo, None, None)
def main(): settings = readSettings() if settings["repository"] == "inmemory": undo = UndoController() rental = RentalController(RentalValidator(), Repository(), Repository(), Repository()) client = ClientController(ClientValidator(), Repository(), rental, undo) movie = MovieController(MovieValidator(), Repository(), rental, undo) ui = UI(client, movie, rental, undo) ui.addClients() ui.addMovies() ui.run() elif settings["repository"] == "textfiles": undo = UndoController() rental = RentalController(RentalValidator(), RentalTextFileRepo(), MovieTextFileRepo(), ClientTextFileRepo()) client = ClientController(ClientValidator(), ClientTextFileRepo(), rental, undo) movie = MovieController(MovieValidator(), MovieTextFileRepo(), rental, undo) ui = UI(client, movie, rental, undo) ui.run() elif settings["repository"] == "binaryfiles": undo = UndoController() rental = RentalController(RentalValidator(), RentalPickleFileRepo(), MoviePickleFileRepo(), ClientPickleFileRepo()) client = ClientController(ClientValidator(), ClientPickleFileRepo(), rental, undo) movie = MovieController(MovieValidator(), MoviePickleFileRepo(), rental, undo) ui = UI(client, movie, rental, undo) ui.writeToBinaryFile() ui.run() else: return
def get_controllers(student_repo, discipline_repo, grade_repo): """ Creates the controllers """ undo_controller = UndoController() student_controller = StudentControllerWithUndo(student_repo, undo_controller) discipline_controller = DisciplineControllerWithUndo( discipline_repo, grade_repo, undo_controller) grade_controller = GradeControllerWithUndo(grade_repo, student_repo, discipline_repo, undo_controller) return [ student_controller, discipline_controller, grade_controller, undo_controller ]
def initAll(): studentRepository, disciplineRep, gradeRepo = PersistencyHandler.obtainPersistencySource( ) undoController = UndoController() undoController._recorded = False studentValidator = StudentValidator() disciplineVal = DisciplineValidator() gradeValid = GradeValidator() gradeController = GradeControllerWithUndo(undoController, gradeRepo, gradeValid, studentRepository, disciplineRep) studentController = StudentControllerWithUndo(undoController, studentRepository, studentValidator, gradeController) disciplineController = DisciplineControllerWithUndo( undoController, disciplineRep, disciplineVal, gradeController) studentController.addStudent("1", "Ami") studentController.addStudent("80", "Bobo") studentController.addStudent("9", "Gicu") studentController.addStudent("121", "Marcel") studentController.addStudent("abc", "Dan") studentController.addStudent("7805", "Mercury") studentController.addStudent("a12", "Fred") studentController.addStudent("8", "Dennis") studentController.addStudent("9011", "Smith") studentController.addStudent("bx88", "Peter") # len = 10 disciplineController.addDiscipline("1", "Humanities") disciplineController.addDiscipline("5", "Geography") disciplineController.addDiscipline("9", "Religion") disciplineController.addDiscipline("ab22", "Archaeology") disciplineController.addDiscipline("3030", "Economics") disciplineController.addDiscipline("1010", "Medicine") disciplineController.addDiscipline("1bcs", "Philosophy") disciplineController.addDiscipline("964", "Arts") disciplineController.addDiscipline("135", "Literature") disciplineController.addDiscipline("aabb", "Mathematics") disciplineController.addDiscipline("19ac1", "Logic") disciplineController.addDiscipline("9347", "Statistics") disciplineController.addDiscipline("10046", "Agriculture") # len = 13 gradeController.enrollStudent("10046", "9011", [0]) gradeController.addGrade("10046", "9011", [2]) gradeController.addGrade("10046", "9011", [8]) gradeController.addGrade("10046", "9011", [10]) gradeController.enrollStudent("1", "1", [0]) gradeController.addGrade("1", "1", [6]) gradeController.addGrade("1", "1", [8]) gradeController.addGrade("1", "1", [2]) gradeController.addGrade("1", "1", [10]) gradeController.enrollStudent("5", "80", [0]) gradeController.addGrade("5", "80", [10]) gradeController.addGrade("5", "80", [9]) gradeController.enrollStudent("9347", "1", [0]) gradeController.addGrade("9347", "1", [4]) gradeController.addGrade("9347", "1", [3]) gradeController.enrollStudent("19ac1", "1", [0]) gradeController.addGrade("19ac1", "1", [3]) gradeController.addGrade("19ac1", "1", [3]) gradeController.addGrade("19ac1", "1", [3]) gradeController.enrollStudent("1", "bx88", [0]) gradeController.addGrade("1", "bx88", [10]) # len = 5 undoController._recorded = True return undoController, studentController, disciplineController, gradeController
def undoExampleHard(): undoController = UndoController() clientRepo = Repository() carRepo = Repository() ''' Start rental Controller ''' rentRepo = Repository() rentValidator = RentalValidator() rentController = RentalController(undoController, rentValidator, rentRepo, carRepo, clientRepo) ''' Start client Controller ''' clientValidator = ClientValidator() clientController = ClientController(undoController, rentController, clientValidator, clientRepo) ''' Start car Controller ''' carValidator = CarValidator() carController = CarController(undoController, rentController, carValidator, carRepo) ''' We add 2 clients ''' clientSophia = clientController.create(103, "2990511035588", "Sophia") clientCarol = clientController.create(104, "2670511035588", "Carol") ''' We add 2 cars ''' carHyundaiTucson = carController.create(201, "CJ 02 TWD", "Hyundai", "Tucson") carToyotaCorolla = carController.create(202, "CJ 02 FWD", "Toyota", "Corolla") printReposWithMessage("Added 2 clients and 2 cars", clientRepo, carRepo, None) ''' We delete 1 client and 1 car ''' clientController.delete(103) carController.delete(202) printReposWithMessage("Deleted Sophia and the Corolla", clientRepo, carRepo, None) ''' We undo twice ''' undoController.undo() printReposWithMessage("1 undo, the Corolla is back", clientRepo, carRepo, None) undoController.undo() printReposWithMessage("1 undo, Sophia is back", clientRepo, carRepo, None) ''' Redo twice ''' undoController.redo() undoController.redo() printReposWithMessage("2 redos, Sophia and the Corolla are again deleted", clientRepo, carRepo, None) ''' Last redo ''' undoController.redo() printReposWithMessage("1 redo - but there are no more redos", clientRepo, carRepo, None)
def setUp(self): self.val = ClientValidator() self.repo = ClientRepository() self.ctrl = ClientController(self.repo, UndoController(), self.val) self.cl = Client(1, "Pop Vasile", "1950621390064")
class TestClientController(unittest.TestCase): def setUp(self): self.repo = Repository() self.rentalRepo = Repository() self.Ucontroller = UndoController() self.client = Client(11, 'alex') self.client2 = Client(12, 'ana') self.controller = ClientController(self.repo, self.Ucontroller, self.rentalRepo) def test_something(self): self.assertRaises(Exception, self.Ucontroller.undo) self.assertRaises(Exception, self.Ucontroller.redo) self.Ucontroller.newOperation() self.controller.addClient(self.client) self.assertEqual(len(self.repo), 1) self.Ucontroller.newOperation() self.controller.removeClient(11) self.assertEqual(len(self.repo), 0) self.Ucontroller.newOperation() self.controller.addClient(self.client) self.Ucontroller.newOperation() self.controller.updateClient(self.client) self.Ucontroller.newOperation() self.controller.addClient(self.client2) self.assertEqual(len(self.controller.getAllClients()), 2) self.assertRaises(ControllerException, self.controller.searchClient, 'nu') self.assertRaises(ControllerException, self.controller.searchClient, '') self.assertEqual(len(self.controller.searchClient('a')), 2) self.Ucontroller.newOperation() self.controller.removeClient(12) self.Ucontroller.undo() self.assertEqual(len(self.controller.getAllClients()), 2) self.Ucontroller.redo() self.assertEqual(len(self.controller.getAllClients()), 1)
from controller.GradeController import GradeController from controller.StudentController import StudentController from controller.UndoController import UndoController from domain.validators import GradeValidator, StudentValidator from repository.GradeRepo import GradeRepo from repository.StudentRepo import StudentRepo if __name__ == '__main__': print("App started running.") grade_validator = GradeValidator() student_validator = StudentValidator() grade_repo = GradeRepo(grade_validator) student_repo = StudentRepo(student_validator) grade_controller = GradeController(grade_repo) student_controller = StudentController(student_repo) undo_controller = UndoController(student_controller, grade_controller) console = Console(student_controller, grade_controller, undo_controller) try: console.run() except Exception: print("An error accured.") traceback.print_exc() print("That's all folks.")
def undoTest(): ''' ''' undoController = UndoController() studentRepo = Repository() disciplineRepo = Repository() ''' Start grade Controller ''' gradeRepo = GradeRepository() gradeValidator = GradeValidator() gradeController = GradeControllerWithUndo(undoController, gradeRepo, gradeValidator, studentRepo, disciplineRepo) ''' Start student Controller ''' studentValidator = StudentValidator() studentController = StudentControllerWithUndo(undoController, studentRepo, studentValidator, gradeController) ''' Start discipline Controller ''' disciplineValidator = DisciplineValidator() disciplineController = DisciplineControllerWithUndo(undoController, disciplineRepo, disciplineValidator, gradeController) ####################################################### undoController.newOperation() studentA = studentController.addStudent("cc11", "Ion") undoController.newOperation() studentB = studentController.addStudent("ab133", "Ana") undoController.newOperation() studentC = studentController.addStudent("1", "George") undoController.newOperation() studentD = studentController.addStudent("2", "Mihai") ''' print("Len of Student Controller: ", len(studentController)) print(msg) print(studentController) undoController.undo() undoController.undo() undoController.undo() undoController.undo() print(msg) print("Len of Student Controller: ", len(studentController)) print(msg) undoController.redo() undoController.redo() undoController.redo() undoController.redo() print("Len of Student Controller: ", len(studentController)) print(msg) print(studentController) ''' undoController.newOperation() disciplineA = disciplineController.addDiscipline("1", "Math") undoController.newOperation() disciplineB = disciplineController.addDiscipline("2", "Physics") undoController.newOperation() disciplineC = disciplineController.addDiscipline("3", "Grammar") undoController.newOperation() disciplineD = disciplineController.addDiscipline("4", "C++") ''' print("Len of DisciplineController Controller: ", len(disciplineController)) print(msg) print(disciplineController) undoController.undo() undoController.undo() undoController.undo() undoController.undo() print(msg) print("Len of DisciplineController Controller: ", len(disciplineController)) print(msg) print(disciplineController) undoController.redo() undoController.redo() undoController.redo() undoController.redo() print(msg) print("Len of DisciplineController Controller: ", len(disciplineController)) print(disciplineController) ''' gradeController.enrollStudent("1", "cc11", [0]) gradeController.addGrade("1", "cc11", [7]) gradeController.addGrade("1", "cc11", [10]) gradeController.enrollStudent("2", "cc11", [0]) gradeController.addGrade("2", "cc11", [9]) gradeController.addGrade("2", "cc11", [2]) gradeController.enrollStudent("1", "ab133", [0]) gradeController.addGrade("1", "ab133", [10]) gradeController.addGrade("1", "ab133", [3]) gradeController.enrollStudent("1", "1", [0]) gradeController.addGrade("1", "1", [10]) gradeController.addGrade("1", "1", [3]) print(msg) print("Len of GradeController: ", len(gradeController)) print(gradeController) print(msg) undoController.newOperation() studentController.removeStudent("1") undoController.newOperation() studentController.removeStudent("ab133") print(msg) print(studentController) print(msg) print(msg) print("Len of GradeController: ", len(gradeController)) print(gradeController) undoController.undo() print(gradeController) undoController.undo() print(gradeController)
def undoExample(): """ An example for doing multiple undo operations. This is a bit more difficult than in Lab2-4 due to the fact that there are now several controllers, and each of them can perform operations that require undo support. Follow the code below and figure out how it works! """ undoController = UndoController() ''' Start client Controller ''' clientRepo = Repository() clientValidator = ClientValidator() clientController = ClientController(undoController, clientValidator, clientRepo) ''' Start car Controller ''' carRepo = Repository() carValidator = CarValidator() carController = CarController(undoController, carValidator, carRepo) ''' Start rental Controller ''' rentRepo = Repository() rentValidator = RentalValidator() rentController = RentalController(undoController, rentValidator, rentRepo, carRepo, clientRepo) print("---> Initial state of repositories") print(clientRepo) print(carRepo) print(rentRepo) ''' We add a client, a new car as well as a rental ''' clientController.create(103, "1900102035588", "Dale") carController.create(201, "CJ 02 ZZZ", "Dacia", "Sandero") rentStart = datetime.strptime("2015-11-26", "%Y-%m-%d") rentEnd = datetime.strptime("2015-11-30", "%Y-%m-%d") rentController.createRental(301, clientRepo.find(103), carRepo.find(201), rentStart, rentEnd) print("---> We added a client, a new car and a rental") print(clientRepo) print(carRepo) print(rentRepo) ''' Now undo the performed operations, one by one ''' undoController.undo() print("---> After 1 undo") print(clientRepo) print(carRepo) print(rentRepo) undoController.undo() print("---> After 2 undos") print(clientRepo) print(carRepo) print(rentRepo) undoController.undo() print("---> After 3 undos") print(clientRepo) print(carRepo) print(rentRepo)
def setUp(self): self.val = BookValidator() self.repo = BookRepository() self.ctrl = BookController(self.repo, UndoController(), self.val) self.book = Book(1, "Alchimistul", "", "Paulo Coelho")
""" Created on 29/01/2017 @author Stefan """ from console.console import Console from controller.Controller import Controller from controller.UndoController import UndoController from repository.SentenceRepository import SentenceRepository if __name__ == '__main__': sentence_repo = SentenceRepository() controller = Controller(sentence_repo) undo_controller = UndoController() console = Console(controller, undo_controller) console.run()
class TestRentalController(unittest.TestCase): def setUp(self): self.repo = Repository() self.brepo = Repository() self.crepo = Repository() self.Ucontroller = UndoController() self.book = Book(21, 'titlu', 'descriere', 'author') self.book2 = Book(22, 'titlu2', 'descriere2', 'author') self.brepo.add(self.book) self.brepo.add(self.book2) self.client = Client(23, 'alex') self.client2 = Client(24, 'ana') self.client3 = Client(29, 'ana') self.crepo.add(self.client) self.crepo.add(self.client2) self.rental = Rental(21236, 21, 24, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental2 = Rental(21238, 22, 24, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental3 = Rental(21238, 23, 24, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental4 = Rental(21238, 21, 29, date(2017, 11, 5), date(2017, 12, 6), date(2017, 12, 5)) self.rental5 = Rental(21231, 21, 23, date(2017, 11, 5), date(2017, 10, 6), None) self.controller = RentalController(self.repo, self.brepo, self.crepo, self.Ucontroller) self.ceva = LateRentalCount(12, 32) self.altceva = BookRentalCount(12, 23) self.nu = AuthorRentalCount('da', 23) self.da = ClientRentalCount(32, 12) def test_controller(self): self.Ucontroller.newOperation() self.controller.addRental(self.rental2) self.assertEqual(len(self.repo), 1) self.assertEqual(self.controller.getRental(21238).getID(), 21238) self.assertRaises(ControllerException, self.controller.addRental, self.rental3) self.assertRaises(ControllerException, self.controller.addRental, self.rental4) self.book2.set_available(False) self.assertRaises(ControllerException, self.controller.addRental, self.rental2) self.assertRaises(ControllerException, self.controller.returnBook, 13123124) self.Ucontroller.newOperation() self.controller.addRental(self.rental) self.assertEqual(self.controller.returnBook(21236), self.rental) self.assertEqual(len(self.controller.getAllRental()), 2) self.assertEqual(len(self.controller.mostRentedBooks()), 2) self.assertEqual(len(self.controller.mostActiveClients()), 2) self.assertEqual(len(self.controller.mostRentedAuthors()), 1) self.controller.deleteRental(self.rental.getID()) self.Ucontroller.newOperation() self.controller.addRental(self.rental5) self.assertEqual(len(self.controller.lateRentals()), 1) self.assertEqual(self.ceva.getBook(), 12) self.assertEqual(self.ceva.getCount(), 32) self.assertFalse(self.ceva > self.ceva) self.assertEqual(str(self.ceva), 'Book 12 has been overdue for 32 days') self.assertEqual(self.altceva.getCount(), 23) self.assertEqual(self.altceva.getBook(), 12) self.assertFalse(self.altceva > self.altceva) self.assertEqual(str(self.altceva), 'Book 12 was rented 23 times') self.assertEqual(self.nu.getBook(), 'da') self.assertEqual(self.nu.getCount(), 23) self.assertEqual(str(self.nu), 'Author da was rented for 23 times') self.assertFalse(self.nu > self.nu) self.assertFalse(self.da > self.da) self.assertEqual(str(self.da), 'Client 32 has rented books for 12 days') self.assertEqual(self.da.getCount(), 12) self.assertEqual(self.da.getBook(), 32)
def statisticsExample(): """ An example for the creation of statistics. Several cars, clients and rentals are created and then a statistics is calculated over them. Follow the code below and figure out how it works! """ undoController = UndoController() ''' Start client Controller ''' clientRepo = Repository() clientValidator = ClientValidator() clientController = ClientController(undoController, clientValidator, clientRepo) clientController.create(100, "1820203556699", "Aaron") clientController.create(101, "2750102885566", "Bob") clientController.create(102, "1820604536579", "Carol") # We name the instances so it's easier to create some test values later aaron = clientRepo.find(100) bob = clientRepo.find(101) carol = clientRepo.find(102) ''' Start car Controller ''' carRepo = Repository() carValidator = CarValidator() carController = CarController(undoController, carValidator, carRepo) carController.create(200, "CJ 01 AAA", "Audi", "A3") carController.create(201, "CJ 01 BBB", "Audi", "A4") carController.create(202, "CJ 01 CCC", "Audi", "A5") carController.create(203, "CJ 01 DDD", "Audi", "A6") audiA3 = carRepo.find(200) audiA4 = carRepo.find(201) audiA5 = carRepo.find(202) audiA6 = carRepo.find(203) ''' Start rental Controller ''' rentRepo = Repository() rentValidator = RentalValidator() rentController = RentalController(undoController, rentValidator, rentRepo, carRepo, clientRepo) rentController.createRental(300, aaron, audiA3, datetime.strptime("2015-11-20", "%Y-%m-%d"), datetime.strptime("2015-11-22", "%Y-%m-%d")) rentController.createRental(301, carol, audiA5, datetime.strptime("2015-11-24", "%Y-%m-%d"), datetime.strptime("2015-11-25", "%Y-%m-%d")) rentController.createRental(302, carol, audiA6, datetime.strptime("2015-12-10", "%Y-%m-%d"), datetime.strptime("2015-12-12", "%Y-%m-%d")) rentController.createRental(303, aaron, audiA4, datetime.strptime("2015-11-21", "%Y-%m-%d"), datetime.strptime("2015-11-25", "%Y-%m-%d")) rentController.createRental(304, aaron, audiA3, datetime.strptime("2015-11-24", "%Y-%m-%d"), datetime.strptime("2015-11-27", "%Y-%m-%d")) rentController.createRental(305, bob, audiA5, datetime.strptime("2015-11-26", "%Y-%m-%d"), datetime.strptime("2015-11-27", "%Y-%m-%d")) rentController.createRental(306, carol, audiA6, datetime.strptime("2015-12-15", "%Y-%m-%d"), datetime.strptime("2015-12-20", "%Y-%m-%d")) rentController.createRental(307, bob, audiA4, datetime.strptime("2015-12-01", "%Y-%m-%d"), datetime.strptime("2015-12-10", "%Y-%m-%d")) rentController.createRental(308, carol, audiA4, datetime.strptime("2015-12-11", "%Y-%m-%d"), datetime.strptime("2015-12-15", "%Y-%m-%d")) rentController.createRental(309, aaron, audiA5, datetime.strptime("2015-11-28", "%Y-%m-%d"), datetime.strptime("2015-12-02", "%Y-%m-%d")) for cr in rentController.mostRentedCars(): print(cr)
class TestBookController(unittest.TestCase): def setUp(self): self.repo = Repository() self.rentalRepo = Repository() self.Ucontroller = UndoController() self.book = Book(21, 'titlu', 'descriere', 'author') self.book2 = Book(22, 'titlu2', 'descriere2', 'author2') self.controller = BookController(self.repo, self.Ucontroller, self.rentalRepo) def test_something(self): self.Ucontroller.newOperation() self.controller.addBook(self.book) self.assertEqual(len(self.repo), 1) self.Ucontroller.newOperation() self.controller.removeBook(21) self.assertEqual(len(self.repo), 0) self.Ucontroller.newOperation() self.controller.addBook(self.book) self.Ucontroller.newOperation() self.controller.updateBook(self.book) self.Ucontroller.newOperation() self.controller.addBook(self.book2) self.assertEqual(len(self.controller.getAllBooks()), 2) self.assertEqual(self.controller.getBook(22), self.book2) self.assertRaises(ControllerException, self.controller.searchBook, 'nu') self.assertRaises(ControllerException, self.controller.searchBook, '') self.assertEqual(len(self.controller.searchBook('tit')), 2) self.Ucontroller.newOperation() self.controller.removeBook(22) self.assertEqual( str(self.controller), '021. titlu by author\n->descriere\nStatus: Available\n\n')
self._player.update(player) elif option == '4': print("Creating a team.") team = self._player.createTeam() for p in team: print(p) elif option == '5': print("Importing players.") print(self._player.importPlayers()) elif option == '6': self._undo.undo() elif option == '7': self._undo.redo() elif option == '0': break else: print("Invalid option. Enter * for help.") validator = PlayerValidator() repository = CSVPlayers('players.csv', 'imported.csv') undo = UndoController() player = PlayerController(validator, repository, undo) menu = UI(player, undo) menu.start()
def undoExampleHardest(): undoController = UndoController() clientRepo = Repository() carRepo = Repository() ''' Start rental Controller ''' rentRepo = Repository() rentController = RentalController(undoController, rentRepo, carRepo, clientRepo) ''' Start client Controller ''' clientController = ClientController(undoController, rentController, clientRepo) ''' Start car Controller ''' carController = CarController(undoController, rentController, carRepo) ''' We add 1 client, 1 car and 2 rentals ''' clientSophia = clientController.create(103, "2990511035588", "Sophia") carHyundaiTucson = carController.create(201, "CJ 02 TWD", "Hyundai", "Tucson") rentController.createRental(301, clientSophia, carHyundaiTucson, date(2016, 11, 1), date(2016, 11, 30)) rentController.createRental(302, clientSophia, carHyundaiTucson, date(2016, 12, 1), date(2016, 12, 31)) printReposWithMessage("We added Sophia, the Hyundai and its 2 rentals", clientRepo, carRepo, rentRepo) carController.delete(201) printReposWithMessage("Delete the Hyundai (also deletes its rentals)", clientRepo, carRepo, rentRepo) ''' Now undo the performed operations, one by one ''' undoController.undo() printReposWithMessage("1 undo, the Hyundai and its rentals are back", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the second rental", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the first rental", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the Hyundai", clientRepo, carRepo, rentRepo) ''' After 5 undos, all repos should be empty, as we did 5 operations in total ''' undoController.undo() printReposWithMessage("1 undo deletes Sophia (no more undos)", clientRepo, carRepo, rentRepo) ''' Redos start here ''' undoController.redo() printReposWithMessage("1 redo and Sophia is added", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo adds the Hyundai", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo adds the first rental", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo adds the second rental", clientRepo, carRepo, rentRepo) undoController.redo() printReposWithMessage("1 redo deletes the Hyundai and its rentals (again)", clientRepo, carRepo, rentRepo) ''' Let's do a few undos again... ''' undoController.undo() undoController.undo() undoController.undo() printReposWithMessage("3 undos later, we have Sophia and the Hyundai", clientRepo, carRepo, rentRepo) ''' Now we try something new - let's add another car! NB! A new operation must invalidate the history for redo() operations ''' carController.create(202, "CJ 02 SSE", "Dacia", "Sandero") print("\n Do we have a redo? -", undoController.redo(), "\n") ''' Now we should have 2 cars ''' printReposWithMessage("After adding the Dacia, there is no redo ", clientRepo, carRepo, rentRepo) ''' However, undos is still available ! ''' undoController.undo() printReposWithMessage("1 undo deletes the Dacia", clientRepo, carRepo, rentRepo) undoController.undo() printReposWithMessage("1 undo deletes the Hyundai", clientRepo, carRepo, rentRepo)
Brepo = FileRepository('books.txt', Book.readBookFromLine, Book.writeBookToLine) Crepo = FileRepository('clients.txt', Client.readClientFromLine, Client.writeClientToLine) Rrepo = Repository() Rrepo.add(Rental(16675, 16, 67, date(2017, 11, 10), date(2017, 12, 11))) Rrepo.add(Rental(12675, 12, 67, date(2017, 11, 1), date(2017, 11, 11))) Rrepo.add(Rental(13115, 13, 11, date(2017, 11, 21), date(2017, 11, 11))) Rrepo.add(Rental(13678, 13, 67, date(2017, 11, 7), date(2017, 11, 20))) Rrepo.add(Rental(12622, 12, 62, date(2017, 11, 18), date(2017, 1, 11))) Rrepo.add(Rental(14675, 14, 67, date(2017, 11, 5), date(2017, 11, 24))) Rrepo.add(Rental(14637, 14, 63, date(2017, 11, 10), date(2017, 10, 11))) Rrepo.add(Rental(14671, 14, 67, date(2017, 11, 9), date(2017, 12, 11))) Rrepo.add(Rental(16652, 16, 65, date(2017, 11, 4), date(2017, 11, 16))) Rrepo.add(Rental(15661, 15, 66, date(2017, 11, 12), date(2017, 10, 30))) Rrepo.add(Rental(12629, 12, 62, date(2017, 11, 13), date(2017, 11, 25))) Rrepo.add(Rental(15695, 15, 69, date(2017, 11, 19), date(2017, 11, 24))) Rrepo.add(Rental(16770, 16, 77, date(2017, 11, 2), date(2017, 11, 24))) Ucontroller = UndoController() Bcontroller = BookController(Brepo, Ucontroller, Rrepo) Ccontroller = ClientController(Crepo, Ucontroller, Rrepo) Rcontroller = RentalController(Rrepo, Brepo, Crepo, Ucontroller) ui = UI(Bcontroller, Ccontroller, Rcontroller, Ucontroller) # ui.mainMenu() root = Tk() gui = GUI(root, Bcontroller, Ccontroller, Rcontroller, Ucontroller) root.mainloop()