当前位置:   article > 正文

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

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', 'user1@gmail.com')
    system.register('user2', '123456', 'user2@gmail.com')
    system.add_admin('admin', '123456', 'admin@gmail.com')

    # 添加图书
    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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/859504
推荐阅读
相关标签
  

闽ICP备14008679号