Exercise 11

.docx

School

Old Dominion University *

*We aren’t endorsed by this school

Course

150

Subject

Computer Science

Date

Apr 3, 2024

Type

docx

Pages

7

Uploaded by GeneralPigeon2812

Report
Upgrading the Library Management System This tutorial will guide you through upgrading our Library Management System to utilize a nested dictionary instead of the list. The primary advantage of using dictionaries in this example is their ability to store more complex, structured data, enabling us to keep track not only of book titles but also of how many copies are available, who has borrowed them, and more. #Our old Book Repository was a list: books = [] Now, let's modify the system to use a nested dictionary. This will allow us to store each book's title along with the number of copies available, the number of copies loaned out, and a list of borrowers. Step1: ### Initializing the Book Repository with Nested Dictionaries We'll transition from a list to a dictionary named `books`. Each key in this dictionary is a book title, and the value is a nested dictionary that tracks the number of copies (`copies`), the number of copies currently loaned out (`loaned`), and a list of borrowers (`loaned_to`). Your task is to initialize this dictionary with the given book titles and their corresponding information. Your task is to initialize the `books` dictionary with the provided book entries, following the structure described above. Define a variable `books` as a dictionary to store the book inventory. Each key in the `books` dictionary is a string representing a book title. The value corresponding to each key is another dictionary with the following keys and values: A key `copies` with an integer value indicating the total number of copies of the book. A key `loaned` with an integer value starting at `0`, representing the number of copies currently loaned out. A key `loaned_to` with an empty list value `[]`, which will hold the names of the individuals who have borrowed copies of the book. Step2: ### Modifying the Functions to Work with Dictionaries. Here is a Summary of the Required Tasks 1. Code the `add_book(title, copies)` function to manage new additions or updates to the inventory. 2. Implement `loan_out(title, borrower_name)` 3. Code the `return_book(title, borrower_name)` to handle the return process. 4. Code the `display_inventory()` to show the current state of all books in the library. 5. Develop `display_loan_history()` to review which users have borrowed books. 6. Code the `search_for_book(query)` to find and display information on specific books. 7. Implement `most_popular_books()` to find which books are favorites among users (Extra Credit).
1. Adding Books: Our previous `add_book` function simply added a title to the list. We'll modify it to update our dictionary. 1. Define a function `add_book` that takes two parameters: `title` (a string) and `copies` (an integer) . 2. Check if the `title` already exists in the `books` dictionary.    - If it does, increment the `copies` value for that book title by the additional number of `copies` provided.    - If it does not, add a new entry to the `books` dictionary with the `title` as the key and a new dictionary as the value. The nested dictionary should include `copies` (set to the number provided), `loaned` (set to 0), and `loaned_to` (set to an empty list). 3. Print a confirmation message with the title and the number of copies added. 2. Loaning Out a book: The `loan_out` function in the old system, checked the loaned_books_count not to exceed 5 and updated this number when the loan was successful as shown below: In our new code, since the dictionary stores the number of copies and the names of the individuals who have borrowed copies of the book, The function needs to record the borrower's name and updates the loan count for the book. Note: there is no need to have a global variable “loaned_books_count” in this version of the code we will just check and update each book’s loan count without having an upper limit for loaning books (for now).
# Updated loan_out function for nested dictionaries here is the code please add your modification: *Notice how we need the borrower_name as input for the function to update the list * The new condition will not only check that the book is in the repository (check if the “title” is in the dictionary), it will also check if there are available copies to loan 3. Return Book: The `return_book` function will reverse the actions of `loan_out`, updating the loaned count and removing the borrower from the list: **what will the new condition check? 4. Display Inventory: A function to display the current state of the inventory, which means display the entries of the books dictionary. It provides the total number of copies and how many are currently loaned out. 1. Define a function `display_inventory` with no parameters. 2. Loop through the `books` dictionary using a `for` loop that retrieves both the key (book title) and the value (book info). 3. For each book, print the title along with the number of `copies` and the number of copies `loaned` from the book info. 5. Display Loan History:
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help