编写一个Python程序的图书管理系统源代码
时间: 2024-10-07 11:07:30 浏览: 40
图书管理系统源程序,图书管理系统源码,Python
5星 · 资源好评率100%
编写一个简单的图书管理系统的Python源代码,我们可以用字典来存储书籍信息,例如书名、作者和库存。这里提供一个基本的例子:
```python
class LibrarySystem:
def __init__(self):
self.books = {} # 使用字典存储书籍信息
def add_book(self, title, author, quantity):
if title in self.books:
print("Book already exists.")
else:
self.books[title] = {'author': author, 'quantity': quantity}
print(f"Book '{title}' added successfully.")
def remove_book(self, title):
if title not in self.books:
print("Book not found.")
elif self.books[title]['quantity'] == 0:
print("Book is out of stock.")
else:
self.books[title]['quantity'] -= 1
if self.books[title]['quantity'] > 0:
print(f"Book '{title}' removed.")
else:
del self.books[title]
print(f"Book '{title}' has been returned to inventory.")
def borrow_book(self, title):
if title not in self.books or self.books[title]['quantity'] == 0:
print("Book unavailable.")
else:
self.books[title]['quantity'] -= 1
print(f"You borrowed '{title}'. It now has {self.books[title]['quantity']} copies left.")
def return_book(self, title):
if title in self.books and self.books[title]['quantity'] < 0:
print("Book was already returned.")
else:
self.books[title]['quantity'] += 1
print(f"{title} returned. Now available for borrowing.")
# 示例用法
library = LibrarySystem()
library.add_book("Harry Potter", "J.K. Rowling", 3)
library.borrow_book("Harry Potter")
library.return_book("Harry Potter")
```
这个系统允许添加新书、移除已借出的书、查看库存并借还书。然而,这只是一个非常简化的模型,实际应用可能需要数据库支持,错误处理和用户界面。
阅读全文