Question

In: Computer Science

Write a Python class to represent a LibraryMember that takes books from a library. A member...

Write a Python class to represent a LibraryMember that takes books from a library. A member has four private attributes, a name, an id, a fine amount, and the number of books borrowed. Your program should have two functions to add to the number of books and reduce the number of books with a member. Both functions, to add and return books, must return the final number of books in hand. Your program should print an error message whenever the member tries to take more than 3 books with the message “Note: You cannot have more than 3 books.” and return the value -1.

The following test case reveals the use of the class. Define a Python class to match the test case given below.

#Test: Do NOT change this code

member1 = LibraryMember()
member1.setName("John")
member1.setID("4521")
member1.setBooks(2) # set number of books in hand

member1.setFine(27.5) # set fine amount

print(member1.showInfo()) # Shows all the information of a member

print("Books in hand:", member1.returnBook(2)) # Return 2 books


member2 = LibraryMember("Mariam", "4531", 5)

print("Books in hand:", member2.takeBook(6)) # Take 6 books

Solutions

Expert Solution

This is the Python class created as per the given requirements.

I have created all the required functions and the private data members and also tested it with the given statements.

Thank you.

class LibraryMember:

    def __init__(self,name = "",id = "",books = 0,fine_amt = 0.0):

        self.name = name

        self.id = id

        self.fine_amt = fine_amt

        self.books = books

    def setName(self,name):

        self.name = name

    def setID(self,id):

        self.id = id

    

    def setBooks(self,books):

        self.books = books

    def setFine(self,fine_amt):

        self.fine_amt = fine_amt

    def takeBook(self,newBooks):

        if(newBooks>3):

            print("Note: You cannot have more than 3 books.")

            return -1

        else:

            self.books = self.books + newBooks

            return self.books

    

    def returnBooks(self,retBook):

        if(retBook>self.books):

            print("Books to be returned is greater than the number of books taken!")

            return -1

        else:

            self.books = self.books - retBook

            return self.books

    def showInfo(self):

        info = {"Name":self.name,"ID":self.id,"Fine Amount": self.fine_amt,"Books": self.books}

        return info

member1 = LibraryMember()

member1.setName("John")

member1.setID("4521")

member1.setBooks(2)

member1.setFine(27.5)

print(member1.showInfo())

print("Books in hand: ",member1.returnBooks(2))

member2 = LibraryMember("Mariam","4531",5)

print("Books in hand:",member2.takeBook(6))


Related Solutions

write the code in python Design a class named PersonData with the following member variables: lastName...
write the code in python Design a class named PersonData with the following member variables: lastName firstName address city state zip phone Write the appropriate accessor and mutator functions for these member variables. Next, design a class named CustomerData , which is derived from the PersonData class. The CustomerData class should have the following member variables: customerNumber mailingList The customerNumber variable will be used to hold a unique integer for each customer. The mailingList variable should be a bool ....
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill...
Write a Python class definition called CellPhone to represent a monthly cell phone bill. The bill should contain the following information: customer name (default value is the empty string) account number (default value is 0) number of gigabytes (GB) used over the monthly limit (default value is 0) Include the following methods: a constructor which given a customer name, account number, and number of GB used in excess of the monthly limit, creates a CellPhone object (be sure to account...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...
LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler....
Write a Python program that takes information from the user about a spider seen in North...
Write a Python program that takes information from the user about a spider seen in North America and returns whether it is likely to be a dangerous spider. The only two spiders in North America that are actually dangerous are the widow and recluse. The program will ask the user for the following information: color and special markings. printWelcome – prints program information printColorMenu – Color menu options should be brown, black, other printSpecialMarkingsMenu – Special markings options should be...
write a Python program that takes information from the user about a spider seen in North...
write a Python program that takes information from the user about a spider seen in North America and returns whether it is likely to be a dangerous spider. The only two spiders in North America that are actually dangerous are the widow and recluse. The program will ask the user for the following information: color and special markings. printWelcome – prints program information printColorMenu – Color menu options should be brown, black, other printSpecialMarkingsMenu – Special markings options should be...
Write a Python program which takes a set of positive numbers from the input and returns...
Write a Python program which takes a set of positive numbers from the input and returns the sum of the prime numbers in the given set. The sequence will be ended with a negative number.
Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
Write a program in Python that - Takes in a gross salary. - If the gross...
Write a program in Python that - Takes in a gross salary. - If the gross salary is above €50.000 taxes are 50%. - If the gross salary is above €25.000 taxes are 25%. - If the gross salary is above €10.000 taxes are 10%. - If the gross salary is below €10.000 there is no tax. - Output the gross salary and the net income. Note: Give the pseudocode of your program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT