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

Hi, i need help with this question, the answer has to be in Python 3.x. Help ple

ID: 3859866 • Letter: H

Question

Hi, i need help with this question, the answer has to be in Python 3.x. Help please, thanks!

A simple software system for a library models a library as a collection of books and patrons. A patron can have at most three books out on loan at any given time. Each book has a title, an author, a patron to whom it has been checked out, and a list of patrons waiting for that book to be returned. When a patron wants to borrow a book, that patron is automatically added to the book's wait list if the book is already checked out. When a patron returns a book, it is automatically loaned to the first patron on its wait list who can check out a book. Each patron has a name and the number of books that patron has currently checked out. Develop the classes Book and Patron to model these objects. Think first of the interface or set of methods to be used with each class, and then choose appropriate data structures for the state of the objects. Develop a Library class that can manage the books and patrons. This class should include methods for adding, removing, and finding books and patrons. Develop a Manager class that provides a menu-driven command processor for managing a library

Explanation / Answer

1ans;

class cPatron(object):   

    def __init__(self, name):
        self.name = name
        self.borrowed = []
    def borrow(self, bname):
        if len(self.borrowed) >= 3:
            print self.name + " >> " + " has already borrowed 3 books, must return 1 book."
            return None
        
        cBook = cLib.dic.get(bname)
        if cBook.name == None:
            print bname + " is not in the library."
            return None
    
        if cBook.loan == '':
            self.borrowed.append(cBook)
            cBook.loan = self.name
            print self.name + " >> " + cBook.name + " is loaned to " +self.name
        else:
            cBook.wait.append(self)
            print self.name + " >> " + cBook.name + " is loaned to " + cBook.loan
                  + " so you are wait listed."
    
    def back(self, bname):
        for index in range(len(self.borrowed)):
            if bname == self.borrowed[index].name:
                cBook = self.borrowed[index]
                self.borrowed.pop(index)
                break
        if cBook != None:
            print self.name + " >> " + bname + " is returned to the library."
            cBook.loan =''
            if len(cBook.wait) > 0:
                print self
                cBook.wait[0].borrow(cBook.name)
                cBook.wait.pop(0)
            else:
                cBook.loan = ''
                print cBook.name+ " is available."
        else:
            print self.name + " >> " + " don't have " +bname+ "."
        
class cBook(object):
    def __init__(self, name, author):
        self.name = name
        self.author = author
        self.wait = []
        self.loan = ''
    def __str__(self):
        print self.name, self.author, self.wait, self.loan
class cLib(object):
    dic = {}
    member = {}
    def __init__(self):
        print " Welcome to the Library!!!"
    def addBook(self, cBook):
        cLib.dic[cBook.name] = cBook
        ##        self.dic[cBook.name] = cBook
    def addMember(self, cPatron):
##        self.member[cPatron.name] = cPatron
        cLib.member[cPatron.name] = cPatron
def main():
    lib = cLib()
    tm = cPatron('taemin')
    ty = cPatron('taeyoon')
    lib.addBook(cBook('t', 'tt'))
    lib.addBook(cBook('y', 'yy'))
    lib.addBook(cBook('z', 'zz'))
    lib.addBook(cBook('a', 'aa'))
    ty.borrow('t')
    tm.borrow('t')
    ty.borrow('y')
    ty.borrow('z')
    ty.borrow('a')
    ty.back('t')
    tm.borrow('a')
main()

2ans;

class cPatron(object):
    def __init__(self, name):
        self.name = name
        self.borrowed = []
        print "Welcome to the library" + name
    def borrow(self, bname):
        if len(self.borrowed) >= 3:
            print self.name + " >> " + " has already borrowed 3 books, must return 1 book."
            return None
        
        cBook = cLib.dic.get(bname)
        if cBook.name == None:
            print bname + " is not in the library."
            return None
    
        if cBook.loan == '':
            self.borrowed.append(cBook)
            cBook.loan = self.name
            print self.name + " >> " + cBook.name + " is loaned to " +self.name
        else:
            cBook.wait.append(self)
            print self.name + " >> " + cBook.name + " is loaned to " + cBook.loan
                  + " so you are wait listed."
    
    def back(self, bname):
        for index in range(len(self.borrowed)):
            if bname == self.borrowed[index].name:
                cBook = self.borrowed[index]
                self.borrowed.pop(index)
                break
        if cBook != None:
            print self.name + " >> " + bname + " is returned to the library."
            cBook.loan =''
            if len(cBook.wait) > 0:
                cBook.wait[0].borrow(cBook.name)
                cBook.wait.pop(0)
            else:
                cBook.loan = ''
                print cBook.name+ " is available."
        else:
            print self.name + " >> " + " don't have " +bname+ "."
        
class cBook(object):
    def __init__(self, name, author = None):
        self.name = name
        self.author = author
        self.wait = []
        self.loan = ''
    def __str__(self):
        print self.name, self.author, self.wait, self.loan
class cLib(object):
    dic = {}
    member = {}
##    def __init__(self):
        
    def addBook(self, cBook):
        self.dic[cBook.name] = cBook
        print "A book called" + cBook.name + " is added to the library!"
        
    def addMember(self, cPatron):
        self.member[cPatron.name] = cPatron
        
    def removeBook(self, bname):
        if self.dic.get(bname, None) != None:
            if self.dic[bname].loan == '':
                self.dic.pop(bname)
                print bname + " is removed from the library."
            else:
                print bname + " is loaned to " + self.dic[bname].loan + ", so cannot be removed."
        else:
            print bname + " is not in the library."
        
    def findBook(self, bname):
        if self.dic.get(bname) == None:
            print bname + " is not in the library."
        else:
            print bname + " is in the library."
    def listBook(self):
         print "Here is the list of names of books in the library."
         bList = self.dic.keys()
         for index in range(len(bList)):
             print str(index+1) + " : " + bList[index]
def main():
    print "Welcome to the Library!!!"
    print "please login"
    patron = cPatron(raw_input("Enter your name >> "))
    lib = cLib()
    while True:
        print "Choose a command"
        print " 1. add a book to the library."
        print " 2. remove a book from the library."
        print " 3. Borrow a book."
        print " 4. Return a book."
        print " 5. Find a book."
        print " 6. quit."
        command = int(raw_input(">>"))
        if command == 6:
            print "Good Bye!!!"
            break
        elif command == 1:
            bname = raw_input("Enter a book name >> ")
            aname = raw_input("Enter the book's author name >> ")
            lib.addBook(cBook(bname, aname))
        elif command == 2:
            lib.listBook()
            bname = raw_input("Enter name of a book to be removed >> ")
            lib.removeBook(bname)
        elif command == 3:
            lib.listBook()
            bname = raw_input("Enter name of a book you wish to borrow >> ")
            patron.borrow(bname)
        elif command == 4:
            lib.listBook()
            bname = raw_input("Enter name of a book you wish to return >> ")
            patron.back(bname)
        elif command == 5:
            lib.listBook()             
  
main()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote