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

Use Python to design a class Library and class Patron Patron should have the fol

ID: 3910272 • Letter: U

Question

Use Python to design a class Library and class Patron

Patron should have the following characteristics:

- Name

- Library account number, composed of 5 digits. Must be random and unique. Library account number cannot be the same as other patrons. Patron will not have a library account number until the library adds the patron into its system.

- List of books that the patron borrowed

Library should have the following characteristics:

- Name

- Location

- List of patrons

- List of available books

The library should be able to:

- Add a patron to the library. When the patron is added, a library account number is assigned to

the patron. You might want to delegate the task of creating a library account number to another

function.

- Lend out books. Make sure the person to lend to is a patron of the library. Also make sure the

book is available. Once the book is lent to the patron, the book is not available in the library’s

book list.

- Add books to its list of available books

Example Interaction:

Explanation / Answer

I have written classes according to the use cases you have given. Since the code is written in python, indentation is to be taken care of. While copying the code, if the indentation error occurs, please align it according to the comments I have written. I have tried to define the start and end of functions,loops and if-else statements which will help you to indent them properly.

Here are the classes:

import random


class Patron:
   'Common base class for all Patrons'
   library_account_number = None
   def __init__(self, name):
      self.name = name #Name of Patron
      self.borrowed_books = []
   #end __init__ definition

class Library:
   'Common base class for all Libraries'
   patrons = [] #List of patrons
   available_books = [] #List of available books

   numlst = []    #list of random library account numbers

   def __init__(self, name, address):
      self.name = name #Name of Library
      self.address = address #Address of library
   #end __init__ definition
   def add_book(self,book_name):
      self.available_books.append(book_name);
   #end add_book definition
   def add_patron(self,patron):

      count = 0
      while count != 100000000: #this number can go up to the max random number.
      #while start
         num = random.randint(10000,100000)     #to give random library account number of 5 digits
         #if inside while
         if num not in self.numlst:
            #inside if
            self.numlst.append(num)
            patron.library_account_number = num
            self.patrons.append(patron)
            break
         else:
            #inside else
            count += 1
            #end if else loop

   #end add_patron definition


   # __str__ function returns whatever is inside, this is returned in print(bk_library) command
   def __str__(self):
      print "Name:"+self.name
      print "Location:"+self.address
      print "Available Books:"
      for i in self.available_books:
         print i
      print "Library Patron Information:"
      for j in self.patrons:
         print "Name:"+j.name
         print "Library Account Number:"+str(j.library_account_number)
         print "Borrowed Books:"
         for k in j.borrowed_books:
            print k


      return ""
   #end __str__ definition


   def lend(self,patron,name_of_book):
      if patron in self.patrons:
      #start of if
         if name_of_book in self.available_books:
            #if statement inside if
            patron.borrowed_books.append(name_of_book)
            self.available_books.remove(name_of_book)
            #end of if statement inside if
         else:
            #else statement inside if
            print name_of_book+" is not available"
            #end of else statement inside if
      #end of if
      else:
      #start of else
         print patron.name+" is not a member of "+self.name
      #end of else
   #end lend definition