Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the function in Python please Provide explanations for each line of code Provide snapshot of code simple_books.txt QS12.03.001,The Lord Of The Rings QK12.04.002,The Hitchhiker's

Write the function in Python please Provide explanations for each line of code Provide snapshot of code

simple_books.txt
QS12.03.001,The Lord Of The Rings QK12.04.002,The Hitchhiker's Guide To The Galaxy QS12.02.003,The Dune Chronicles QA12.04.004,A Song Of Ice And Fire Series QS12.01.005,The Foundation Trilogy

Question 1 a) Define a class named MyLibrary which represents a simple library system. The simple library system should allow people to borrow, return and search for books in the collection. It also provides the functionalities such as displaying the list of available books and a list of on-loan books, etc. The MyLibrary class contains:

  • A private data field named __name that defines the name of a library.
  • A private data field named __books_list that defines a list of the Book objects in the library.
  • A constructor/initializer that takes a name and a filename as parameters (default of empty string). If the filename is not an empty string, the method should load all the books from a text file into the books list of the library system. Download the sample text files from here.
    • If the file does not exist, an error message is displayed as in the example below.
    • After loading all the books into the books list, the method should print the number of books loaded as in the example below.
  • The show_all_books(self) method which prints out all the books in the collection as shown in the example below.

Test Result
library = MyLibrary('MyLibrary','simple_books.txt') print() library.show_all_books()
5 books loaded. The Lord Of The Rings, QS12.03.001 (Available) The Hitchhiker's Guide To The Galaxy, QK12.04.002 (Available) The Dune Chronicles, QS12.02.003 (Available) A Song Of Ice And Fire Series, QA12.04.004 (Available) The Foundation Trilogy, QS12.01.005 (Available)
library = MyLibrary('MyLibrary','input_file.txt') library.show_all_books()
ERROR: The file 'input_file.txt' does not exist.

Question 1 b)

Extend the MyLibrary class by implementing the following methods:

  • The find_available_book(self, code) method which takes a book code as a parameter and searches through the collection of books based on a given code. If the book is found and it is available, the method should return the Book object, and Noneotherwise.
  • The borrow_book(self, code, borrower_name) method which takes a book code and a name of the borrower as parameters. The method should search for the book in the books list. If the book is available, the method should issue the book to the borrower by calling the borrow_book() method on the Book and print a message as in the example below. If the book is NOT available, the method should print: "ERROR: could not issue the book: 'XXX'" where XXX indicates the book code.
Test Result
library = MyLibrary('MyLibrary', 'simple_books.txt') print(library.find_available_book('QS12.02.003')) print(library.find_available_book('003'))
5 books loaded. The Dune Chronicles, QS12.02.003 (Available) None
library = MyLibrary('MyLibrary','simple_books.txt') library.borrow_book('QS12.02.003', 'Michael') library.borrow_book('003', 'Michael')
5 books loaded. 'The Dune Chronicles' is issued to Michael successfully. ERROR: could not issue the book: '003'.
library = MyLibrary('MyLibrary','simple_books.txt') library.borrow_book('QS12.02.003', 'Michael') library.borrow_book('QS12.02.003', 'May') print() library.show_all_books()
5 books loaded. 'The Dune Chronicles' is issued to Michael successfully. ERROR: could not issue the book: 'QS12.02.003'. The Lord Of The Rings, QS12.03.001 (Available) The Hitchhiker's Guide To The Galaxy, QK12.04.002 (Available) The Dune Chronicles, QS12.02.003 (On Loan) A Song Of Ice And Fire Series, QA12.04.004 (Available) The Foundation Trilogy, QS12.01.005 (Available)
library = MyLibrary('MyLibrary','simple_books.txt') library.borrow_book('QS12.02.003', 'Michael') library.borrow_book('003', 'Michael') print(library.find_available_book('QS12.02.003')) print(library.find_available_book('003'))
5 books loaded. 'The Dune Chronicles' is issued to Michael successfully. ERROR: could not issue the book: '003'. None None

Question 1 c)

Extend the MyLibrary class by implementing the following methods:

  • The find_on_loan_book(self, code) method which takes a book code as a parameter and and searches through the collection of books based on the parameter code. If the book is found and it is on loan, the method should return the Book object, and None otherwise.
  • The return_book(self, code) method which takes a book code as a parameter and searches through the collection of books based on the parameter code. If the book is on loan, the method should return the book and print a message as in the example below. If the book is not "on loan" or cannot be found, the method should print "ERROR: could not return the book: XXX" where XXX indicates the book code.
Test Result
library = MyLibrary('MyLibrary', 'simple_books.txt') library.borrow_book('QS12.02.003', 'Michael') library.return_book('QS12.02.003') print(library.find_available_book('QS12.02.003'))
5 books loaded. 'The Dune Chronicles' is issued to Michael successfully. 'QS12.02.003' is returned successfully. The Dune Chronicles, QS12.02.003 (Available)
library = MyLibrary('MyLibrary', 'simple_books.txt') library.return_book('QS12.02.003') library.return_book('003')
5 books loaded. ERROR: could not return the book: 'QS12.02.003'. ERROR: could not return the book: '003'.

Question 1 d)

Extend the MyLibrary class by implementing the following methods:

  • The show_available_books(self) method which prints all the available books in the library system.
  • The show_on_loan_books(self) method which prints all the on loan books in the library system.
Test Result
library = MyLibrary('MyLibrary', 'simple_books.txt') library.borrow_book('QS12.02.003', 'Michael') library.borrow_book('QK12.04.002', 'Michael') print("Available books:") library.show_available_books() print("On loan books:") library.show_on_loan_books()
5 books loaded. 'The Dune Chronicles' is issued to Michael successfully. 'The Hitchhiker's Guide To The Galaxy' is issued to Michael successfully. Available books: The Lord Of The Rings, QS12.03.001 (Available) A Song Of Ice And Fire Series, QA12.04.004 (Available) The Foundation Trilogy, QS12.01.005 (Available) On loan books: The Hitchhiker's Guide To The Galaxy, QK12.04.002 (On Loan) The Dune Chronicles, QS12.02.003 (On Loan)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Why could the Robert Bosch approach make sense to the company?

Answered: 1 week ago