How to add insertion sort in this program? So that in every display, the books will be sorted according to its title.

Programming Logic & Design Comprehensive
9th Edition
ISBN:9781337669405
Author:FARRELL
Publisher:FARRELL
Chapter6: Arrays
Section: Chapter Questions
Problem 15RQ
icon
Related questions
Question
How to add insertion sort in this program? So that in every display, the books will be sorted according to its title.

#Library Management System

#Importing Libraries
import datetime
import os
#os.getcwd()

class LMS:
    """ This class is used to keep record of books library
    It has total four module: "Display book, "Issue Books", "Return Books", "Add Books" """

    def __init__(self, list_of_books, library_name):
        self.list_of_books = "list_of_books.txt"
        self.library_name = library_name
        self.books_dict = {} #This dictionary will contain books title, lender name, issue date, and book status
        Id = 101 #Starting ID

        #read the text file
        with open (self.list_of_books) as bk:
            content = bk.readlines()
        for line in content:
            self.books_dict.update({str(Id):{ #Updating Dictionary
            "books_title":line.replace("\n", ""),
            "lender_name": "",
            "issue_date": "",
            "Status":"Available"}})
            Id = Id + 1

    def display_books(self):
        print("------------------------List of Books---------------------")
        print("Books ID", "\t", "Title")
        print("----------------------------------------------------------")

        for key, value in self.books_dict.items():
            print(key, "\t\t", value.get("books_title"), "- [", value.get("Status"),"]")

    def Issue_books(self):
        books_id = input ("Enter the book's ID: ")
        current_date = datetime.datetime.now().strftime ("%Y-%m_%d %H:%M:%S")
        if books_id in self.books_dict.keys():
            if not self.books_dict[books_id]["Status"] == "Available":
                print(f"This book is already issued to {self.books_dict[books_id]['lender_name']} \
                    on {self.books_dict[books_id]['issue_date']}")
                return self.Issue_books()
            elif self.books_dict[books_id]['Status'] == "Available":
                name = input ("Enter your name: ")
                self.books_dict[books_id]['lender_name'] = name
                self.books_dict[books_id]['issue_date'] = current_date
                self.books_dict[books_id]['Status'] = "Already Issued"
                print ("Books Issued Successfully! \n")

        else:
            print("Book ID not found!")
            return self.Issue_books()

    def add_books(self):
        new_books = input("Enter the title of the book: ")
        if new_books == "":
            return self.add_books()
        elif len(new_books) > 25:
            print("The book's title is too long! Title length should be 20 chars")
            return self.add_books()
        else:
            with open(self.list_of_books, "a") as bk:
                bk.writelines(f"{new_books}\n")
                self.books_dict.update({str(int(max(self.books_dict))+1):{
                'books_title':new_books,
                'lender_name':"",
                'issue_date':"",
                'Status':"Available"}})
                print (f"This books '{new_books}' has been added successfully!")

    def return_books(self):
        books_id = input ("Enter the book's ID: ")
        if books_id in self.books_dict.keys():
            if self.books_dict[books_id]["Status"] == "Available":
                print("This book is already available in library. Please check your book ID.")
                return self.return_books()
            elif not self.books_dict[books_id]["Status"] == "Available":
                self.books_dict[books_id]['lender_name'] = ""
                self.books_dict[books_id]['Issue_date'] = ""
                self.books_dict[books_id]['Status'] = "Available"
                print("Successfully Returned")

        else:
            print("Book ID is not found")

try:
    myLMS = LMS("list_of_books.txt", "Python's")
    press_key_dict = {"D": "Display Books",
                    "I":"Issue Books",
                    "A": "Add Books",
                    "R": "Return Books",
                    "Q": "Quit"}    
    key_press = False
    while not (key_press == "q"):
        print(f"\n----------Welcome To {myLMS.library_name}'s Library Management System---------\n")
        for key, value in press_key_dict.items():
            print("Press", key, "To", value)
        key_press = input ("Press key: ").lower()
        if key_press == "i":
            print("\nCurrent Selection: Issue Books\n")
            myLMS.Issue_books()
        elif key_press == 'd':
            print("\nCurrent Selection: Display Books\n")
            myLMS.display_books()
        elif key_press == 'a':
            print("\nCurrent Selection: Add Books\n")
            myLMS.add_books()
        elif key_press == 'r':
            print("\nCurrent Selection: Return Books\n")
            myLMS.return_books()
        elif key_press == 'q':
            break
        else:
            continue    

except Exception as e:
    print("Something went wrong. Please check your input")
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

The solution code for the question about modifying the binary search to only display the result with Title of the book and its ID number where user's input is the title of the book is not working.

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Hello it is possible to modify the binary search to only display the result with Title of the book and its ID number where user's input is the title of the book?

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Can't we modify binary search to only display the result with book title with its ID number where the user will search a book using an ID?

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

My question is to modify binary search to only display the result with book title with its ID number.

Here is the code to modify:

    def search_books(self, book_title):
        # Remove leading and trailing white spaces and convert to lowercase
        book_title = book_title.strip().lower()
   
        # Extract book titles and IDs from self.books_dict
        book_titles = [value["books_title"].strip().lower() for key, value in self.books_dict.items()]
        #book_ids = list(self.books_dict.keys())
   
        # Sort the book titles and IDs together
        book_titles, book_ids = zip(*sorted(zip(book_titles, book_ids))) #zip takes iterable or containers and return a single iterator
   
        # Perform binary search for the book title
        low = 0
        high = len(book_titles) - 1
        while low <= high:
            mid = (low + high) // 2
            bt = book_titles[mid].replace('"', '')
            if bt == book_title:
                print(f"Book '{book_title}' is found in the Library.")
                return book_ids[mid]
            elif book_titles[mid] < book_title:
                low = mid + 1
            else:
                high = mid - 1
   
        print(f"Book '{book_title}' not found.")
        return None
Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

How to only display the result of the search with only title and its corresponding id? (eg. 101)

Solution
Bartleby Expert
SEE SOLUTION
Follow-up Question

Can you explain the algorithm of insertion sort here? Also, add code for the binary search to search for a book by title or author.

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Mergesort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage