Creating Library Management System using Python

This is codded by Rakesh Giri
rakeshgiree999@gmail.com









#library management system
#create libData.txt to store data

import time
import json


class mgmt:
    def __init__(selflib_namelist_of_books):
        self.lib_name = lib_name
        self.list_of_books = list_of_books

    def lib_menu(self):
        print("***************************************************")
        print(f"**  Welcome to : {self.lib_name} ")
        print("***************************************************")
        print("**  Type '1' to display list of books available  **")
        print("**  Type '2' to lend book                        **")
        print("**  Type '3' to return book                      **")
        print("**  Type '4' to add book                         **")
        print("**  Type '5' to exit)                            **")
        print("**_______________________________________________**")

    def display_books(self):
        print("List of books : ")
        i = 1
        for book, stock in self.list_of_books.items():
            if stock == "on_stock":
                print(f"{i}. Book '{book}' is available")
            else:
                print(f"{i}. Book '{book}' is taken by {stock}.")
            i = i + 1
        print("**  Type any key and press enter to continue :")
        input("")

    def add_books(self):
        print("**  Enter the name of book that you want to add : ")
        book_name = input("")
        if book_name in self.list_of_books:
            print("**  We already have that book")
        else:
            dict_2 = {book_name: "on_stock"}
            self.list_of_books.update(dict_2)
            print("**  Updating list of books")
            time.sleep(3)
            print("**  List of books have been updated")
        print("**  Type any key and press enter to continue :")
        mgmt.log_data(self.list_of_books)
        input("")
        return self.list_of_books

    def return_books(self):
        print("**  Please enter your name :")
        ret_ch = input(" ")
        res = dict((v, k) for k, v in self.list_of_books.items())
        if ret_ch in res:
            print(
                f"**  Sir/Mam..{ret_ch}, You have taken these books on lend :")
            print(
                f"**  Type the number of book which you want to return and press enter :")
            i = 1
            for book, stock in self.list_of_books.items():
                if stock == ret_ch:
                    print(f"{i}{book}")
                    time.sleep(1)
                    print("**  Do you want to return this book ? type 'y' if yes : ")
                    ret_b = input(" ")
                    if ret_b == "y":
                        self.list_of_books[book] = "on_stock"
                        mgmt.log_data(self.list_of_books)
                    else:
                        print("**  You chosed to not return this book...")
                    i = i + 1
            else:
                print("***************************************************")
                time.sleep(3)
        else:
            print("You have not taken any books")
            time.sleep(3)


    def lend_books(self):
        print("**  Please ! Enter your name to lend book : ")
        lender = input(" ")
        print("**  Please ! Type the name of book that you want to lend : ")
        lend_book= input(" ")
        if lend_book.title() in self.list_of_books:
            for book, stock in self.list_of_books.items():
                if book==lend_book:
                    print("**  Do you really want to lend this books ? 'y' for yes : ")
                    r = input(" ")
                    if r=="y":
                        self.list_of_books[book] = lender
                        mgmt.log_data(self.list_of_books)
                        print("**  Successfully updated")
                        print(f"**  You have {book} on lend")
                        time.sleep(2)
                    else:
                        print(f"**  You choosed not to lend the book {book}")
                        time.sleep(2)
        else:
            print(f"**  We do not have {lend_book} book.")
            time.sleep(2)


    def log_data(data):
        with open("libData.txt""w"as file:
            file.write(json.dumps(data))

with open ("libData.txt""r"as f1:
    lib_dict = dict(json.loads(f1.read()))


a = mgmt("Himalayan Library", lib_dict)
while True:
    a.lib_menu()
    ch = input("**  Enter your choice : ")
    if (ch == "1"):
        a.display_books()
    elif (ch == "2"):
        a.lend_books()
    elif (ch == "3"):
        a.return_books()
    elif (ch == "4"):
        a.add_books()
    elif (ch == "5"):
        print("Updating the data")
        time.sleep(2)
        exit()
    else:
        pass

Comments