def _loadFile(self): try: f = open(self._fileName, "r") s = f.readline() while len(s)>1: tok = s.split(",") """ function to turn string into entity """ if len(tok) == 3: tok[2] = tok[2].split("\n") book = Book(int(tok[0]), tok[1], tok[2][0]) else: tok[3] = tok[3].split("\n") book = Book(int(tok[0]), tok[1], tok[2], tok[3][0]) BookRepository.add(self, book) s = f.readline() except Exception as e: raise RepoError("Error reading input file: " + str(e)) finally: f.close()
def _loadBinaryFile(self): try: f = open(self._fileName, "rb") self._clients = pickle.load(f) except EOFError: self._clients = [] except IOError as ioe: raise RepoError("An error occured: " + str(ioe))
def add(self, rental): """ Adds the given rental in the repository Input: rental - an object of the class Rental Exceptions: RepoError - if the rental exists in the repository already """ if rental in self._rentals: raise RepoError("! This book rental exists already") self._rentals.append(rental)
def _saveFile(self): try: f = open(self._fileName, "w") for c in ClientRepository.get_clients(self): f.write(str(c.get_id()) + "," + c.get_name() + "\n") except Exception as e: raise RepoError("Error writing file: " + str(e)) finally: f.close()
def _loadFile(self): with open(self._fileName, "r") as f: try: json_books = json.load(f) for b in json_books: book = self.json_to_book(b) BookRepository.add(self, book) except JSONDecodeError as je: raise RepoError("could not load file: " + str(je))
def remove(self, element): """ function that removes an element from repository :param element: student/assignment/grade :return: - """ if element not in self.__entities: raise RepoError("Inexisting element") self.__entities.remove(element)
def add(self, client): """ Adds a given client in the repository Input: client - an object of the class Client Exception: RepoError - if the client exists in the repository already """ if client in self._clients: raise RepoError("! This client already exists.") self._clients.append(client)
def _loadFile(self): with open(self._fileName, "r") as f: try: json_clients = json.load(f) for c in json_clients: client = self.json_to_client(c) ClientRepository.add(self, client) except JSONDecodeError as je: raise RepoError("could not load file: " + str(je))
def _loadFile(self): with open(self._fileName, "r") as f: try: json_rentals = json.load(f) for r in json_rentals: rental = self.json_to_rental(r) RentalRepository.add(self, rental) except JSONDecodeError as je: raise RepoError("could not load file: " + str(je))
def available_rent(self,rental): """ Checks id the book from the given rent is available to be rented Input: rental - an object of the class Rental Exceptions: RepoError - if the book is not available to be rented """ for i in range (0,len(self._rentals)): if self._rentals[i].get_b_id() == rental.get_b_id() and (self._rentals[i].get_returned_date() == None): raise RepoError("! The book is not available at the moment") return
def add(self, element): """ function that adds a new element to the repository :param element: student/assignment/grade :return: - """ if element not in self.__entities: self.__entities.append(element) else: raise RepoError("Element already exist!")
def add(self, book): """ Adds a given book to the repository Input: book - an object of the class Book Output: Exceptions: RepoError - if the book exists in the repository already """ if book in self._books: raise RepoError("! We already have this book.") self._books.append(book)
def search(self, element): """ function that search for an element in repository :param element: student/assignment/grade :return: - """ if element not in self.__entities: raise RepoError("Inexisting element") for elem in self.__entities: if elem == element: return elem
def list(self): """ Gives a list containing the clients from repository in a printable form Output: a list of the clients in the repository Exception: RepoError - if the repository is empty """ if len(self._clients) == 0: raise RepoError("! There is nothing to list.") clients = "ID \t Name \n" for c in self._clients: clients += str(c) + "\n" return clients
def list(self): """ Gives a list containing the books as printable strings Input: Output: a list of the books in the repository Exceptions: RepoError - if the repository is empty """ if len(self._books) == 0: raise RepoError("! Nothing to list") books = "ID \t Title \t Author \t\t\t\t Description \n" for i in range(0, len(self._books)): books += str(self._books[i]) + "\n" return books
def remove(self,rental ): """ Removes the given rental from the repository Input: rental - an object of the class Rental Exceptions: RepoError - if the rental does not exist in the repository """ if rental not in self._rentals: raise RepoError("! There is no such rental, so it cannot be removed") i=0 while i < len(self._rentals): if self._rentals[i] == rental: del self._rentals[i] return else: i+=1
def update(self, rental): """ Updates the given rental (changes every attribute except the rental id) Input: rental - an object of the class Rental Output: r - the oldest version of the updated rental Exception: RepoError - if the rental does not exist in the repository """ if rental not in self._rentals: raise RepoError("! We cannot update the rentas, as it does not exist.") for i in range (0,len(self._rentals)): if self._rentals[i] == rental: r = self._rentals[i] self._rentals[i] = rental return r
def _loadFile(self): try: f = open(self._fileName, "r") s = f.readline() while len(s) > 1: tok = s.split(",") tok[1] = tok[1].split("\n") client = Client(int(tok[0]), tok[1][0]) ClientRepository.add(self, client) s = f.readline() except Exception as e: raise RepoError("Error reading input file: " + str(e)) finally: f.close()
def update(self, client): """ Changes the name of the client with the same id as the given client Input: client - an object of the class Client Exception: RepoError - if the client does not exist in the repository """ if client not in self._clients: raise RepoError( "! This client does not exist, therefore cannot be updated.") for i in range(0, len(self._clients)): if self._clients[i] == client: c = self._clients[i] self._clients[i] = client return c
def update(self, book): """ Updates the given book ( changes only the title, author or description) Input: book - an object of the class Book Output: b - the old version of the book Exceptions: RepoError - if the book does not exist in the repository """ if book not in self._books: raise RepoError( "! We can't update the book because it doesn't exist.") for i in range(0, len(self._books)): if self._books[i] == book: b = self._books[i] self._books[i] = book return b
def _saveFile(self): try: f = open(self._fileName, "w") for b in BookRepository.get_books(self): """ function to turn entity into string """ if b.get_description() != None: f.write(str(b.get_id()) +","+b.get_title()+","+ b.get_author()+","+b.get_description()+"\n") else: f.write(str(b.get_id()) +","+b.get_title()+","+ b.get_author()+"\n") except Exception as e: raise RepoError("Error writing file" +str(e)) finally: f.close()
def remove(self, client): """ Removes the given client from the repository Input: client - an object of the class Client Exception: RepoError - if the client does not exist in the repository """ if client not in self._clients: raise RepoError( "! This client does not exist, therefore cannot be removed.") i = 0 while i < len(self._clients): if self._clients[i] == client: c = self._clients[i] del self._clients[i] else: i += 1 return c
def remove(self, book): """ Removes the given book from the repository Input: book - an object of the class Book Output: Exceptions: RepoError - if the book does not exist in the repository """ if book not in self._books: raise RepoError("! We don't even have that book.") i = 0 while i < len(self._books): if self._books[i] == book: b = self._books[i] del self._books[i] else: i += 1 return b
def _saveFile(self): try: f = open(self._fileName, "w") for r in RentalRepository.get_rentals(self): if r.get_returned_date() != None: f.write( str(r.get_id()) + "," + str(r.get_b_id()) + "," + str(r.get_c_id()) + "," + date_to_str_format(r.get_rent_date()) + "," + date_to_str_format(r.get_due_date()) + "," + date_to_str_format(r.get_returned_date()) + "\n") else: f.write( str(r.get_id()) + "," + str(r.get_b_id()) + "," + str(r.get_c_id()) + "," + date_to_str_format(r.get_rent_date()) + "," + date_to_str_format(r.get_due_date()) + "\n") except Exception as e: raise RepoError("Error writing file" + str(e)) finally: f.close()
def _loadFile(self): try: f = open(self._fileName, "r") s = f.readline() while len(s) > 1: tok = s.split(",") if len(tok) == 5: tok[4] = tok[4].split("\n") rental = Rental(int(tok[0]), int(tok[1]), int(tok[2]), tok[3], tok[4][0]) else: tok[5] = tok[5].split("\n") rental = Rental(int(tok[0]), int(tok[1]), int(tok[2]), tok[3], tok[4], tok[5][0]) RentalRepository.add(self, rental) s = f.readline() except Exception as e: raise RepoError("Error reading input file: " + str(e)) finally: f.close()