写一个pycharm图书管理系统编码
时间: 2023-08-25 17:13:02 浏览: 186
book_PyCharm_python_图书管理系统_
5星 · 资源好评率100%
以下是一个简单的图书管理系统的Python代码示例,使用PyCharm编写:
```python
# 图书管理系统
class Book:
def __init__(self, title, author, isbn, year):
self.title = title
self.author = author
self.isbn = isbn
self.year = year
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, book):
self.books.remove(book)
def search_book(self, title):
for book in self.books:
if book.title == title:
return book
def display_books(self):
for book in self.books:
print(book.title, book.author, book.isbn, book.year)
# 测试
library = Library()
book1 = Book("Python for Everybody", "Charles Severance", "978-1530051120", 2016)
book2 = Book("Automate the Boring Stuff with Python", "Al Sweigart", "978-1593275990", 2015)
library.add_book(book1)
library.add_book(book2)
print("所有图书信息:")
library.display_books()
print("查找Python for Everybody书籍:")
book = library.search_book("Python for Everybody")
print(book.title, book.author, book.isbn, book.year)
print("删除Automate the Boring Stuff with Python书籍:")
library.remove_book(book2)
print("所有图书信息:")
library.display_books()
```
这段代码实现了一个简单的图书管理系统,其中有两个类:Book和Library。Book类表示一本书,包含标题、作者、ISBN号和出版年份。Library类表示图书馆,包含一组书籍,可以添加、删除、查找和显示这些书籍。在测试部分,我们创建了两本书并添加到图书馆中,然后查找并显示其中一本书的信息,最后删除另一本书并显示所有书籍的信息。
阅读全文