示例#1
0
class TestRepository(unittest.TestCase):
    def setUp(self):
        self.repo = Repository()
        self.c = Client(12, 'alex')
        self.c1 = Client(121, 'ana')
        self.repo2 = FileRepository('testclient.txt',
                                    Client.readClientFromLine,
                                    Client.writeClientToLine)
        self.repo3 = FileRepository('testclient4.txt',
                                    Client.readClientFromLine,
                                    Client.writeClientToLine)

    def test_repo(self):
        self.assertEqual(len(self.repo), 0)
        self.repo.add(self.c)
        self.assertEqual(str(self.repo), '012. alex\n\n')
        self.assertEqual(len(self.repo), 1)
        self.assertRaises(RepositoryException, self.repo.add, self.c)
        self.assertEqual(len(self.repo.getAll()), 1)
        self.assertEqual(self.repo.findByID(12), 12)
        self.assertEqual(self.repo.findByID(13), -1)
        self.assertEqual(self.repo.get(12), self.c)
        self.assertRaises(RepositoryException, self.repo.get, 13)
        self.assertRaises(RepositoryException, self.repo.remove, 13)
        self.assertRaises(RepositoryException, self.repo.update, 13)
        self.repo.update(self.c)
        self.repo.remove(12)
        self.assertEqual(len(self.repo), 0)
        self.repo2.add(self.c)
        self.repo2.upd(self.c)
        self.repo2.rem(self.c.getID())
        self.assertEqual(len(self.repo2), 1)
示例#2
0
 def testRepository(self):
     bookRepo = Repository()
     testList = [
         Book(0, "book0", "desc0", "author0"),
         Book(1, "book1", "desc1", "author1")
     ]
     for i in range(0, len(testList)):
         bookRepo.add(testList[i])
         self.assertEqual(bookRepo.get(i), testList[i])
     bookRepo.update(1, Book(0, "Book1", "Author1", "Description1"))
     self.assertEqual(bookRepo.get(1),
                      Book(1, "Book1", "Author1", "Description1"))
    def testRepositoryClients(self):
        repoClient = Repository()
        client = Client(1, "Name")
        client1 = Client(2, "Name1")

        self.assertTrue(repoClient.size() == 0)
        repoClient.add(client)
        self.assertTrue(repoClient.size() == 1)
        repoClient.add(client1)

        self.assertTrue(repoClient.existsById(1))
        self.assertTrue(repoClient.existsById(2))
        self.assertTrue(repoClient.getById(1).getName() == "Name")

        repoClient.removeById(1)
        self.assertTrue(repoClient.size() == 1)
        repoClient.remove(client1)
        self.assertTrue(repoClient.size() == 0)

        self.assertTrue(repoClient.existsById(1) == False)

        repoClient.add(client)
        repoClient.update(Client(1, "Updated Name"))

        self.assertTrue(repoClient.findById(1) == 0)
        self.assertTrue(repoClient.getById(1).getName() == "Updated Name")

        self.assertTrue(str(repoClient.getById(1)) == "CLIENT ID: " + str(
            repoClient.getById(1).getId()) + "| Name: " + repoClient.getById(1).getName())
示例#4
0
文件: tester.py 项目: crssic/courses
 def test(self):
     x = Route(1, "24b", 93, 3)
     assert (x.getId() == 1)
     x.setId(2)
     assert (x.getId() == 2)
     assert (x.getRouteCode() == "24b")
     x.setRouteCode("24")
     assert (x.getRouteCode() == "24")
     assert (x.getUsage() == 93)
     x.setUsage(95)
     assert (x.getUsage() == 95)
     assert (x.getBuses() == 3)
     x.setBuses(x.getBuses() + 1)
     assert (x.getBuses() == 4)
     x = Route(1, "24b", 93, 3)
     repo = Repository()
     repo.add(x)
     assert (repo.getRoutes() == [Route(1, "24b", 93, 3)])
     y = Route(13, "25", 93, 7)
     repo.add(y)
     assert (repo.getRoutes() == [
         Route(1, "24b", 93, 3),
         Route(13, "25", 93, 7)
     ])
     repo.add(y)
     assert (repo.getRoutes() == [
         Route(1, "24b", 93, 3),
         Route(13, "25", 93, 7),
         Route(13, "25", 93, 7)
     ])
示例#5
0
 def test(self):
     x = Route(1,"24b",93,3)
     assert(x.getId() == 1)
     x.setId(2)
     assert(x.getId() == 2)
     assert(x.getRouteCode() == "24b")
     x.setRouteCode("24")
     assert(x.getRouteCode() == "24")
     assert(x.getUsage() == 93)
     x.setUsage(95)
     assert(x.getUsage() == 95)
     assert(x.getBuses() == 3)
     x.setBuses(x.getBuses() + 1)
     assert(x.getBuses() == 4)
     x = Route(1,"24b",93,3)
     repo = Repository()
     repo.add(x)
     assert(repo.getRoutes() == [Route(1,"24b",93,3)])
     y = Route(13,"25",93,7)
     repo.add(y)
     assert(repo.getRoutes() == [Route(1,"24b",93,3), Route(13,"25",93,7)])
     repo.add(y)
     assert(repo.getRoutes() == [Route(1,"24b",93,3), Route(13,"25",93,7), Route(13,"25",93,7)])
 def add(self, other):
     Repository.add(self, other)
     self.appendToFile(other)
示例#7
0
class PickleRepository(Repository):
    def __init__(self, fileName):
        self.__fileName = fileName
        self.__repo = Repository()
        self.__clear(fileName)
        self.__loadFromFile()

    def add(self, elem):
        self.__repo.add(elem)
        self.__storeToFile()

    def remove(self, elem):
        self.__repo.remove(elem)
        self.__storeToFile()

    def removeById(self, id):
        self.__repo.removeById(id)
        self.__storeToFile()

    def update(self, elem):
        self.__repo.update(elem)
        self.__storeToFile()

    def __len__(self):
        return self.__repo.size()

    def getById(self, id):
        return self.__repo.getById(id)

    def findById(self, id):
        return self.__repo.findById(id)

    def getAll(self):
        return self.__repo.getAll()

    def existsById(self, id):
        return self.__repo.existsById(id)

    def size(self):
        return self.__repo.size()

    def __storeToFile(self):
        f = open(self.__fileName, "wb")
        pickle.dump(self.__repo.getAll(), f)
        f.close()

    def __loadFromFile(self):
        f = open(self.__fileName, "rb")
        try:
            self.__repo._elems = pickle.load(f)
        except EOFError:
            return []
        f.close()

    def __clear(self, fileName):
        """
        Clear the file
        :param fileName:
        :return: None
        """
        open(fileName, "w").close()
