Question

The first part is “addBook method” the Second part is “checkOut method” the third part is...

The first part is “addBook method” the Second part is “checkOut method” the third part is “isAvailable method” and the fourth is “__str__ method”. What you have to do is complete the sample code provided below. do not change the sample code and print result. The program's goal is to keep track of books in a library. Books are stored in dictionaries, with the book title as the key.

Source Code is:

class Library:
bookCount = 0

def __init__(self):
self.cardCatalog = dict()
self.checkedOut = dict()

# PART 1
  
# Add the book to cardCatalog. title is the key, author is the value
# Add the book to checkedOut. title is the key, False is the value
def addBook(self, author, title):
pass

# PART 2
#
# Check to see if the title is in checkedOut dictionary.
# If so, set the value to True
def checkOut(self, author, title):
pass

# PART 3
#
# Check to see if the title is in checkedOut.
# If it isn't return False
# If it is, and the value is True, return False
# If the value is False, return True
# (Note that it appears reversed. Being checked out means it's not available
def isAvailable(self, author, title):
pass

# PART 4
#
# Return a string that contains multiple lines.
# Each line is a title followed by the author name.
# Hint: Create a string and append title, name, and a newline "\n"
# then return the string
def __str__(self):
pass

#
# Code to test the program
#
# DO NOT CHANGE THE CODE BELOW.
#
# Your output should be:
# False
# A Fall of Moondust Arthur C. Clarke
# Pandora's Star Peter F. Hamilton
# Ringworld Larry Niven
# <blank line> <= No brackets, just one blank line, makes __str__ easier
#

lib = Library()

lib.addBook("Peter F. Hamilton", "Pandora's Star")
lib.addBook("Arthur C. Clarke", "A Fall of Moondust")
lib.addBook("Larry Niven", "Ringworld")
lib.checkOut("Peter F. Hamilton", "Pandora's Star")
print(lib.isAvailable("Hamilton", "Pandora's Star"))
print(lib)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PYTHON CODE:

class Library:
    bookCount = 0

    def __init__(self):

        self.cardCatalog = dict()
        self.checkedOut = dict()

    # PART 1
    
    # Add the book to cardCatalog. title is the key, author is the value
    # Add the book to checkedOut. title is the key, False is the value
    def addBook(self, author, title):

        self.cardCatalog[title]=author
        self.checkedOut[title]=False
      

    # PART 2
    #
    # Check to see if the title is in checkedOut dictionary.
    # If so, set the value to True
    def checkOut(self, author, title):

        for key in self.checkedOut.keys():

            if key == title:
                self.checkedOut[title]=True
                break
          

    # PART 3
    #
    # Check to see if the title is in checkedOut.
    # If it isn't return False
    # If it is, and the value is True, return False
    # If the value is False, return True
    # (Note that it appears reversed. Being checked out means it's not available
    def isAvailable(self, author, title):


        for key,value in self.checkedOut.items():

            if key == title and value == True:
                return False

            if key == title and value == False:
                return True
          

    # PART 4
    #
    # Return a string that contains multiple lines.
    # Each line is a title followed by the author name.
    # Hint: Create a string and append title, name, and a newline "\n"
    # then return the string
    def __str__(self):

        s = ''

        titles = sorted(self.cardCatalog.keys())

        for title in titles:

            for key,value in self.cardCatalog.items():

                if title == key:

                    s += key +' '+ value+'\n'
                    break

        return s
          

#
# Code to test the program
#
# DO NOT CHANGE THE CODE BELOW.
#
# Your output should be:
# False
# A Fall of Moondust Arthur C. Clarke
# Pandora's Star Peter F. Hamilton
# Ringworld Larry Niven
# <blank line> <= No brackets, just one blank line, makes __str__ easier
#

lib = Library()

lib.addBook("Peter F. Hamilton", "Pandora's Star")
lib.addBook("Arthur C. Clarke", "A Fall of Moondust")
lib.addBook("Larry Niven", "Ringworld")
lib.checkOut("Peter F. Hamilton", "Pandora's Star")
print(lib.isAvailable("Hamilton", "Pandora's Star"))
print(lib)


SCREENSHOT FOR CODING:

class Library: bookCount = 0 def __init__(self): self.cardCatalog = dict() self.checkedOut = dict() # PART 1 # Add the book t

SCREENSHOT FOR OUTPUT:

False A Fall of Moondust Arthur C. Clarke Pandoras Star Peter F. Hamilton Ringworld Larry Niven >>>

Add a comment
Know the answer?
Add Answer to:
The first part is “addBook method” the Second part is “checkOut method” the third part is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hey I have a task which consists of two part. Part A asks for writing a...

    Hey I have a task which consists of two part. Part A asks for writing a program of WORD & LINE CONCORDANCE APPLICATION in python which I have completed it. Now the second part has given 5 dictionary implementation codes namely as: (ChainingDict, OpenAddrHashDict with linear probing, OpenAddrHashDict with quadratic probing, and 2 tree-based dictionaries from lab 12 (BST-based dictionary implementation) and asks for my above program WORD & LINE CONCORDANCE APPLICATION  to use these implemented code and show the time...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • Attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i nee...

    attention!!!!!!! I need python method!!!!!!!!! the part which need to edit is below: i need python one!!!!!!!!! the part below is interface for the range search tree which don’t need to modify it. Week 3: Working with a BST TODO: Implement a Binary Search Tree (Class Name: RangesizeTree) Choose one language fromJava or Python. Iin both languages,there is an empty main function in the Range Size Tree file. The main function is not tested, however, it is provided for you...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT