用python写一个完整的图书管理系统

下面是一个简单的Python图书管理系统的示例代码,包括用户管理、图书管理、借还管理和系统管理等功能:

import csv
import os
import hashlib

# 定义用户类
class User:
    def __init__(self, username, password, email, is_admin=False):
        self.username = username
        self.password = hashlib.sha256(password.encode()).hexdigest()
        self.email = email
        self.is_admin = is_admin

    def authenticate(self, password):
        return self.password == hashlib.sha256(password.encode()).hexdigest()

# 定义图书类
class Book:
    def __init__(self, name, author, publisher, publish_date, isbn, introduction, cover):
        self.name = name
        self.author = author
        self.publisher = publisher
        self.publish_date = publish_date
        self.isbn = isbn
        self.introduction = introduction
        self.cover = cover
        self.borrower = None

# 定义图书馆类
class Library:
    def __init__(self):
        self.books = []

    # 添加图书
    def add_book(self, book):
        self.books.append(book)

    # 删除图书
    def delete_book(self, book):
        if book in self.books:
            self.books.remove(book)

    # 查找图书
    def search_book(self, keyword):
        result = []
        for book in self.books:
            if keyword in book.name or keyword in book.author or keyword in book.publisher:
                result.append(book)
        return result

    # 借阅图书
    def borrow_book(self, book, user):
        if book.borrower is None:
            book.borrower = user
        else:
            raise Exception('The book is already borrowed by ' + book.borrower.username)

    # 归还图书
    def return_book(self, book):
        book.borrower = None

# 定义管理员类
class Admin(User):
    def __init__(self, username, password, email):
        super().__init__(username, password, email, is_admin=True)

    # 添加图书
    def add_book(self, library, book):
        library.add_book(book)

    # 删除图书
    def delete_book(self, library, book):
        library.delete_book(book)

    # 查找图书
    def search_book(self, library, keyword):
        return library.search_book(keyword)

# 定义一个系统类
class System:
    def __init__(self):
        self.library = Library()
        self.users = []
        self.current_user = None

    # 注册用户
    def register(self, username, password, email):
        user = User(username, password, email)
        self.users.append(user)

    # 登录
    def login(self, username, password):
        for user in self.users:
            if user.username == username and user.authenticate(password):
                self.current_user = user
                return True
        return False

    # 登出
    def logout(self):
        self.current_user = None

    # 添加管理员
    def add_admin(self, username, password, email):
        admin = Admin(username, password, email)
        self.users.append(admin)

    # 添加图书
    def add_book(self, book):
        if isinstance(self.current_user, Admin):
            self.current_user.add_book(self.library, book)
        else:
            raise Exception('Permission denied')

    # 删除图书
    def delete_book(self, book):
        if isinstance(self.current_user, Admin):
            self.current_user.delete_book(self.library, book)
        else:
            raise Exception('Permission denied')

    # 查找图书
    def search_book(self, keyword):
        return self.library.search_book(keyword)

    # 借阅图书
    def borrow_book(self, book):
        if isinstance(self.current_user, User):
            self.library.borrow_book(book, self.current_user)
        else:
            raise Exception('Permission denied')

    # 归还图书
    def return_book(self, book):
        self.library.return_book(book)

    # 保存数据
    def save_data(self):
        file_path = 'library.csv'
        with open(file_path, 'w') as f:
            writer = csv.writer(f)
            for book in self.library.books:
                writer.writerow([book.name, book.author, book.publisher, book.publish_date, book.isbn, book.introduction, book.cover, book.borrower.username if book.borrower is not None else ''])

        file_path = 'users.csv'
        with open(file_path, 'w') as f:
            writer = csv.writer(f)
            for user in self.users:
                writer.writerow([user.username, user.password, user.email, user.is_admin])

    # 读取数据
    def load_data(self):
        file_path = 'library.csv'
        if os.path.exists(file_path):
            with open(file_path, 'r') as f:
                reader = csv.reader(f)
                books = []
                for row in reader:
                    borrower = None
                    if row[7] != '':
                        for user in self.users:
                            if user.username == row[7]:
                                borrower = user
                                break
                    book = Book(row[0], row[1], row[2], row[3], row[4], row[5], row[6])
                    book.borrower = borrower
                    books.append(book)
                self.library.books = books

        file_path = 'users.csv'
        if os.path.exists(file_path):
            with open(file_path, 'r') as f:
                reader = csv.reader(f)
                users = []
                for row in reader:
                    if row[3] == 'True':
                        user = Admin(row[0], row[1], row[2])
                    else:
                        user = User(row[0], row[1], row[2])
                    users.append(user)
                self.users = users

# 测试
if __name__ == '__main__':
    system = System()
    system.load_data()

    # 注册用户
    system.register('user1', '123456', '[email protected]')
    system.register('user2', '123456', '[email protected]')
    system.add_admin('admin', '123456', '[email protected]')

    # 添加图书
    book1 = Book('Python编程入门', 'Jack', '机械工业出版社', '2020-01-01', '9787111579516', 'Python编程入门教材', 'cover1.png')
    book2 = Book('Python高级编程', 'Tom', '清华大学出版社', '2019-01-01', '9787302423281', 'Python高级编程教材', 'cover2.png')
    system.add_book(book1)
    system.add_book(book2)

    # 登录
    system.login('user1', '123456')

    # 查找图书
    books = system.search_book('Python')
    print('Search Result:')
    for book in books:
        print(book.name)

    # 借阅图书
    system.borrow_book(book1)

    # 登出
    system.logout()

    # 登录管理员
    system.login('admin', '123456')

    # 删除图书
    system.delete_book(book2)

    # 登出
    system.logout()

    # 保存数据
    system.save_data()

上述代码实现了一个完整的图书管理系统,包括用户管理、图书管理、借还管理和系统管理等功能。其中,使用了一个User类、Admin类、Book类和Library类。System类用于管理整个系统,实现了注册用户、登录、添加管理员和保存数据等功能。同时,它还实现了添加图书、删除图书、查找图书、借阅图书和归还图书等具体操作。最后,通过load_data函数和save_data函数,实现数据的持久化。

你可能感兴趣的:(python,开发语言)