0% found this document useful (0 votes)
8 views27 pages

Code

The document outlines a library management system implemented in Python, utilizing MySQL for database management. It includes functionalities for users to add, search, issue, and return books, as well as manage user accounts and library policies. The code features various user menus and input handling to facilitate interaction with the library system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views27 pages

Code

The document outlines a library management system implemented in Python, utilizing MySQL for database management. It includes functionalities for users to add, search, issue, and return books, as well as manage user accounts and library policies. The code features various user menus and input handling to facilitate interaction with the library system.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

# Importing necessary libraries

import [Link]
import pyfiglet
import wikipediaapi
from datetime import datetime

# Connect to the MySQL database


# Note: You may need to change these credentials to match your MySQL setup.
db = [Link](
host="localhost",
user="root",
password="pappu2008",
database="library",
)
c = [Link]()

# Global variable to store the currently logged-in user's ID


USERID = 0

# Function to display the return policy information


def returnPolicy():
print("Return Policy : ")
print("The issued book should be returned within 14 days(2 weeks).")
print(
"If the user kept the issued book for more than 14 days, then the "
"user have to pay ₹5 as fine for each extra day the user kept the issued "
"book."
)
print(" ")

# Function to calculate the length of a given integer for formatting


def length(i):
s = str(i)
length = len(s) + 2
return length

# Function to display a message for an invalid option


def validOption():
print("Please enter a valid option!")
print(" ")

def exiting():
# Prints the banner with bold red text
Exit_text = pyfiglet.figlet_format("EXITING THE PROGRAM THANK YOU",
font="slant", width=1000)
print(Exit_text)
exit()

# Function to display the user menu and handle user choices


