请输入图片描述
之前程序都是单一的一次演示,我们希望程序可以多用户使用,所以每个用户应该有登录与登出的操作
前面已经演示了用户注册,登录,这里接着增加登出的操作

'''
ver4.py: 增加用户logout功能
'''
class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author
        self.is_checked_out = False

    def check_out(self):
        if not self.is_checked_out:
            self.is_checked_out = True
            print(f"The book '{self.title}' by {self.author} has been checked out.")
        else:
            print("Sorry, the book is already checked out.")

    def check_in(self):
        if self.is_checked_out:
            self.is_checked_out = False
            print(f"Thank you for returning '{self.title}' by {self.author}.")
        else:
            print("The book is not checked out.")

    def display_info(self):
        status = "Available" if not self.is_checked_out else "Checked Out"
        print(f"Title: {self.title}, Author: {self.author}, Status: {status}")


class User:
    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.borrowed_books = []

    def borrow_book(self, book):
        if not book.is_checked_out:
            book.check_out()
            self.borrowed_books.append(book)
            print(f"{self.username} has borrowed '{book.title}'.")
        else:
            print("Sorry, the book is already checked out.")

    def return_book(self, book):
        if book in self.borrowed_books:
            book.check_in()
            self.borrowed_books.remove(book)
            print(f"{self.username} has returned '{book.title}'.")
        else:
            print("This book is not borrowed by the user.")

class Library:
    def __init__(self):
        self.books = []
        self.users = []
        self.current_user = None

    def add_book(self, book):
        self.books.append(book)

    def add_user(self, user):
        self.users.append(user)

    def list_books(self):
        print("Library Catalog:")
        for book in self.books:
            book.display_info()

    def search_book_by_name(self, book_name):
        for book in self.books:
            if book.title == book_name:
                return book
        return None

    def check_book_status(self, book_name):
        book = self.search_book_by_name(book_name)
        if book:
            if book.is_checked_out:
                print(f"The book '{book_name}' is checked out.")
            else:
                print(f"The book '{book_name}' is available.")
        else:
            print(f"The book '{book_name}' is not found in the library.")

    def user_login(self, username, password):
        for user in self.users:
            if user.username == username and user.password == password:
                self.current_user = user
                return user
        return None

    def user_registration(self, username, password):
        new_user = User(username, password)
        self.users.append(new_user)
        print(f"User '{username}' has been registered.")

    def user_logout(self):
        print(f"Goodbye, {self.current_user.username}!")
        self.current_user = None

# 创建图书馆对象
library = Library()

# 将图书加入图书馆
library.add_book(Book("The Great Gatsby", "F. Scott Fitzgerald"))
library.add_book(Book("To Kill a Mockingbird", "Harper Lee"))
library.add_book(Book("1984", "George Orwell"))

while True:
    print("\nLibrary Menu:")
    print("1. List Books")
    print("2. Check Book Status")
    print("3. User Login")
    print("4. User Registration")
    print("5. Logout")
    print("6. Exit")

    choice = input("Enter your choice (1-6): ")

    if choice == "1":
        library.list_books()
    elif choice == "2":
        book_name = input("Enter the book name: ")
        library.check_book_status(book_name)
    elif choice == "3":
        username = input("Enter your username: ")
        password = input("Enter your password: ")
        logged_in_user = library.user_login(username, password)
        if logged_in_user:
            print(f"Welcome, {logged_in_user.username}!")
            # 用户操作(借书、还书等)
            while True:
                print("\nUser Menu:")
                print("1. Borrow a Book")
                print("2. Return a Book")
                print("3. Logout")
                user_choice = input("Enter your choice (1-3): ")
                if user_choice == "1":
                    book_name = input("Enter the book name to borrow: ")
                    book = library.search_book_by_name(book_name)
                    if book:
                        logged_in_user.borrow_book(book)
                    else:
                        print("Book not found.")
                elif user_choice == "2":
                    book_name = input("Enter the book name to return: ")
                    book = library.search_book_by_name(book_name)
                    if book:
                        logged_in_user.return_book(book)
                    else:
                        print("Book not found.")
                elif user_choice == "3":
                    library.user_logout()
                    break
                else:
                    print("Invalid choice. Please enter a number between 1 and 3.")
        else:
            print("Login failed. Invalid username or password.")
    elif choice == "4":
        username = input("Enter your new username: ")
        password = input("Enter your new password: ")
        library.user_registration(username, password)
    elif choice == "5":
        if library.current_user:
            library.user_logout()
        else:
            print("No user currently logged in.")
    elif choice == "6":
        print("Exiting the library. Goodbye!")
        break
    else:
        print("Invalid choice. Please enter a number between 1 and 6.")

标签: none