示例#8
0
class ControllerTest(unittest.TestCase):
    def setUp(self):
        unittest.TestCase.setUp(self)
        undoController = UndoController()

        self.rentalList = Repository()
        self.bookList = Repository()
        self.clientList = Repository()

        self.rentalList.add(
            Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10",
                                                       '%Y-%m-%d'),
                   datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), ""))
        self.rentalList.add(
            Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10",
                                                       '%Y-%m-%d'),
                   datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), ""))

        self.bookList.add(Book(0, "book0", "desc0", "author0"))
        self.bookList.add(Book(1, "book1", "desc1", "author1"))
        self.bookList.add(Book(2, "book2", "desc2", "author2"))

        self.clientList.add(Client(0, "name0"))
        self.clientList.add(Client(1, "name1"))
        self.clientList.add(Client(2, "name2"))

        self.rentalController = RentalController(self.rentalList,
                                                 self.bookList,
                                                 self.clientList,
                                                 undoController)
        self.bookController = BookController(self.bookList, undoController,
                                             self.rentalController)
        self.clientController = ClientController(self.clientList,
                                                 undoController,
                                                 self.rentalController)

    def tearDown(self):
        unittest.TestCase.tearDown(self)

    def testAddBook(self):

        self.bookController.addBook(3, "book3", "desc3", "author3")
        self.assertEqual(self.bookList.getAll(), [
            Book(0, "book0", "desc0", "author0"),
            Book(1, "book1", "desc1", "author1"),
            Book(2, "book2", "desc2", "author2"),
            Book(3, "book3", "desc3", "author3")
        ])

    def testAddClient(self):

        self.clientController.addClient(3, "name3")
        self.assertEqual(self.clientList.getAll(), [
            Client(0, "name0"),
            Client(1, "name1"),
            Client(2, "name2"),
            Client(3, "name3")
        ])

    def testAddRental(self):

        rentalDate = datetime.datetime.now().date()
        self.rentalController.addRental(2, 2, 1)
        self.assertEqual(self.rentalList.getAll(), [
            Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10",
                                                       '%Y-%m-%d'),
                   datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), ""),
            Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10",
                                                       '%Y-%m-%d'),
                   datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), ""),
            Rental(2, 2, 1, rentalDate,
                   rentalDate + datetime.timedelta(days=10), "")
        ])
        try:
            self.rentalController.addRental(
                Rental(100, 1,
                       datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'),
                       datetime.datetime.strptime("2017-10-20", '%Y-%m-%d')))
            self.rentalController.addRental(
                Rental(2, 200,
                       datetime.datetime.strptime("2017-10-10", '%Y-%m-%d'),
                       datetime.datetime.strptime("2017-10-20", '%Y-%m-%d')))
        except Exception:
            pass

    def testRemoveBook(self):

        self.bookController.removeBook(0)
        self.assertEqual(self.bookList.getAll(), [
            Book(1, "book1", "desc1", "author1"),
            Book(2, "book2", "desc2", "author2")
        ])
        self.bookController.removeBook(2)
        self.bookController.removeBook(1)
        self.assertEqual(self.bookList.getAll(), [])
        with self.assertRaises(Exception):
            self.bookController.removeBook(10)

    def testRemoveClient(self):

        self.clientController.removeClient(1)
        self.assertEqual(
            self.clientList.getAll(),
            [Client(0, "name0"), Client(2, "name2")])
        with self.assertRaises(Exception):
            self.clientController.removeClient(100)

    def testRemoveRentalByClientId(self):

        self.rentalController.removeRentalByClientId(1)
        self.assertEqual(self.rentalList.getAll(), [])

    def testRemoveRentalByBookId(self):

        self.rentalController.removeRentalByBookId(0)
        self.assertEqual(self.rentalList.getAll(), [
            Rental(1, 1, 1, datetime.datetime.strptime("2017-10-10",
                                                       '%Y-%m-%d'),
                   datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'), "")
        ])

    def testUpdateBook(self):
        with self.assertRaises(InvalidIdException):
            self.bookController.updateBook(20, "book2", "author3", "desc4")

        self.bookController.updateBook(1, "Book1", "Author1", "Description1")
        self.assertEqual(self.bookList.get(1),
                         Book(1, "Book1", "Author1", "Description1"))

    def testUpdateClients(self):
        with self.assertRaises(InvalidIdException):
            self.clientController.updateClient(20, "Name1")
        self.clientController.updateClient(1, "Name1")
        self.assertEqual(self.clientList.get(1), Client(1, "Name1"))

    def testReturnBook(self):

        with self.assertRaises(Exception):
            self.rentalController.returnBook(100)
        self.rentalController.returnBook(0)
        self.assertEqual(
            self.rentalList.get(0),
            Rental(0, 0, 1, datetime.datetime.strptime("2017-10-10",
                                                       '%Y-%m-%d'),
                   datetime.datetime.strptime("2017-10-20", '%Y-%m-%d'),
                   datetime.datetime.now().date()))

    def testGetBooks(self):

        self.assertEqual(self.bookController.getBooks(), [
            Book(0, "book0", "desc0", "author0"),
            Book(1, "book1", "desc1", "author1"),
            Book(2, "book2", "desc2", "author2")
        ])

    def testGetClients(self):

        self.assertEqual(
            self.clientController.getClients(),
            [Client(0, "name0"),
             Client(1, "name1"),
             Client(2, "name2")])

    def testRentalVaidation(self):

        self.assertEqual(self.rentalController.rentalValidation(2, 1), True)

        with self.assertRaises(InvalidIdException):
            self.rentalController.rentalValidation(0, 7)
            self.rentalController.rentalValidation(13, 0)

    def testMostRentedBooks(self):

        newList = self.rentalController.mostRentedBooks()
        self.assertEqual(newList, [
            BookRental(0, "book0", 1),
            BookRental(1, "book1", 1),
            BookRental(2, "book2", 0)
        ])

    def testMostActiveClient(self):

        newList = self.rentalController.mostActiveClients()
        self.assertEqual(newList, [ClientActivity("name1", 20)])

    def testMostRentedAuthor(self):

        newList = self.rentalController.mostRentedAuthors()
        self.assertEqual(newList, [
            AuthorRental(0, "author0", 1),
            AuthorRental(1, "author1", 1),
            AuthorRental(2, "author2", 0)
        ])

    def testSearchBooks(self):

        searchList = self.bookController.searchBooks("book")
        self.assertEqual(searchList, [
            Book(0, "book0", "desc0", "author0"),
            Book(1, "book1", "desc1", "author1"),
            Book(2, "book2", "desc2", "author2")
        ])
        self.bookList.add(Book(3, "book27", "desc3", "author4"))
        searchList1 = self.bookController.searchBooks("2")
        self.assertEqual(searchList1, [
            Book(2, "book2", "desc2", "author2"),
            Book(3, "book27", "desc3", "author4")
        ])

    def testSearchClients(self):
        searchList = self.clientController.searchClients("cl")
        self.assertEqual(searchList, [])
        searchList1 = self.clientController.searchClients("na")
        self.assertEqual(
            searchList1,
            [Client(0, "name0"),
             Client(1, "name1"),
             Client(2, "name2")])
    def testRepositoryBooks(self):
        repoBooks = Repository()
        repoText = CSVRepository("bookRepo.csv", Book(1, "e", "s", "w"))
        repoBinary = PickleRepository("binaryBookRepo.pickle")
        repoCustom = CustomRepository(Book)

        book = Book(1, "Title", "Description", "Author")
        book1 = Book(2, "Title1", "Description1", "Author1")

        self.assertEqual(repoBooks.size(), 0)
        self.assertEqual(repoBinary.size(), 0)
        self.assertEqual(repoText.size(), 0)
        self.assertEqual(repoCustom.size(), 0)

        repoBooks.add(book)
        repoBooks.add(book1)
        repoText.add(book)
        repoText.add(book1)
        repoBinary.add(book)
        repoBinary.add(book1)
        repoCustom.add(book)
        repoCustom.add(book1)

        self.assertTrue(repoBooks.existsById(1))
        self.assertTrue(repoBooks.size() == 2)
        self.assertTrue(repoBooks.getById(1) == book)
        self.assertTrue(repoBooks.existsById(2))
        self.assertTrue(repoBooks.getById(2) == book1)

        self.assertTrue(repoText.existsById(1))
        self.assertTrue(repoText.size() == 2)
        self.assertTrue(repoText.getById(1) == book)
        self.assertTrue(repoText.existsById(2))
        self.assertTrue(repoText.getById(2) == book1)

        self.assertTrue(repoBinary.existsById(1))
        self.assertTrue(repoBinary.size() == 2)
        self.assertTrue(repoBinary.getById(1) == book)
        self.assertTrue(repoBinary.existsById(2))
        self.assertTrue(repoBinary.getById(2) == book1)

        self.assertTrue(repoCustom.existsById(1))
        self.assertTrue(repoCustom.size() == 2)
        self.assertTrue(repoCustom.getById(1) == book)
        self.assertTrue(repoCustom.existsById(2))
        self.assertTrue(repoCustom.getById(2) == book1)

        repoBooks.removeById(1)
        repoBooks.remove(book1)
        repoText.removeById(1)
        repoText.remove(book1)
        repoBinary.removeById(1)
        repoBinary.remove(book1)
        repoCustom.removeById(1)
        repoCustom.remove(book1)

        self.assertTrue(repoBooks.size() == 0)
        self.assertTrue(repoBooks.existsById(1) == False)
        self.assertTrue(repoText.size() == 0)
        self.assertTrue(repoText.existsById(1) == False)
        self.assertTrue(repoBinary.size() == 0)
        self.assertTrue(repoBinary.existsById(1) == False)
        self.assertTrue(repoCustom.size() == 0)
        self.assertTrue(repoCustom.existsById(1) == False)

        repoBooks.add(book)
        repoBooks.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        repoText.add(book)
        repoText.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        repoBinary.add(book)
        repoBinary.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        repoCustom.add(book)
        repoCustom.update(Book(1, "Updated Title", "Updated Desc", "Updated Author"))

        self.assertTrue(repoBooks.findById(1) == 0)
        self.assertTrue(repoText.findById(1) == 0)
        self.assertTrue(repoBinary.findById(1) == 0)
        self.assertTrue(repoCustom.findById(1) == 0)

        self.assertTrue(repoBooks.getById(1).getTitle() == "Updated Title")
        self.assertTrue(repoText.getById(1).getTitle() == "Updated Title")
        self.assertTrue(repoBinary.getById(1).getTitle() == "Updated Title")
        self.assertTrue(repoCustom.getById(1).getTitle() == "Updated Title")

        self.assertTrue(repoBooks.getById(1).getDescription() == "Updated Desc")
        self.assertTrue(repoText.getById(1).getDescription() == "Updated Desc")
        self.assertTrue(repoBinary.getById(1).getDescription() == "Updated Desc")
        self.assertTrue(repoCustom.getById(1).getDescription() == "Updated Desc")

        self.assertTrue(repoBooks.getById(1).getAuthor() == "Updated Author")
        self.assertTrue(repoText.getById(1).getAuthor() == "Updated Author")
        self.assertTrue(repoBinary.getById(1).getAuthor() == "Updated Author")
        self.assertTrue(repoCustom.getById(1).getAuthor() == "Updated Author")

        self.assertTrue(str(repoBooks.getById(1)) == "BOOK ID: " + str(repoBooks.getById(1).getId()) + "| Title: " + \
                        repoBooks.getById(1).getTitle() + "| Description: " + \
                        repoBooks.getById(1).getDescription() + \
                        "| Author: " + repoBooks.getById(1).getAuthor())
        self.assertTrue(str(repoText.getById(1)) == "BOOK ID: " + str(repoText.getById(1).getId()) + "| Title: " + \
                        repoText.getById(1).getTitle() + "| Description: " + \
                        repoText.getById(1).getDescription() + \
                        "| Author: " + repoText.getById(1).getAuthor())
        self.assertTrue(str(repoBinary.getById(1)) == "BOOK ID: " + str(repoBinary.getById(1).getId()) + "| Title: " + \
                        repoBinary.getById(1).getTitle() + "| Description: " + \
                        repoBinary.getById(1).getDescription() + \
                        "| Author: " + repoBinary.getById(1).getAuthor())
        self.assertTrue(str(repoCustom.getById(1)) == "BOOK ID: " + str(repoCustom.getById(1).getId()) + "| Title: " + \
                        repoCustom.getById(1).getTitle() + "| Description: " + \
                        repoCustom.getById(1).getDescription() + \
                        "| Author: " + repoCustom.getById(1).getAuthor())
示例#10
0
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 add(self, other):
     Repository.add(self, other)
     self.writeAllToFile()
class CSVRepository(Repository):
    def __init__(self, fileName, objectType):
        self.__fileName = fileName
        self.__repo = Repository()
        self.__clear(fileName)
        self.type = objectType
        self.__loadFromFile(objectType)

    def __clear(self, fileName):
        """
        Clear the file
        :param fileName:
        :return: None
        """
        open(fileName, "w").close()

    def add(self, elem):
        """
        Write the updated list to the file
        :param elem: elem to be added
        :return:
        """
        self.__repo.add(elem)
        self.__storeToFile(elem)

    def remove(self, elem):
        """
        Remove elem from the repo
        :param elem:
        :return:
        """
        self.__repo.remove(elem)
        self.__storeToFile(elem)

    def removeById(self, id):
        """
        :param id: int
        :return: The element with the id id
        """
        self.__repo.removeById(id)
        self.__storeToFile(self.type)  #TODO

    def update(self, elem):
        """
        Update an element
        :param elem:
        :return: None
        """
        self.__repo.update(elem)
        self.__storeToFile(elem)

    def __len__(self):
        return len(self.__repo)

    def getById(self, id):
        """
        :param id: int
        :return: The element with the id id
        """
        return self.__repo.getById(id)

    def findById(self, id):
        """

        :param id: int
        :return: the index of the elem with the id id
        """
        return self.__repo.findById(id)

    def getAll(self):
        """

        :return: a list of all elements
        """
        return self.__repo.getAll()

    def existsById(self, id):
        """

        :param id: int
        :return: True if the element with the id is in the repo
        """
        return self.__repo.existsById(id)

    def size(self):
        """
        :return: The number of elements in the repo
        """
        return self.__repo.size()

    def __storeToFile(self, type):
        """
        :param elemSample: element
        :return: None
        """
        f = open(self.__fileName, "w")

        # handle each type of object accordingly
        if type == Book:
            for book in self.__repo.getAll():
                output = str(book.getId()) + ","
                output += book.getTitle() + ","
                output += book.getDescription() + ";"
                output += book.getAuthor()
                output += "\n"
                f.write(output)

        if type == Client:
            for client in self.__repo.getAll():
                output = str(client.getId()) + ","
                output += client.getName()
                output += "\n"
                f.write(output)

        if type == Rental:
            for rental in self.__repo.getAll():
                output = str(rental.getRentalId()) + ","
                output += str(rental.getBookId()) + ","
                output += str(rental.getClientId()) + ","
                output += str(rental.getRentedDate()) + ","
                output += str(rental.getDueDate()) + ","
                output += str(rental.getReturnDate())
                output += "\n"
                f.write(output)

        f.close()

    def __loadFromFile(self, type):
        """
        :param sample: Element
        :return: None
        :raises RepositoryException when the file cannot be read for some reason
        """
        try:
            f = open(self.__fileName, "r")
            line = f.readline().strip()
            while line != "":
                attrs = line.split(",")

                if type == Book:
                    book = Book(int(attrs[0]), attrs[1], attrs[2], attrs[3])
                    self.__repo.add(book)
                    line = f.readline().strip()

                if type == Client:
                    client = Client(int(attrs[0]), attrs[1])
                    self.__repo.add(client)
                    line = f.readline().strip()

                if type == Rental:
                    rental = Rental(
                        int(attrs[0]), int(attrs[1]), int(attrs[2]),
                        createDateFromString(self.__reverseDate(attrs[3])),
                        createDateFromString(self.__reverseDate(attrs[4])),
                        createDateFromString(self.__reverseDate(attrs[5])))
                    self.__repo.add(rental)
                    line = f.readline().strip()
        except IOError:
            raise RepositoryException()
        finally:
            f.close()
示例#13
0
from domain.Client import Client
from domain.Rental import Rental
from controller.BookController import BookController
from controller.ClientController import ClientController
from controller.RentalController import RentalController
from controller.UndoController import UndoController
from repository.repository import Repository, FileRepository
from datetime import date
from tkinter import *

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)