def userMenu():
print("1. Add Note")
print("2. Home")
print("3. Back")
print("4. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: addNote()
elif userChoice == 2: home()
elif userChoice == 3: user()
elif userChoice == 4: exiting()
else:
validOption()

# Function to display information about the library


def aboutLibrary():
[Link]("SELECT userName FROM users WHERE adminStatus='admin'")
userName = [Link]()
[Link]("SELECT * FROM books")
totalBooks = [Link]()
[Link]("SELECT * FROM users")
totalUsers = [Link]()
[Link]()
print(" ")
print("About Library")
print(" ")
print("Year of Library's Establishment : ", 2023)
print("Name of the Librarian : ", userName[0][0])
print("Total Number of Books Available in the Library : ", len(totalBooks))
print("Total Number of Users Enrolled in the Library : ", len(totalUsers))
print(" ")
userMenu()

# Function to display the list of books in the library


def displayBooks():
print(" ")
print("Display Books")
print(" ")
[Link]("SELECT * FROM books ORDER BY bookId")
result = [Link]()
[Link]()
if result:
print("Books available in the Digital Library are :")
print(" ")
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. Book ID : {row[0]}")
print(" " * r + f"Book Name : {row[1]}")
print(" " * r + f"Publication Year : {row[2]}")
print(" " * r + f"Author Name : {row[7]}")
print(" " * r + f"Issue Status : {row[8]}")
print(" ")
userMenu()
else:
print("No books found.")
print(" ")
userMenu()

# Menu for after searching books


def searchBooksMenu():
print("1. Add Note")
print("2. Home")
print("3. Back")
print("4. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
if userChoice == 1: addNote()
elif userChoice == 2: home()
elif userChoice == 3: searchBooks()
elif userChoice == 4: exiting()
else:
validOption()

# Function to search books by Book ID


def searchBooksbyId():
print(" ")
print("Search Books by Book ID")
print(" ")
bookId = int(input("Enter the Book ID to search the Book : "))
print(" ")
[Link]("SELECT * FROM books WHERE bookId=%s", (bookId,))
result = [Link]()
[Link]()
if result:
print(f'Book available in the Digital Library with the Book ID "{bookId}"
is :')
print(" ")
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. Book ID : {row[0]}")
print(" " * r + f"Book Name : {row[1]}")
print(" " * r + f"Publication Year : {row[2]}")
print(" " * r + f"Author Name : {row[7]}")
print(" " * r + f"Issue Status : {row[8]}")
print(" ")
searchBooksMenu()
else:
print(f'No book found with the book id "{bookId}".')
print(" ")
searchBooksMenu()

# Function to search books by keyword


def searchBooksbyKeyword():
print(" ")
print("Search Books by Keyword")
print(" ")
keyword = input("Enter a Keyword to search Books : ")
print(" ")
[Link]("SELECT * FROM books WHERE bookName LIKE '%{}%' ORDER BY
bookId".format(keyword))
result = [Link]()
[Link]()
if result:
print(f'Books available in the Digital Library with the Keyword "{keyword}"
are :')
print(" ")
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. Book ID : {row[0]}")
print(" " * r + f"Book Name : {row[1]}")
print(" " * r + f"Publication Year : {row[2]}")
print(" " * r + f"Author Name : {row[7]}")
print(" " * r + f"Issue Status : {row[8]}")
print(" ")
searchBooksMenu()
else:
print(f'No books found with the keyword "{keyword}".')
print(" ")
searchBooksMenu()

# Function to display search options for books


def searchBooks():
print(" ")
print("Search Books")
print(" ")
print("1. Search by Book ID")
print("2. Search by Keyword")
print("3. Home")
print("4. Back")
print("5. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: searchBooksbyId()
elif userChoice == 2: searchBooksbyKeyword()
elif userChoice == 3: home()
elif userChoice == 4: user()
elif userChoice == 5: exiting()
else:
validOption()

# Function to display the add book menu and handle user choices
def addBookMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: modifyBook()
elif userChoice == 3: exiting()
else:
validOption()

# Function to add a new book to the library


def addBook():
print(" ")
print("Add Book")
print(" ")
bookId = int(input("Enter the Book ID : "))
bookName = input("Enter the Book Name : ")
publicationYear = int(input("Enter the Book Publication Year : "))
author = input("Enter the Book Author Name : ")
print(" ")
[Link]("SELECT bookId FROM books")
result = [Link]()
[Link]()
if (bookId,) in result:
print(f'The book of book id "{bookId}" is already available in the digital
library.')
print(" ")
addBookMenu()
else:
[Link](
"INSERT INTO books (bookId, bookName, publicationYear, author) VALUES
(%s, %s, %s, %s)",
(bookId, bookName, publicationYear, author),
)
[Link]()
print("Book added Successfully!")
print(" ")
addBookMenu()

# Function to display the delete book menu and handle user choices
def deleteBookMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: modifyBook()
elif userChoice == 3: exiting()
else:
validOption()

# Function to delete a book from the library


def deleteBook():
print(" ")
print("Delete Book")
print(" ")
bookId = int(input("Enter the Book ID : "))
choice = input("Are you sure to delete the Book? (Yes/No) : ")
print(" ")
[Link]("SELECT bookId FROM books")
result = [Link]()
[Link]()
if [Link]() in ["yes", "y"]:
if (bookId,) in result:
[Link]("DELETE FROM books WHERE bookId=%s", (bookId,))
[Link]()
print("Book deleted Successfully!")
print(" ")
deleteBookMenu()
else:
print(f'The book of book id "{bookId}" does not available in the
digital library.')
print(" ")
deleteBookMenu()
elif [Link]() in ["no", "n"]:
print("Book Not Deleted!")
print(" ")
deleteBookMenu()
else:
validOption()

# Update book menu options


def updateBookMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: updateBook()
elif userChoice == 3: exiting()
else:
validOption()

def notBook(bookId):
print(f'The book of book id "{bookId}" does not available in the digital
library.')
print(" ")
updateBookMenu()

# Function to update book details


def updateBook():
print(" ")
print("Update Book Details")
print(" ")
print("1. Update the Book ID")
print("2. Update the Book Name")
print("3. Update the Book Publication Year")
print("4. Update the Book Author Name")
print("5. Home")
print("6. Back")
print("7. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
[Link]("SELECT bookId FROM books")
result = [Link]()
[Link]()
if userChoice == 1:
currentBookId = int(input("Enter the Current Book ID : "))
newBookId = int(input("Enter the New Book ID : "))
if (currentBookId,) in result:
[Link]("UPDATE books SET bookId=%s WHERE bookId=%s", (newBookId,
currentBookId))
[Link]()
print("Book ID changed Successfully!")
print(" ")
updateBookMenu()
else:
notBook(currentBookId)
elif userChoice == 2:
bookId = int(input("Enter the Book ID : "))
newBookName = input("Enter the New Book Name : ")
if (bookId,) in result:
[Link]("UPDATE books SET bookName=%s WHERE bookId=%s", (newBookName,
bookId))
[Link]()
print("Book Name changed Successfully!")
print(" ")
updateBookMenu()
else:
notBook(bookId)
elif userChoice == 3:
bookId = int(input("Enter the Current Book ID : "))
newPublicationYear = input("Enter the New Publication Year : ")
if (bookId,) in result:
[Link]("UPDATE books SET publicationYear=%s WHERE bookId=%s",
(newPublicationYear, bookId))
[Link]()
print("Book Publication Year changed Successfully!")
print(" ")
updateBookMenu()
else:
notBook(bookId)
elif userChoice == 4:
bookId = int(input("Enter the Current Book ID : "))
newAuthor = input("Enter the New Author Name : ")
if (bookId,) in result:
[Link]("UPDATE books SET author=%s WHERE bookId=%s", (newAuthor,
bookId))
[Link]()
print("Book Author Name changed Successfully!")
print(" ")
updateBookMenu()
else:
notBook(bookId)
elif userChoice == 5: home()
elif userChoice == 6: modifyBook()
elif userChoice == 7: exiting()
else:
validOption()

# Function to display the issue book menu and handle user choices
def issueBookMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: admin()
elif userChoice == 3: exiting()
else:
validOption()

# Function to issue a book


def issueBook():
print(" ")
print("Issue Book")
print(" ")
bookId = int(input("Enter the Book ID to be Issued: "))
userId = int(input("Enter the User ID to whom Book will be Issued: "))

[Link]("SELECT userId FROM users")


result1 = [Link]()
[Link]("SELECT bookId FROM books")
result2 = [Link]()
[Link]("SELECT issueStatus FROM books WHERE bookId=%s", (bookId,))
result3 = [Link]()
[Link]()

if (userId,) in result1:
if (bookId,) in result2:
if result3[0][0] == "not issued":
[Link]("UPDATE books SET issueDate = CURRENT_DATE WHERE bookId =
%s", (bookId,))
[Link]("UPDATE books SET issueTime = CURRENT_TIME WHERE bookId =
%s", (bookId,))
[Link]("UPDATE books SET issueStatus = 'issued' WHERE bookId =
%s", (bookId,))
[Link]("UPDATE books SET returnDate = NULL WHERE bookId = %s",
(bookId,))
[Link]("UPDATE books SET returnTime = NULL WHERE bookId = %s",
(bookId,))
[Link]("UPDATE books SET issuedUserId = %s WHERE bookId = %s",
(userId, bookId))
[Link]()

[Link]("SELECT issuedUserId, bookName, issueDate, issueTime FROM


books WHERE bookId=%s", (bookId,))
result = [Link]()
[Link](
"INSERT INTO issuedBooksDetails (userId, bookId, bookName,
issueDate, issueTime) VALUES (%s, %s, %s, %s, %s)",
(result[0][0], bookId, result[0][1], result[0][2], result[0]
[3])
)
[Link]()

print(" ")
print(f'Book of Book Id "{bookId}" is issued successfully to the
User of User Id "{userId}".')
print(" ")
returnPolicy()
issueBookMenu()
else:
print(f'The book of book id "{bookId}" is already issued by another
user.')
print(" ")
issueBookMenu()
else:
print(f"Book with book id {bookId} does not available in the digital
library.")
print(" ")
issueBookMenu()
else:
print(f"User with user id {userId} does not exists in the digital
library.")
print(" ")
issueBookMenu()

# Function to display the return book menu and handle user choices
def returnBookMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: admin()
elif userChoice == 3: exiting()
else:
validOption()

# Function to return a book


def returnBook():
print(" ")
print("Return Book")
print(" ")
bookId = int(input("Enter the Book ID to be Returned: "))
[Link]("SELECT bookId FROM books")
result1 = [Link]()
[Link]("SELECT issueStatus FROM books WHERE bookId=%s", (bookId,))
result2 = [Link]()
[Link]()

if (bookId,) in result1:
if result2[0][0] == "issued":
[Link]("UPDATE books SET returnDate = CURRENT_DATE WHERE bookId =
%s", (bookId,))
[Link]("UPDATE books SET returnTime = CURRENT_TIME WHERE bookId =
%s", (bookId,))
[Link]("UPDATE books SET issueStatus = 'not issued' WHERE bookId =
%s", (bookId,))
[Link]()

[Link]("SELECT issuedUserId, returnDate, returnTime FROM books WHERE


bookId=%s", (bookId,))
result = [Link]()

[Link](
"UPDATE issuedBooksDetails SET returnDate = %s, returnTime = %s
WHERE userId = %s AND bookId = %s",
(result[0][1], result[0][2], result[0][0], bookId)
)
[Link]()

[Link]("UPDATE books SET issuedUserId = NULL WHERE bookId = %s",


(bookId,))
[Link]()

print(f'The book of book id "{bookId}" is returned successfully.')

[Link]("SELECT issueDate FROM books WHERE bookId = %s", (bookId,))


issueDate = [Link]()
[Link]("SELECT returnDate FROM books WHERE bookId = %s", (bookId,))
returnDate = [Link]()
[Link]()

[Link]("UPDATE books SET issueDate = NULL WHERE bookId = %s",


(bookId,))
[Link]("UPDATE books SET issueTime = NULL WHERE bookId = %s",
(bookId,))
[Link]("UPDATE books SET returnDate = NULL WHERE bookId = %s",
(bookId,))
[Link]("UPDATE books SET returnTime = NULL WHERE bookId = %s",
(bookId,))
[Link]()

d1 = [Link](f"{issueDate[0][0]}", "%Y-%m-%d")
d2 = [Link](f"{returnDate[0][0]}", "%Y-%m-%d")
dateDifference = abs(d2 - d1)

if [Link] > 14:


extraDays = [Link] - 14
fine = extraDays * 5
print("Fine(in Rs.) : ", fine)
[Link](
"UPDATE issuedBooksDetails SET fineInRs=%s WHERE userId=%s AND
bookId=%s",
(fine, result[0][0], bookId)
)
[Link]()
else:
fine = 0
print("Fine(in Rs.) : ", fine)
[Link](
"UPDATE issuedBooksDetails SET fineInRs=%s WHERE userId=%s AND
bookId=%s",
(fine, result[0][0], bookId)
)
[Link]()

print(" ")
returnBookMenu()
else:
print(f'The book of book id "{bookId}" is not issued by any user.')
print(" ")
returnBookMenu()
else:
print(f"Book with book id {bookId} does not available in the digital
library.")
print(" ")
returnBookMenu()

# Function to display the add user menu and handle user choices
def addUserMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: modifyUser()
elif userChoice == 3: exiting()
else:
validOption()

# Function to add a new user


def addUser():
print(" ")
print("Add User")
print(" ")
userId = int(input("Enter the User ID : "))
userName = input("Enter the User Name : ")
userPhoneNumber = input("Enter the User Phone Number : ")
userEmailId = input("Enter the User Email ID : ")
password = input("Enter the User Password : ")
print(" ")
[Link]("SELECT userId FROM users")
result = [Link]()
[Link]()
if (userId,) in result:
print(f'The user of user number "{userId}" is already enrolled in the
digital library.')
print(" ")
addUserMenu()
else:
[Link](
"INSERT INTO users (userId, userName, phoneNumber, emailId, password)
VALUES (%s, %s, %s, %s, %s)",
(userId, userName, userPhoneNumber, userEmailId, password),
)
[Link]()
print(" ")
print("User added successfully!")
print(" ")
addUserMenu()

# Function to display the delete user menu and handle user choices
def deleteUserMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: modifyUser()
elif userChoice == 3: exiting()
else:
validOption()

# Function to delete a user


def deleteUser():
print(" ")
print("Delete User")
print(" ")
userId = int(input("Enter the User ID : "))
choice = input("Are you sure to delete the User? (Yes/No) : ")
[Link]("SELECT userId FROM users")
result = [Link]()
[Link]()
if [Link]() in ["yes", "y"]:
if (userId,) in result:
[Link]("DELETE FROM users WHERE userId=%s", (userId,))
[Link]()
print("User deleted successfully!")
print(" ")
deleteUserMenu()
else:
print(f'The user of user id "{userId}" does not enrolled in the digital
library.')
print(" ")
deleteUserMenu()
elif [Link]() in ["no", "n"]:
print("User Not Deleted!")
print(" ")
deleteUserMenu()
else:
validOption()

# Function to display the update user menu and handle user choices
def updateUserMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
if userChoice == 1: home()
elif userChoice == 2: updateUser()
elif userChoice == 3: exiting()
else:
validOption()

def notUser(userId):
print(f'The user of user id "{userId}" does not enrolled in the digital
library.')
print(" ")
updateUserMenu()

# Function to update user details


def updateUser():
print(" ")
print("Update User Details")
print(" ")
print("1. Update the User ID")
print("2. Update the User Name")
print("3. Update the User Phone Number")
print("4. Update the User Email ID")
print("5. Update the User Password")
print("6. Home")
print("7. Back")
print("8. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
[Link]("SELECT userId FROM users")
result = [Link]()
[Link]()
if userChoice == 1:
currentUserId = int(input("Enter the Current User ID : "))
newUserId = int(input("Enter the New User ID : "))
if (currentUserId,) in result:
[Link]("UPDATE users SET userId=%s WHERE userId=%s", (newUserId,
currentUserId))
[Link]()
print("User ID changed Successfully!")
print(" ")
updateUserMenu()
else:
notUser(currentUserId)
elif userChoice == 2:
userId = int(input("Enter the User ID : "))
newUserName = input("Enter the New User Name : ")
if (userId,) in result:
[Link]("UPDATE users SET userName=%s WHERE userId=%s", (newUserName,
userId))
[Link]()
print("User Name changed Successfully!")
print(" ")
updateUserMenu()
else:
notUser(userId)
elif userChoice == 3:
userId = int(input("Enter the Current User ID : "))
newPhoneNumber = input("Enter the New Phone Number : ")
if (userId,) in result:
[Link]("UPDATE users SET phoneNumber=%s WHERE userId=%s",
(newPhoneNumber, userId))
[Link]()
print("User Phone Number changed Successfully!")
print(" ")
updateUserMenu()
else:
notUser(userId)
elif userChoice == 4:
userId = int(input("Enter the Current User ID : "))
newEmailId = input("Enter the New Email ID : ")
if (userId,) in result:
[Link]("UPDATE users SET emailId=%s WHERE userId=%s", (newEmailId,
userId))
[Link]()
print("User Email ID changed Successfully!")
print(" ")
updateUserMenu()
else:
notUser(userId)
elif userChoice == 5:
userId = int(input("Enter the Current User ID : "))
newPassword = input("Enter the New Password : ")
if (userId,) in result:
[Link]("UPDATE users SET password=%s WHERE userId=%s", (newPassword,
userId))
[Link]()
print("User Password changed Successfully!")
print(" ")
updateUserMenu()
else:
notUser(userId)
elif userChoice == 6: home()
elif userChoice == 7: modifyUser()
elif userChoice == 8: exiting()
else:
validOption()

# Function to modify user


def modifyUser():
print(" ")
print("Modify User")
print(" ")
print("1. Add User")
print("2. Delete User")
print("3. Update User Details")
print("4. Home")
print("5. Back")
print("6. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: addUser()
elif userChoice == 2: deleteUser()
elif userChoice == 3: updateUser()
elif userChoice == 4: home()
elif userChoice == 5: admin()
elif userChoice == 6: exiting()
else:
validOption()

# Display users menu options


def displayUsersMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
if userChoice == 1: home()
elif userChoice == 2: admin()
elif userChoice == 3: exiting()
else:
validOption()

# Function to display all users


def displayUsers():
print(" ")
print("Display Users")
print(" ")
[Link]("SELECT * FROM users ORDER BY userId")
result = [Link]()
[Link]()
if result:
print("Users enrolled in the Digital Library are :")
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. User ID : {row[0]}")
print(" " * r + f"User Name : {row[1]}")
print(" " * r + f"Phone Number : {row[2]}")
print(" " * r + f"Email ID : {row[3]}")
print(" " * r + f"Admin Status : {row[5]}")
print(" ")
displayUsersMenu()
else:
print("No users found.")
print(" ")
displayUsersMenu()

# Search user menu options


def searchUsersMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
if userChoice == 1: home()
elif userChoice == 2: searchUsers()
elif userChoice == 3: exiting()
else:
validOption()

# Function to search users by ID


def searchUsersbyId():
print(" ")
print("Search Users by User ID")
print(" ")
userId = int(input("Enter the User ID to search the User : "))
[Link]("SELECT * FROM users WHERE userId=%s", (userId,))
result = [Link]()
[Link]()
if result:
print(f'User enrolled in the Digital Library with the User ID "{userId}" is
:')
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. User ID : {row[0]}")
print(" " * r + f"User Name : {row[1]}")
print(" " * r + f"Phone Number : {row[2]}")
print(" " * r + f"Email ID : {row[3]}")
print(" " * r + f"Admin Status : {row[5]}")
print(" ")
searchUsersMenu()
else:
print(f'No user found with the user id "{userId}".')
print(" ")
searchUsersMenu()

# Function to search users by keyword


def searchUsersbyKeyword():
print(" ")
print("Search Users by Keyword")
print(" ")
keyword = input("Enter a Keyword to search Users : ")
[Link]("SELECT * FROM users WHERE userName LIKE '%{}%' ORDER BY
userId".format(keyword))
result = [Link]()
[Link]()
if result:
print(f'Users enrolled in the Digital Library with the Keyword "{keyword}"
are :')
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. User ID : {row[0]}")
print(" " * r + f"User Name : {row[1]}")
print(" " * r + f"Phone Number : {row[2]}")
print(" " * r + f"Email ID : {row[3]}")
print(" " * r + f"Admin Status : {row[5]}")
print(" ")
searchUsersMenu()
else:
print(f'No users found with the keyword "{keyword}".')
print(" ")
searchUsersMenu()

# Function to search users


def searchUsers():
print(" ")
print("Search Users")
print(" ")
print("1. Search by User ID")
print("2. Search by Keyword")
print("3. Home")
print("4. Back")
print("5. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: searchUsersbyId()
elif userChoice == 2: searchUsersbyKeyword()
elif userChoice == 3: home()
elif userChoice == 4: admin()
elif userChoice == 5: exiting()
else:
validOption()

# Function to modify books


def modifyBook():
print(" ")
print("Modify Book")
print(" ")
print("1. Add Book")
print("2. Delete Book")
print("3. Update Book Details")
print("4. Home")
print("5. Back")
print("6. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: addBook()
elif userChoice == 2: deleteBook()
elif userChoice == 3: updateBook()
elif userChoice == 4: home()
elif userChoice == 5: admin()
elif userChoice == 6: exiting()
else:
validOption()

# Function to manage notes


def notes():
print(" ")
print("Notes")
print(" ")
print("1. Modify Note")
print("2. Display Notes")
print("3. Search Notes")
print("4. Home")
print("5. Back")
print("6. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: modifyNote()
elif userChoice == 2: displayNotes()
elif userChoice == 3: searchNotes()
elif userChoice == 4: home()
elif userChoice == 5: user()
elif userChoice == 6: exiting()
else:
validOption()

# Function to display the add note menu and handle user choices
def addNoteMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
if userChoice == 1: home()
elif userChoice == 2: modifyNote()
elif userChoice == 3: exiting()
else:
validOption()

# Function to add note


def addNote():
print(" ")
print("Add Note")
print(" ")
noteNumber = int(input("Enter the Note Number : "))
noteTitle = input("Enter the Note Title : ")
noteDescription = input("Enter the Note Description : ")
print(" ")
[Link]("SELECT noteNumber FROM notes WHERE userId=%s", (USERID,))
result = [Link]()
[Link]()
if (noteNumber,) in result:
print(f'The note of note number "{noteNumber}" is already exists in the
digital library.')
print(" ")
addNoteMenu()
else:
[Link](
"INSERT INTO notes (userId, noteNumber, noteTitle, noteDescription,
updateDate, updateTime) VALUES (%s, %s, %s, %s, CURRENT_DATE, CURRENT_TIME)",
(USERID, noteNumber, noteTitle, noteDescription),
)
[Link]()
print(f'The note of note number "{noteNumber}" is added successfully.')
print(" ")
addNoteMenu()

# Function to display the delete note menu and handle user choices
def deleteNoteMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: modifyNote()
elif userChoice == 3: exiting()
else:
validOption()

# Function to delete a note


def deleteNote():
print(" ")
print("Delete Note")
print(" ")
noteNumber = int(input("Enter the Note Number to Delete the Note : "))
choice = input("Are you sure to delete the Note? (Yes/No) : ")
print(" ")
[Link]("SELECT noteNumber FROM notes WHERE userId=%s", (USERID,))
result = [Link]()
[Link]()
if [Link]() in ["yes", "y"]:
if (noteNumber,) in result:
[Link]("DELETE FROM notes WHERE userId=%s AND noteNumber=%s",
(USERID, noteNumber))
[Link]()
print(f'The note of note number "{noteNumber}" is deleted
successfully.')
print(" ")
deleteNoteMenu()
else:
print(f'The note of note number "{noteNumber}" does not exists in the
digital library.')
print(" ")
deleteNoteMenu()
elif [Link]() in ["no", "n"]:
print("Note Not Deleted!")
print(" ")
deleteNoteMenu()
else:
validOption()

# Function to display the update notes menu and handle user choices
def updateNotesMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: updateNotes()
elif userChoice == 3: exiting()
else:
validOption()

def notNote(noteNumber):
print(f'The note of note number "{noteNumber}" does not exists in the digital
library.')
print(" ")
updateNotesMenu()

# Function to update a note


def updateNotes():
print(" ")
print("Update Notes")
print(" ")
print("1. Update the Note Number")
print("2. Update the Note Title")
print("3. Update the Note Description")
print("4. Home")
print("5. Back")
print("6. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
[Link]("SELECT noteNumber FROM notes WHERE userId=%s", (USERID,))
result = [Link]()
[Link]()
if userChoice == 1:
currentNoteNumber = int(input("Enter the Current Note Number : "))
newNoteNumber = int(input("Enter the New Note Number : "))
if (currentNoteNumber,) in result:
[Link]("UPDATE notes SET updateDate=CURRENT_DATE WHERE userId=%s AND
noteNumber=%s", (USERID, currentNoteNumber))
[Link]("UPDATE notes SET updateTime=CURRENT_TIME WHERE userId=%s AND
noteNumber=%s", (USERID, currentNoteNumber))
[Link]("UPDATE notes SET noteNumber=%s WHERE userId=%s AND
noteNumber=%s", (newNoteNumber, USERID, currentNoteNumber))
[Link]()
print("Note Number changed Successfully!")
print(" ")
updateNotesMenu()
else:
notNote(currentNoteNumber)
elif userChoice == 2:
noteNumber = int(input("Enter the Current Note Number : "))
newTitle = input("Enter the New Note Title : ")
if (noteNumber,) in result:
[Link]("UPDATE notes SET updateDate=CURRENT_DATE WHERE userId=%s AND
noteNumber=%s", (USERID, noteNumber))
[Link]("UPDATE notes SET updateTime=CURRENT_TIME WHERE userId=%s AND
noteNumber=%s", (USERID, noteNumber))
[Link]("UPDATE notes SET noteTitle=%s WHERE userId=%s AND
noteNumber=%s", (newTitle, USERID, noteNumber))
[Link]()
print("Note Title changed Successfully!")
print(" ")
updateNotesMenu()
else:
notNote(noteNumber)
elif userChoice == 3:
noteNumber = int(input("Enter the Current Note Number : "))
newDescription = input("Enter the New Note Description : ")
if (noteNumber,) in result:
[Link]("UPDATE notes SET updateDate=CURRENT_DATE WHERE userId=%s AND
noteNumber=%s", (USERID, noteNumber))
[Link]("UPDATE notes SET updateTime=CURRENT_TIME WHERE userId=%s AND
noteNumber=%s", (USERID, noteNumber))
[Link]("UPDATE notes SET noteDescription=%s WHERE userId=%s AND
noteNumber=%s", (newDescription, USERID, noteNumber))
[Link]()
print("Note Description changed successfully!")
print(" ")
updateNotesMenu()
else:
notNote(noteNumber)
elif userChoice == 4: home()
elif userChoice == 5: modifyNote()
elif userChoice == 6: exiting()
else:
validOption()

# Function to handle note modifications


def modifyNote():
print(" ")
print("Modify Notes")
print(" ")
print("1. Add Note")
print("2. Delete Note")
print("3. Update Notes")
print("4. Home")
print("5. Back")
print("6. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: addNote()
elif userChoice == 2: deleteNote()
elif userChoice == 3: updateNotes()
elif userChoice == 4: home()
elif userChoice == 5: user()
elif userChoice == 6: exiting()
else:
validOption()

# Function to display the display notes menu and handle user choices
def displayNotesMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: notes()
elif userChoice == 3: exiting()
else:
validOption()

# Function to display notes


def displayNotes():
[Link]("SELECT * FROM notes WHERE userId=%s ORDER BY noteNumber", (USERID,))
result = [Link]()
[Link]()
if result:
print("Notes available in the Digital Library are :")
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. Note Number : {row[1]}")
print(" " * r + f"Note Title : {row[2]}")
print(" " * r + f"Note Description : {row[3]}")
print(" " * r + f"Update Date : {row[4]}")
print(" " * r + f"Update Time : {row[5]}")
print(" ")
displayNotesMenu()
else:
print("No notes found.")
print(" ")
displayNotesMenu()

# Function to display the search notes menu and handle user choices
def searchNotesMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
if userChoice == 1: home()
elif userChoice == 2: searchNotes()
elif userChoice == 3: exiting()
else:
validOption()

# Function to search notes by note number


def searchNotesbynoteNumber():
noteNumber = int(input("Enter the Note Number to search the Note : "))
[Link]("SELECT * FROM notes WHERE userId=%s AND noteNumber=%s", (USERID,
noteNumber))
result = [Link]()
[Link]()
if result:
print(f'Note available in the Digital Library with the Note Number
"{noteNumber}" is :')
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. Note Number : {row[1]}")
print(" " * r + f"Note Title : {row[2]}")
print(" " * r + f"Note Description : {row[3]}")
print(" ")
searchNotesMenu()
else:
print(f'No note found with the note number "{noteNumber}".')
print(" ")
searchNotesMenu()

# Function to search notes by keyword


def searchNotesbyKeyword():
print(" ")
print("Search Notes by Keyword")
print(" ")
keyword = input("Enter a Keyword to search Notes : ")
[Link]("SELECT * FROM notes WHERE userId=%s AND noteTitle LIKE '%{}%' ORDER
BY noteNumber".format(keyword), (USERID,))
result = [Link]()
[Link]()
if result:
print(f'Notes available in the Digital Library with the Keyword "{keyword}"
are :')
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. Note Number : {row[1]}")
print(" " * r + f"Note Title : {row[2]}")
print(" " * r + f"Note Description : {row[3]}")
print(" ")
searchNotesMenu()
else:
print(f'No notes found with the keyword "{keyword}".')
print(" ")
searchNotesMenu()

# Function to handle note searching


def searchNotes():
print(" ")
print("Search Notes")
print(" ")
print("1. Search by Note Number")
print("2. Search by Keyword")
print("3. Home")
print("4. Back")
print("5. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: searchNotesbynoteNumber()
elif userChoice == 2: searchNotesbyKeyword()
elif userChoice == 3: home()
elif userChoice == 4: notes()
elif userChoice == 5: exiting()
else:
validOption()

# Function to display the change admin menu and handle user choices
def changeAdminMenu():
print("1. Home")
print("2. Back")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1: home()
elif userChoice == 2: admin()
elif userChoice == 3: exiting()
else:
validOption()

# Function to change the admin status


def changeAdmin():
print(" ")
print("Change Admin")
print(" ")
newAdminId = int(input("Enter the New Admin's User ID : "))
newAdminPassword = input("Enter the New Admin's Password : ")
choice = input("Are you sure to change the Admin? (Yes/No) : ")
print(" ")
[Link]("SELECT password FROM users WHERE userId=%s", (newAdminId,))
result = [Link]()
[Link]()
if [Link]() in ["yes", "y"]:
if len(result) == 0:
print("Please enter a valid user id!")
else:
if newAdminPassword == result[0][0]:
[Link]("UPDATE users SET adminStatus='not admin' WHERE
adminStatus ='admin'")
[Link]("UPDATE users SET adminStatus='admin' WHERE userId =%s",
(newAdminId,))
[Link]()
print("Admin Changed Successfully!")
print(" ")
changeAdminMenu()
else:
print("Please enter a valid password!")
elif [Link]() in ["no", "n"]:
print("Admin Not Changed!")
print(" ")
changeAdminMenu()
else:
validOption()

# Function to authenticate admin


def authAdmin():
print(" ")
print("Admin Authentication")
print(" ")
adminId = int(input("Enter the Admin's User ID : "))
adminPassword = input("Enter the Admin's User Password : ")
[Link]("SELECT password FROM users WHERE userId=%s", (adminId,))
result = [Link]()
[Link]()
if len(result) == 0:
print(" ")
print("Please enter a valid user id!")
print(" ")
else:
if adminPassword == result[0][0]:
global USERID
USERID = adminId

print("Admin is verified successfully")

admin()
else:
print("Please enter a valid password!")
print(" ")

# Function to display the admin menu


def admin():
print(" ")
print("Admin")
print(" ")
print("1. Login into User Panel")
print("2. Modify User")
print("3. Display Users")
print("4. Search Users")
print("5. Modify Book")
print("6. Issue Book")
print("7. Return Book")
print("8. Change Admin")
print("9. Home")
print("10. Back")
print("11. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1:
print("You are successfully login into user panel.")
print(" ")
user()
elif userChoice == 2: modifyUser()
elif userChoice == 3: displayUsers()
elif userChoice == 4: searchUsers()
elif userChoice == 5: modifyBook()
elif userChoice == 6: issueBook()
elif userChoice == 7: returnBook()
elif userChoice == 8: changeAdmin()
elif userChoice == 9: home()
elif userChoice == 10: authAdmin()
elif userChoice == 11: exiting()
else:
validOption()

# Function to authenticate a user


def authUser():
print(" ")
print("User Authentication")
print(" ")
userId = int(input("Enter the User ID : "))
password = input("Enter the User Password : ")
[Link]("SELECT password FROM users WHERE userId=%s", (userId,))
result = [Link]()
[Link]()
if len(result) == 0:
print(" ")
print("Please enter a valid user id!")
print(" ")
else:
if password == result[0][0]:
global USERID
USERID = userId

print("User is verified successfully")

user()
else:
print("Please Enter a Valid Password!")
print(" ")

# Function to search & display the wikipedia articles


def wikipediaArticles():
def fetchingArticle(keyword, articleLength=1500):
wiki = [Link](language="en",
user_agent="digital-library/1.1")
page = [Link](keyword)
if not [Link]():
print(f'Sorry, the Wikipedia Article for the keyword "{keyword}" does
not exists.')
print(" ")
else:
print("Title : ")
print([Link])
print("URL : ")
print([Link])
print("Summary : ")
start = 0
end = 157
article = [Link][:articleLength]
while end <= articleLength:
print(article[start:end])
start += 157
end += 157
else:
print(article[start:]) # Print any remaining part of the article
print(" ")

print("Wikipedia Articles")
print(" ")
keyword = input("Enter the Keyword for searching the Wikipedia Article : ")
articleLength = int(input("Enter the Article Length : "))
print(" ")
fetchingArticle(keyword, articleLength)
userMenu()

# Function to display the issued books details of a user


def issuedBooksDetails():
print(" ")
print("Issued Books Details")
print(" ")
returnPolicy()
[Link]("SELECT * FROM issuedBooksDetails WHERE userId=%s ORDER BY bookId",
(USERID,))
result = [Link]()
[Link]()
if not result:
print("No Books Issued!")
print(" ")
userMenu()
else:
i = 0
for row in result:
i += 1
r = length(i)
print(f"{i}. Book ID : ", row[1])
print(" " * r + "Book Name : ", row[2])
print(" " * r + "Issue Date : ", row[3])
print(" " * r + "Issue Time : ", row[4])
print(" " * r + "Return Date : ", row[5])
print(" " * r + "Return Time : ", row[6])
print(" " * r + "Fine(in Rs.) : ", row[7])
print(" ")
userMenu()

# Function to display the user menu


def user():
print(" ")
print("User")
print(" ")
[Link]('SELECT userId FROM users WHERE adminStatus="admin"')
result = [Link]()
[Link]()
if result and result[0][0] == USERID: # Check if the list is not empty before
accessing
print("1. Login into Admin Panel")
print("2. About the Library")
print("3. Wikipedia Articles")
print("4. Display Books")
print("5. Search Books")
print("6. Issued Books Details")
print("7. Notes")
print("8. Home")
print("9. Back")
print("10. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print(" ")
if userChoice == 1:
print("You are successfully login into admin panel.")
print(" ")
admin()
elif userChoice == 2: aboutLibrary()
elif userChoice == 3: wikipediaArticles()
elif userChoice == 4: displayBooks()
elif userChoice == 5: searchBooks()
elif userChoice == 6: issuedBooksDetails()
elif userChoice == 7: notes()
elif userChoice == 8: home()
elif userChoice == 9: authUser()
elif userChoice == 10: exiting()
else:
validOption()
else:
print("1. About Library")
print("2. Wikipedia Articles")
print("3. Display Books")
print("4. Search Books")
print("5. Issued Books Details")
print("6. Notes")
print("7. Home")
print("8. Back")
print("9. Exit")

userChoice = int(input("Enter your Choice to Continue : "))


print(" ")
if userChoice == 1: aboutLibrary()
elif userChoice == 2: wikipediaArticles()
elif userChoice == 3: displayBooks()
elif userChoice == 4: searchBooks()
elif userChoice == 5: issuedBooksDetails()
elif userChoice == 6: notes()
elif userChoice == 7: home()
elif userChoice == 8: authUser()
elif userChoice == 9: exiting()

else:
validOption()

# Function to display the main menu


def home():
while True:

print("============================================================================
===================================================================================
=========================")
# The following line combines the banner into a single pyfiglet call
welcome_text = pyfiglet.figlet_format("WELCOME TO THE DIGITAL LIBRARY",
font="slant", width=1000)
# This prints the banner in bold red, without the extra tilde lines
print(welcome_text)

print("============================================================================
===================================================================================
=========================")
print("\t")
print("Home")
print("\t")
print("1. Admin")
print("2. User")
print("3. Exit")
userChoice = int(input("Enter your Choice to Continue : "))
print("\t")
if userChoice == 1: authAdmin()
elif userChoice == 2: authUser()
elif userChoice == 3: exiting()
else:
validOption()
# Call the main menu function to start the program
if __name__ == "__main__":
home()

You might also like