这是一个基本的图书库存管理系统的Python代码。这个系统包括以下几个部分:
1. 创建一个名为"Book"的类,用于存储图书的信息。
2. 创建一个名为"Library"的类,用于管理图书馆的所有书籍。
3. 在"Library"类中,添加一个方法来添加新的图书到库存中。
4. 在"Library"类中,添加一个方法来删除库存中的图书。
5. 在"Library"类中,添加一个方法来查找库存中的图书。
6. 在"Library"类中,添加一个方法来显示库存中的所有图书。
7. 在"Library"类中,添加一个方法来更新库存中图书的数量。
8. 在"Library"类中,添加一个方法来打印出库存中所有图书的详细信息。
以下是具体的代码实现:
```python
class Book:
def __init__(self, title, author, isbn):
self.title = title
self.author = author
self.isbn = isbn
self.quantity = 0
def increase_quantity(self):
self.quantity += 1
def decrease_quantity(self):
if self.quantity > 0:
self.quantity -= 1
else:
print("The book is out of stock.")
def get_details(self):
return f"Title: {self.title}, Author: {self.author}, ISBN: {self.isbn}, Quantity: {self.quantity}"
class Library:
def __init__(self):
self.books = []
def add_book(self, book):
self.books.append(book)
def remove_book(self, isbn):
for book in self.books:
if book.isbn == isbn:
self.books.remove(book)
break
else:
print("The book with the given ISBN does not exist.")
def find_book(self, isbn):
for book in self.books:
if book.isbn == isbn:
return book
return None
def display_books(self):
for book in self.books:
print(book.get_details())
def update_stock(self, isbn, quantity):
for book in self.books:
if book.isbn == isbn:
book.increase_quantity()
if book.quantity > quantity:
book.decrease_quantity()
if book.quantity <= 0:
print("The book is out of stock.")
break
else:
print("The book is already full.")
break
else:
print("The book with the given ISBN does not exist.")
```
你可以使用以下方式来使用这个系统:
```python
library = Library()
book1 = Book("Python Programming", "John Doe", "1234567890")
book2 = Book("Java Basics", "Jane Smith", "0987654321")
library.add_book(book1)
library.add_book(book2)
library.display_books()
library.update_stock("1234567890", 5)
library.display_books()
```