Untitled

 avatar
unknown
plain_text
a month ago
3.0 kB
11
Indexable
# User database
users = []
books = []
def register(username, password):
    if username and password:
        users.append({'username': username, 'password': password})
        print("Registration successful!")
    else:
        print("Please fill in all fields!")

def login(username, password):
    user = next((u for u in users if u['username'] == username and u['password'] == password), None)
    if user:
        print("Login successful!")
    else:
        print("Invalid username or password!")

def add_book(title, author, year, book_no):
    if title and author and year and book_no:
        books.append({'title': title, 'author': author, 'year': year, 'book_no': book_no})
        print("Book added successfully!")
    else:
        print("Please fill in all fields!")

def search_book(query, filter_by=None):
    filtered_books = [book for book in books if query.lower() in book['title'].lower() or query.lower() in book['author'].lower()]
    if filter_by:
        filtered_books = [book for book in filtered_books if book['year'] == filter_by]
    for book in filtered_books:
        print(f"Title: {book['title']}, Author: {book['author']}, Year: {book['year']}, Book No: {book['book_no']}")

def delete_book(book_no):
    global books
    books = [book for book in books if book['book_no'] != book_no]
    print("Book deleted successfully!")

# Simulating the registration and login process
if __name__ == "__main__":
    print("Welcome to the Library Management System")
    action = input("Do you want to register or login? (r/l): ").lower()
    if action == 'r':
        username = input("Enter your username: ")
        password = input("Enter your password: ")
        register(username, password)
    elif action == 'l':
        username = input("Enter your username: ")
        password = input("Enter your password: ")
        login(username, password)
        while True:
            action = input("Do you want to add, search, or delete a book? (a/s/d/q): ").lower()
            if action == 'a':
                title = input("Enter book title: ")
                author = input("Enter author name: ")
                year = input("Enter publication year: ")
                book_no = input("Enter book number: ")
                add_book(title, author, year, book_no)
            elif action == 's':
                query = input("Enter title or author to search: ")
                year_filter = input("Filter by year (leave blank for no filter): ")
                search_book(query, year_filter if year_filter else None)
            elif action == 'd':
                book_no = input("Enter book number to delete: ")
                delete_book(book_no)
            elif action == 'q':
                print("Exiting the system.")
                break
            else:
                print("Invalid option selected.")
    else:
        print("Invalid option selected.")
Leave a Comment