Prototype Source Code
BY SUDHAKAR NATH CHOWDHURY
library_books = {
"The Hitchhiker's Guide to the Galaxy": {
"author": "Douglas Adams",
"genre": "Science Fiction",
"is_available": True,
"borrowed_by": None # Will store the user who borrowed it
},
"Pride and Prejudice": {
"author": "Jane Austen",
"genre": "Classic",
"is_available": True,
"borrowed_by": None
},
"1984": {
"author": "George Orwell",
"genre": "Dystopian",
"is_available": False,
"borrowed_by": "user1" # Example of a borrowed book
},
"To Kill a Mockingbird": {
"author": "Harper Lee",
"genre": "Classic",
"is_available": True,
"borrowed_by": None
}
}
users = {
"admin": "admin_pass", # Admin password
"user1": "user_pass1", # Example user password
"user2": "user_pass2"
}
# --- Constants ---
MAX_ATTEMPTS = 3
# --- Helper Functions ---
def get_valid_menu_input(prompt, valid_options):
"""Ensures user input is an integer and within valid options."""
while True:
try:
choice = int(input(prompt))
if choice in valid_options:
return choice
else:
print(f"Please enter a valid number from the menu ({', '.join(map(str, valid_options))}).")
except ValueError:
print("Invalid input. Please enter a number.")
def authenticate(account_type):
"""Handles user authentication for admin or regular users."""
attempts = 0
if account_type == "admin":
print("\n--- ADMIN LOGIN ---")
username = "admin"
correct_pass = users["admin"]
else: # user login
print("\n--- USER LOGIN ---")
username = input("Enter your username: ").strip().lower()
if username not in users or username == "admin":
print("Username not found or invalid type for user login.")
return None, False # Return None for username and False for success
correct_pass = users[username]
while attempts < MAX_ATTEMPTS:
p = input("Enter the password: ")
if p == correct_pass:
print(f"WELCOME {username.upper()}!")
return username, True
else:
print("PASSWORD INCORRECT!")
attempts += 1
print("Too many incorrect attempts. Exiting.")
return None, False
# --- Admin Functions ---
def admin_menu():
"""Displays and handles admin specific operations."""
while True:
print("\n--- ADMIN MENU ---")
print("1) See all books in library (current status)")
print("2) See books currently issued")
print("3) Add a new book")
print("4) Go back to main menu")
admin_choice = get_valid_menu_input("Enter your choice: ", [1, 2, 3, 4])
if admin_choice == 1:
print("\n--- All Books in Library ---")
if not library_books:
print("No books in the library.")
else:
for title, details in library_books.items():
status = "Available" if details["is_available"] else f"Borrowed by {details['borrowed_by']}"
print(f"- Title: {title}, Author: {details['author']}, Status: {status}")
elif admin_choice == 2:
print("\n--- Books Currently Issued ---")
issued_found = False
for title, details in library_books.items():
if not details["is_available"]:
print(f"- Title: {title}, Borrowed by: {details['borrowed_by']}")
issued_found = True
if not issued_found:
print("No books are currently issued.")
elif admin_choice == 3:
print("\n--- Add New Book ---")
new_title = input("Enter the title of the new book: ").strip()
if new_title in library_books:
print(f"'{new_title}' already exists in the library.")
else:
new_author = input(f"Enter the author of '{new_title}': ").strip()
new_genre = input(f"Enter the genre of '{new_title}': ").strip()
library_books[new_title] = {
"author": new_author,
"genre": new_genre,
"is_available": True,
"borrowed_by": None
}
print(f"'{new_title}' by {new_author} has been added to the library.")
elif admin_choice == 4:
break # Exit admin menu loop
# --- User Functions ---
def user_menu(current_user):
"""Displays and handles user specific operations."""
while True:
print(f"\n--- USER MENU ({current_user.upper()}) ---")
print("1) Search and borrow a book")
print("2) See books you have borrowed")
print("3) Return a book")
print("4) Go back to main menu")
user_choice = get_valid_menu_input("Enter your choice: ", [1, 2, 3, 4])
if user_choice == 1:
print("\n--- Available Books ---")
available_books_found = False
for title, details in library_books.items():
if details["is_available"]:
print(f"- Title: {title}, Author: {details['author']}, Genre: {details['genre']}")
available_books_found = True
if not available_books_found:
print("No books currently available for borrowing.")
continue # Go back to user menu
book_to_borrow = input("Enter the title of the book you want to borrow: ").strip()
if book_to_borrow in library_books:
if library_books[book_to_borrow]["is_available"]:
library_books[book_to_borrow]["is_available"] = False
library_books[book_to_borrow]["borrowed_by"] = current_user
print(f"You have successfully borrowed '{book_to_borrow}'.")
else:
print(f"Sorry, '{book_to_borrow}' is currently borrowed by {library_books[book_to_borrow]['borrowed_by']}.")
else:
print(f"'{book_to_borrow}' not found in the library.")
elif user_choice == 2:
print(f"\n--- Books borrowed by {current_user.upper()} ---")
borrowed_found = False
for title, details in library_books.items():
if not details["is_available"] and details["borrowed_by"] == current_user:
print(f"- Title: {title}, Author: {details['author']}")
borrowed_found = True
if not borrowed_found:
print("You have not borrowed any books.")
elif user_choice == 3:
print(f"\n--- Return a Book ---")
books_to_return = []
for title, details in library_books.items():
if not details["is_available"] and details["borrowed_by"] == current_user:
books_to_return.append(title)
if not books_to_return:
print("You don't have any books to return.")
else:
print("Books you have borrowed:")
for book in books_to_return:
print(f"- {book}")
book_to_return = input("Enter the title of the book you want to return: ").strip()
if book_to_return in books_to_return:
library_books[book_to_return]["is_available"] = True
library_books[book_to_return]["borrowed_by"] = None
print(f"'{book_to_return}' has been successfully returned. Thank you!")
else:
print(f"You did not borrow '{book_to_return}' or it's not listed for your account.")
elif user_choice == 4:
break # Exit user menu loop
# --- Main Program Flow ---
print("WELCOME TO SUDHAKAR's LIBRARY (PROTOTYPE)")
while True:
print("\n--- MAIN MENU ---")
print("1) ADMIN LOGIN")
print("2) USER LOGIN")
print("3) EXIT")
main_menu_choice = get_valid_menu_input("ENTER THE NUMBER FROM THE MENU AS YOU WANT TO PROCEED: ", [1, 2, 3])
if main_menu_choice == 1:
username, authenticated = authenticate("admin")
if authenticated:
admin_menu()
elif main_menu_choice == 2:
username, authenticated = authenticate("user")
if authenticated:
user_menu(username) # Pass the authenticated username to the user menu
elif main_menu_choice == 3:
print("Thank you for using Sudhakar's Library. This is a prototype, full code is in progress")
break # Exit the main program loop