请输入图片描述

前面展示了一个简单的面向对象的图书馆应用,定义了Library,Book两个类,缺少用户的管理,比如默认只有一个用户借书、还书
对于正常实际的图书馆应用比如涉及多个用户使用借书、还书的功能,这里我们就在前面程序的基础上增加用户管理的功能 - 简单的用户注册

这个程序首先新增加了一个类定义User

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.")

User类包含用户基本信息,同时也维护所借阅book的信息

Library类内部需要增加一些数据定义,维护用户的信息

def __init__(self):
        self.books = []
        self.users = []

使用一个list保存注册到Library的用户

完整的演示

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 = []
    
        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:
                    return user
            return 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"))
    
    # 注册用户
    user1 = User("user1", "password1")
    user2 = User("user2", "password2")
    
    # 将用户加入图书馆
    library.add_user(user1)
    library.add_user(user2)
    
    # 用户登录
    logged_in_user = library.user_login("user1", "password1")
    
    if logged_in_user:
        # 用户借书
        library.check_book_status("The Great Gatsby")
        logged_in_user.borrow_book(library.search_book_by_name("The Great Gatsby"))
    
        # 用户还书
        logged_in_user.return_book(library.search_book_by_name("The Great Gatsby"))
    
        # 再次查询书籍状态
        library.check_book_status("The Great Gatsby")
    else:
        print("Login failed. Invalid username or password.")

标签: python