Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PYTHON!! thanks! suppose theres a class called books and a class called Patron a

ID: 3573689 • Letter: P

Question

PYTHON!! thanks!

suppose theres a class called books and a class called Patron and a class called library

how would i complete these two functions

1) return_book(self, library, book_id) :: patron asks the Library to reshelve this book. (call a method form the library parameter and let it do the reshelving). o Returns True when the book was successfully returned, and False if the process couldn't be completed for any reason (and don't modify the library or patron when False is returned). o exceptions might be raised via code from the Library class; you must catch them and return False, making no modifications to the library or patron. (which would these be? How will you deal with them?)

2) check_out_book_by_id(self, library, book_id) :: patron asks the Library to lend the Book with that id#. (call a method of the library parameter to request the given book). o Returns True when the book was successfully checked out, and False in all other instances (not modifying the original library or this patron). o Don't worry about multiple matches.

Explanation / Answer

This depends on the implementation of the Library and the Book class.

The classes must be something like this:

class Book:
    id = 0
    name = ''
    def __init__(id, name):
        self.id = id
        self.name = name

class Library:
    id = 0
    books = []
    def __init__(id, books=[]):
        self.id = id
        self.books = books

Methods

def return_book(self, library, book_id):

    try:

        library.books.append(book_id)

        return True

    except e:

        return False

def check_out_book_by_id(self, library, book_id):

    if book_id in library.books:

        library.books.remove(book_id)

        return True

    else:

        return False